Package org.hibernate.search.query.dsl

Examples of org.hibernate.search.query.dsl.QueryBuilder


        searchSelections = new HashMap<Product, Boolean>();
    }

    private FullTextQuery searchQuery(String textQuery) {
        QueryBuilder queryBuilder = entityManager.getSearchFactory()
           .buildQueryBuilder().forEntity(Product.class).get();
       
        //Hibernate Search fulltext query example:
       
        //query to match exact terms occurrence, using custom boosts:
        Query queryToFindExactTerms = queryBuilder.keyword()
           .onFields("title").boostedTo(4f)
           .andField("description").boostedTo(2f)
           .andField("actors.name").boostedTo(2f)
           .andField("categories.name").boostedTo(0.5f)
           .matching(textQuery)
           .createQuery();
       
        //Similar query, but using NGram matching instead of exact terms:
        Query queryToFindMathingNGrams = queryBuilder.keyword()
           .onFields("title:ngrams").boostedTo(2f)
           .andField("description:ngrams")
           .andField("actors.name:ngrams")
           .andField("categories.name:ngrams").boostedTo(0.2f)
           .matching(textQuery)
           .createQuery();
       
        //Combine them for best results, note exact uses an higher boost:
        Query fullTextQuery = queryBuilder.bool()
           .should(queryToFindMathingNGrams)
           .should(queryToFindExactTerms)
           .createQuery();
       
        log.info("Executing fulltext query {0}", fullTextQuery);
View Full Code Here


   }

   private Query getFullTextQuery()
   {
      //Create a QueryBuilder targeting BlogEntry
      QueryBuilder queryBuilder = entityManager
         .getSearchFactory()
         .buildQueryBuilder()
         .forEntity(BlogEntry.class)
         .get();
     
      //A fulltext query using English Analyzer
      Query queryUsingEnglishStemmer = queryBuilder.keyword()
         .onFields("title:en").boostedTo(4f)
         .andField("body:en")
         .matching(searchPattern)
         .createQuery();
     
      //A fulltext query using ngrams
      Query queryUsingNGrams = queryBuilder.keyword()
         .onFields("title:ngrams").boostedTo(2f)
         .andField("body:ngrams").boostedTo(0.4f)
         .matching(searchPattern)
         .createQuery();
     
      //Combine them for best results:
      Query fullTextQuery = queryBuilder.bool()
         .should(queryUsingEnglishStemmer)
         .should(queryUsingNGrams)
         .createQuery();
     
      return fullTextQuery;
View Full Code Here

    transaction.commit();

    fts.clear();

    transaction = fts.beginTransaction();
    final QueryBuilder b = fts.getSearchFactory()
        .buildQueryBuilder()
        .forEntity( Insurance.class )
        .get();
    final Query lq = b.keyword().onField( "name" ).matching( "Macif" ).createQuery();
    final FullTextQuery ftQuery = fts.createFullTextQuery( lq, Insurance.class );
    ftQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    final List<Insurance> resultList = ftQuery.list();
    assertThat( resultList ).hasSize( 1 );
    for ( Object e : resultList ) {
View Full Code Here

    getTransactionManager().commit();

    ftem.clear();

    getTransactionManager().begin();
    final QueryBuilder b = ftem.getSearchFactory()
        .buildQueryBuilder()
        .forEntity( Insurance.class )
        .get();
    final Query lq = b.keyword().onField( "name" ).matching( "Macif" ).createQuery();
    final FullTextQuery ftQuery = ftem.createFullTextQuery( lq, Insurance.class );
    ftQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    final List<Insurance> resultList = ftQuery.getResultList();
    assertThat( getFactory().getPersistenceUnitUtil().isLoaded( resultList.get( 0 ) ) ).isTrue();
    assertThat( resultList ).hasSize( 1 );
View Full Code Here

    getTransactionManager().commit();

    ftSession.clear();

    getTransactionManager().begin();
    final QueryBuilder b = ftSession.getSearchFactory()
        .buildQueryBuilder()
        .forEntity( Insurance.class )
        .get();
    final Query lq = b.keyword().onField( "name" ).matching( "Macif" ).createQuery();
    final org.hibernate.search.FullTextQuery ftQuery = ftSession.createFullTextQuery( lq, Insurance.class );
    ftQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    final List<Insurance> resultList = ftQuery.list();
    assertThat( getFactory().getPersistenceUnitUtil().isLoaded( resultList.get( 0 ) ) ).isTrue();
    assertThat( resultList ).hasSize( 1 );
View Full Code Here

    boolean operationSuccessful = false;
    FullTextEntityManager fullTextEm = null;
    try {
      getTransactionManager().begin();
      fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
      QueryBuilder queryBuilder = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity( IndexedNews.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "newsId" ).ignoreFieldBridge().matching( "tit*" ).createQuery();
      @SuppressWarnings("unchecked")
      List<IndexedNews> list = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
      assertThat( list ).hasSize( 1 );

      List<IndexedLabel> labels = list.get( 0 ).getLabels();
View Full Code Here

    boolean operationSuccessful = false;
    FullTextEntityManager fullTextEm = null;
    try {
      getTransactionManager().begin();
      fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
      QueryBuilder b = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity( IndexedLabel.class ).get();
      {
        Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "tes*" ).createQuery();
        List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
        assertThat( labels ).hasSize( 1 );
        assertThat( contains( labels, "test" ) ).isTrue();
      }
      {
        Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "mas*" ).createQuery();
        List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
        assertThat( labels ).hasSize( 1 );
        assertThat( contains( labels, "massindex" ) ).isTrue();
      }
      operationSuccessful = true;
View Full Code Here

      purgeAll( Insurance.class );
      startAndWaitMassIndexing( Insurance.class );
    }
    {
      FullTextSession session = Search.getFullTextSession( openSession() );
      QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( Insurance.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "name" ).matching( "ins*" ).createQuery();
      Transaction transaction = session.beginTransaction();
      @SuppressWarnings("unchecked")
      List<Insurance> list = session.createFullTextQuery( luceneQuery ).list();
      assertThat( list ).hasSize( 1 );
      assertThat( list.get( 0 ).getName() ).isEqualTo( "Insurance Corporation" );
View Full Code Here

      startAndWaitMassIndexing( IndexedNews.class );
    }
    {
      // Assert index creation
      FullTextSession session = Search.getFullTextSession( openSession() );
      QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( IndexedNews.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "newsId" ).ignoreFieldBridge().matching( "tit*" ).createQuery();
      Transaction transaction = session.beginTransaction();
      @SuppressWarnings("unchecked")
      List<IndexedNews> list = session.createFullTextQuery( luceneQuery ).list();
      assertThat( list ).hasSize( 1 );
      assertThat( list.get( 0 ).getContent() ).isEqualTo( "content" );
View Full Code Here

        .getSingleResult();
  }

  public static Member findWithEmail(EntityManager em, String email) {
    FullTextEntityManager ftem = Search.getFullTextEntityManager( em );
    QueryBuilder b = ftem.getSearchFactory().buildQueryBuilder().forEntity( Member.class ).get();
    Query lq = b.keyword().wildcard().onField( "email" ).matching( email ).createQuery();
    Object uniqueResult = ftem.createFullTextQuery( lq ).getSingleResult();
    return (Member) uniqueResult;
  }
View Full Code Here

TOP

Related Classes of org.hibernate.search.query.dsl.QueryBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.