Package org.hibernate.search

Examples of org.hibernate.search.FullTextQuery


        log.debug(">>>>> search query: " + mainQuery.toString());

        try {

            FullTextQuery ftQuery = getFullTextSession().createFullTextQuery(mainQuery);
            ftQuery.setFirstResult(page * pageSize).setMaxResults(pageSize);
            totalCount = ftQuery.getResultSize();
            log.debug("total search hits (might be paginated next): " + totalCount);
            List result = ftQuery.list();

            // Extract hits
            log.debug("search hits passed to handlers: " + result.size());
            searchResult = new ArrayList<SearchHit>();
            for (Object o : result) {
View Full Code Here


    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 ) {
      fts.delete( e );
    }
    transaction.commit();
View Full Code Here

  public List<?> list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException {
    FullTextSession fullTextSession = Search.getFullTextSession( (Session) session );

    LuceneQueryParsingResult parsingResult = getLuceneQuery( queryParameters, fullTextSession );

    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( parsingResult.getQuery(), parsingResult.getTargetEntity() );

    if ( requiresProjections( parsingResult.getProjections() ) ) {
      fullTextQuery.setProjection( parsingResult.getProjections().toArray( new String[parsingResult.getProjections().size()] ) );
    }

    fullTextQuery.setSort( parsingResult.getSort() );

    // Following options are mandatory to load matching entities without using a query
    // (chicken and egg problem)
    fullTextQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );

    if ( queryParameters.getRowSelection().getFirstRow() != null ) {
      fullTextQuery.setFirstResult( queryParameters.getRowSelection().getFirstRow() );
    }
    if ( queryParameters.getRowSelection().getMaxRows() != null ) {
      fullTextQuery.setMaxResults( queryParameters.getRowSelection().getMaxRows() );
    }

    return fullTextQuery.list();
  }
View Full Code Here

    LuceneQueryParsingResult parsingResult = new QueryParser().parseQuery( queryString,
        createProcessingChain( session, unwrap( namedParameters ), fullTextSession ) );

    log.createdQuery( queryString, parsingResult.getQuery() );

    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( parsingResult.getQuery(), parsingResult.getTargetEntity() );
    if ( requiresProjections( parsingResult.getProjections() ) ) {
      fullTextQuery.setProjection( parsingResult.getProjections().toArray( new String[parsingResult.getProjections().size()] ) );
    }

    fullTextQuery.setSort( parsingResult.getSort() );

    // Following options are mandatory to load matching entities without using a query
    // (chicken and egg problem)
    fullTextQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    return fullTextQuery;
  }
View Full Code Here

    createTestBooks(s);
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser("title", new StopAnalyzer() );

    Query query = parser.parse( "summary:lucene" );
    FullTextQuery hibQuery = s.createFullTextQuery( query, Book.class );
    List<Book> result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 3, result.size() );
    // make sure that the order is according to in which order the books got inserted
    // into the index.
    int id = 1;
    for(Book b : result) {
      assertEquals( "Expected another id", Integer.valueOf( id ), b.getId() );
      id++;
    }

    // now the same query, but with a lucene sort specified.
    query = parser.parse( "summary:lucene" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    Sort sort = new Sort(new SortField("id", true));
    hibQuery.setSort(sort);
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 3, result.size() );
    id = 3;
    for(Book b : result) {
      assertEquals("Expected another id", Integer.valueOf( id ), b.getId());
      id--;
    }

    // order by summary
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "summary_forSort", false ) ); //ASC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    assertEquals( "Groovy in Action", result.get( 0 ).getSummary() );

    // order by summary backwards
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "summary_forSort", true ) ); //DESC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    assertEquals( "Hibernate & Lucene", result.get( 0 ).getSummary() );

    // order by date backwards
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "publicationDate", SortField.STRING, true ) ); //DESC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    for (Book book : result) {
      System.out.println(book.getSummary() + " : " + book.getPublicationDate() );
    }
View Full Code Here

    Map<String, Float> boosts = new HashMap<String, Float>(2);
    boosts.put( "title", new Float(4) );
    boosts.put( "description", new Float(1) );
    MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {"title", "description"}, new StandardAnalyzer(), boosts);
    Query luceneQuery = parser.parse( "dark" );
    FullTextQuery ftQuery = s.createFullTextQuery( luceneQuery, Dvd.class )
        .setProjection( FullTextQuery.DOCUMENT_ID, FullTextQuery.EXPLANATION, FullTextQuery.THIS );
    @SuppressWarnings("unchecked") List<Object[]> results = ftQuery.list();
    assertEquals( 2, results.size() );
    for (Object[] result : results) {
      assertEquals( ftQuery.explain( (Integer) result[0] ).toString(), result[1].toString() );
      s.delete( result[2] );
    }
    tx.commit();
    s.close();
View Full Code Here

    // search if the record made it into the index
    tx = s.beginTransaction();
    String searchQuery = "Joe";
    QueryParser parser = new QueryParser( "Content", new StandardAnalyzer() );
    Query luceneQuery = parser.parse( searchQuery );
    FullTextQuery query = s.createFullTextQuery( luceneQuery );
    List results = query.list();
    assertTrue( "We should have a hit", results.size() == 1 );
    tx.commit();

    // Now try to delete
    tx = s.beginTransaction();
View Full Code Here

    Query query = parser.parse( "summary:Festina Or brand:Seiko" );
    Statistics stats = s.getSessionFactory().getStatistics();
    stats.clear();
    boolean enabled = stats.isStatisticsEnabled();
    if ( !enabled ) stats.setStatisticsEnabled( true );
    FullTextQuery hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    assertEquals( "Exection of getResultSize without actual results", 2, hibQuery.getResultSize() );
    assertEquals( "No entity should be loaded", 0, stats.getEntityLoadCount() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "2 entities should be loaded", 2, stats.getEntityLoadCount() );
    if ( !enabled ) stats.setStatisticsEnabled( false );
    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
View Full Code Here

 
  public void assertFindsByRoadName(String analyzedRoadname) {
    FullTextSession fullTextSession = Search.getFullTextSession( sessions.openSession() );
    Transaction tx = fullTextSession.beginTransaction();
    TermQuery ftQuery = new TermQuery( new Term( "stops.roadName", analyzedRoadname ) );
    FullTextQuery query = fullTextSession.createFullTextQuery( ftQuery, BusLine.class );
    query.setProjection( "busLineName" );
    assertEquals( 1, query.list().size() );
    List results = query.list();
    String resultName = (String) ((Object[])results.get(0))[0];
    assertEquals( "Linea 64", resultName );
    tx.commit();
    fullTextSession.close();
  }
View Full Code Here

    tx.commit();

    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "name", new StandardAnalyzer() );
    Query query = parser.parse( "name:foo" );
    FullTextQuery hibQuery = s.createFullTextQuery( query );
    try {
      hibQuery.list();
      fail();
    }
    catch ( HibernateException e ) {
      assertTrue( "Wrong message", e.getMessage().startsWith( "There are no mapped entities" ) );
    }
View Full Code Here

TOP

Related Classes of org.hibernate.search.FullTextQuery

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.