Package org.hibernate.search.query.dsl

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


  @Test
  @SuppressWarnings("unchecked")
  public void testMoreLikeThisBasicBehavior() {
    Transaction transaction = fullTextSession.beginTransaction();
    try {
      QueryBuilder qb = getCoffeeQueryBuilder();
      Coffee decaffInstance = getDecaffInstance( qb );
      Query mltQuery = qb
          .moreLikeThis()
          .favorSignificantTermsWithFactor( 1 )
          .comparingAllFields()
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      List<Object[]> results = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();

      assertThat( results ).isNotEmpty();

      Set<Term> terms = new HashSet<Term>( 100 );
      mltQuery.extractTerms( terms );
      assertThat( terms )
          .describedAs( "internalDescription should be ignored" )
          .doesNotSatisfy(
              new Condition<Collection<?>>() {
                @Override
                public boolean matches(Collection<?> value) {
                  for ( Term term : (Collection<Term>) value ) {
                    if ( "internalDescription".equals( term.field() ) ) {
                      return true;
                    }
                  }
                  return false;
                }
              }
          );
      outputQueryAndResults( decaffInstance, mltQuery, results );

      //custom fields
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      results = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();

      assertThat( results ).isNotEmpty();
      assertThat( mltQuery instanceof BooleanQuery );
      BooleanQuery topMltQuery = (BooleanQuery) mltQuery;
      // FIXME: I'd prefer a test that uses data instead of how the query is actually built
      assertThat( topMltQuery.getClauses() ).onProperty( "query.boost" ).contains( 1f, 10f );

      outputQueryAndResults( decaffInstance, mltQuery, results );

      //using non compatible field
      try {
        qb
            .moreLikeThis()
            .comparingField( "summary" )
            .andField( "internalDescription" )
            .toEntityWithId( decaffInstance.getId() )
            .createQuery();
View Full Code Here


  @SuppressWarnings("unchecked")
  public void testMoreLikeThisToEntity() {
    Transaction transaction = fullTextSession.beginTransaction();
    Query mltQuery;
    try {
      QueryBuilder qb = getCoffeeQueryBuilder();
      Coffee decaffInstance = getDecaffInstance( qb );
      // query results to compare toEntity() results against
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      List<Object[]> results = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();

      // pass entity itself in a managed state
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntity( decaffInstance )
          .createQuery();
      List<Object[]> entityResults = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();

      // query from id and from the managed entity should match
      assertThat( entityResults ).isNotEmpty();
      assertThat( entityResults ).hasSize( results.size() );
      for ( int index = 0; index < entityResults.size(); index++ ) {
        Object[] real = entityResults.get( index );
        Object[] expected = results.get( index );
        assertThat( real[1] ).isEqualTo( expected[1] );
        assertThat( ( (Coffee) real[0] ).getId() ).isEqualTo( ( (Coffee) expected[0] ).getId() );
      }

      outputQueryAndResults( decaffInstance, mltQuery, entityResults );

      // pass entity itself with a matching id but different values
      // the id should take precedene
      Coffee nonMatchingOne = (Coffee) results.get( results.size() - 1 )[0];
      Coffee copyOfDecaffInstance = new Coffee();
      copyOfDecaffInstance.setId( decaffInstance.getId() );
      copyOfDecaffInstance.setInternalDescription( nonMatchingOne.getInternalDescription() );
      copyOfDecaffInstance.setName( nonMatchingOne.getName() );
      copyOfDecaffInstance.setDescription( nonMatchingOne.getDescription() );
      copyOfDecaffInstance.setIntensity( nonMatchingOne.getIntensity() );
      copyOfDecaffInstance.setSummary( nonMatchingOne.getSummary() );
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntity( copyOfDecaffInstance )
          .createQuery();
      entityResults = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();

      // query from id and from the managed entity should match
      assertThat( entityResults ).isNotEmpty();
      assertThat( entityResults ).hasSize( results.size() );
      for ( int index = 0; index < entityResults.size(); index++ ) {
        Object[] real = entityResults.get( index );
        Object[] expected = results.get( index );
        assertThat( real[1] ).isEqualTo( expected[1] );
        assertThat( ( (Coffee) real[0] ).getId() ).isEqualTo( ( (Coffee) expected[0] ).getId() );
      }

      outputQueryAndResults( decaffInstance, mltQuery, entityResults );

      // pass entity itself with the right values but no id
      copyOfDecaffInstance = new Coffee();
      copyOfDecaffInstance.setInternalDescription( decaffInstance.getInternalDescription() );
      copyOfDecaffInstance.setName( decaffInstance.getName() );
      copyOfDecaffInstance.setDescription( decaffInstance.getDescription() );
      copyOfDecaffInstance.setIntensity( decaffInstance.getIntensity() );
      copyOfDecaffInstance.setSummary( decaffInstance.getSummary() );
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntity( copyOfDecaffInstance )
          .createQuery();
View Full Code Here

  public void testMoreLikeThisExcludingEntityBeingCompared() {
    Transaction transaction = fullTextSession.beginTransaction();
    Query mltQuery;
    List<Object[]> results;
    try {
      QueryBuilder qb = getCoffeeQueryBuilder();
      Coffee decaffInstance = getDecaffInstance( qb );

      // exclude comparing entity
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      results = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();
      mltQuery = qb
          .moreLikeThis()
          .excludeEntityUsedForComparison()
          .comparingField( "summary" ).boostedTo( 10f )
          .andField( "description" )
          .toEntityWithId( decaffInstance.getId() )
View Full Code Here

  public void testMoreLikeThisOnCompressedFields() {
    Transaction transaction = fullTextSession.beginTransaction();
    Query mltQuery;
    List<Object[]> entityResults;
    try {
      QueryBuilder qb = getCoffeeQueryBuilder();
      Coffee decaffInstance = getDecaffInstance( qb );
      // using compressed field
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "brand.description" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      entityResults = (List<Object[]>) fullTextSession
View Full Code Here

  public void testMoreLikeThisOnEmbeddedFields() {
    Transaction transaction = fullTextSession.beginTransaction();
    Query mltQuery;
    List<Object[]> entityResults;
    try {
      QueryBuilder qb = getCoffeeQueryBuilder();
      Coffee decaffInstance = getDecaffInstance( qb );
      // using fields from IndexedEmbedded
      mltQuery = qb
          .moreLikeThis()
          .comparingField( "brand.name" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      entityResults = (List<Object[]>) fullTextSession
          .createFullTextQuery( mltQuery, Coffee.class )
          .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE )
          .list();
      assertThat( entityResults ).isNotEmpty();
      Long matchingElements = (Long) fullTextSession.createQuery(
          "select count(*) from Coffee c where c.brand.name like '%pony'"
      ).uniqueResult();
      assertThat( entityResults ).hasSize( matchingElements.intValue() );
      float score = -1;
      for ( Object[] element : entityResults ) {
        if ( score == -1 ) {
          score = (Float) element[1];
        }
        assertThat( element[1] ).as( "All scores should be equal as the same brand is used" )
            .isEqualTo( score );
      }
      outputQueryAndResults( decaffInstance, mltQuery, entityResults );

      // using indexed embedded id from document
      try {
        qb
            .moreLikeThis()
            .comparingField( "brand.id" )
            .toEntityWithId( decaffInstance.getId() )
            .createQuery();
      }
View Full Code Here

  }

  @Test
  @TestForIssue(jiraKey = "HSEARCH-1614")
  public void testMoreLikeThisOnUnknownFieldThrowsException() {
    QueryBuilder queryBuilder = getCoffeeQueryBuilder();
    Coffee decaffInstance = getDecaffInstance( queryBuilder );

    try {
      queryBuilder.moreLikeThis()
          .comparingField( "foo" )
          .toEntityWithId( decaffInstance.getId() )
          .createQuery();
      fail( "Creating the query should have failed" );
    }
View Full Code Here

  private void verifyResult(FullTextSessionBuilder node) {
    FullTextSession fullTextSession = node.openFullTextSession();
    try {
      Transaction transaction = fullTextSession.beginTransaction();
      QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder()
          .forEntity( Toaster.class ).get();
      Query query = queryBuilder.keyword().onField( "serialNumber" ).matching( "A1" ).createQuery();
      List list = fullTextSession.createFullTextQuery( query ).list();
      assertEquals( 1, list.size() );
      Device device = (Device) list.get( 0 );

      assertEquals( "GE", device.manufacturer );
View Full Code Here

      builder.close();
    }
  }

  private void performtest(FullTextSessionBuilder builder, String... expectedLoadedFields) throws IOException {
    QueryBuilder queryBuilder = builder.getSearchFactory().buildQueryBuilder().forEntity( Item.class ).get();
    Query query = queryBuilder.all().createQuery();
    FullTextSession fullTextSession = builder.openFullTextSession();
    Transaction transaction = fullTextSession.beginTransaction();
    FieldSelectorLeakingReaderProvider.resetFieldSelector();
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query );
    List list = fullTextQuery.list();
View Full Code Here

    }
  }

  private List<EntityA> search(Session s, String field, String value) {
    FullTextSession session = Search.getFullTextSession( s );
    QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( EntityA.class ).get();
    Query query = queryBuilder.keyword().onField( field ).matching( value ).createQuery();
    @SuppressWarnings("unchecked")
    List<EntityA> result = session.createFullTextQuery( query ).list();
    return result;
  }
View Full Code Here

    s.clear();

    s.getTransaction().begin();
    FullTextSession fts = Search.getFullTextSession( s );
    QueryBuilder qb = fts.getSearchFactory().buildQueryBuilder().forEntity( Theater.class ).get();
    Query query = qb.keyword().onField( "movie" ).matching( laConfidential ).createQuery();
    assertThat( fts.createFullTextQuery( query, Theater.class ).list() )
        .as( "The SearchFactory should build and find a bridge for Movie in Theater and  properly use it for indexing" )
        .hasSize( 1 );
    s.getTransaction().commit();
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.