@Override public Query visit(NotExpr notExpr) { Query transformedChild = notExpr.getChild().acceptVisitor(this); return queryBuilder.bool().must(transformedChild).not().createQuery(); }
@Override public Query visit(NotExpr notExpr) { Query transformedChild = notExpr.getChild().acceptVisitor(this); return queryBuilder.bool().must(transformedChild).not().createQuery(); }
@Override public Query getQuery() { return builder.bool().must( getChild().getQuery() ).not().createQuery(); }
@Override public Query visit(ConstantBooleanExpr constantBooleanExpr) { Query all = queryBuilder.all().createQuery(); return constantBooleanExpr.getValue() ? all : queryBuilder.bool().must(all).not().createQuery(); }
@Override public Query visit(ConstantBooleanExpr constantBooleanExpr) { Query all = queryBuilder.all().createQuery(); return constantBooleanExpr.getValue() ? all : queryBuilder.bool().must(all).not().createQuery(); }
@Override public Query getQuery() { BooleanJunction<BooleanJunction> booleanJunction = builder.bool(); for ( Predicate<Query> predicate : children ) { // minor optimization: unwrap negated predicates and add child directly to this // predicate if ( predicate.getType() == Type.NEGATION ) { booleanJunction.must( predicate.as( LuceneNegationPredicate.class ).getChild().getQuery() ).not(); } else { booleanJunction.must( predicate.getQuery() ); } } return booleanJunction.createQuery(); } }
public void testBooleanQueriesMustNot() throws ParseException { loadTestingData(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .must(createQueryParser("name").parse("Goat")).not().createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(1, found.size()); assert found.contains(person1); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .must(createQueryParser("name").parse("Goat")).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(person2); assert found.contains(person3); }
@Test @TestForIssue(jiraKey = "HSEARCH-2037") public void testBooleanWithOnlyNegationQueries() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //must + disable scoring Query query = monthQb .bool() .must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() ) .not() //expectation: exclude January .must( monthQb.keyword().onField( "mythology" ).matching( "snowboarding" ).createQuery() ) .not() //expectation: exclude February .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 3 ); assertTrue( query instanceof BooleanQuery ); BooleanQuery bq = (BooleanQuery) query; BooleanClause firstBooleanClause = bq.clauses().get( 0 ); assertFalse( firstBooleanClause.isScoring() ); }
@Test public void mustNot() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); HSQuery query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ).not() .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_2, DOCUMENT_3 ); query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ).not() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE3 ).createQuery() ).not() .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_2 ); }
@Test @TestForIssue(jiraKey = "HSEARCH-2565") public void testBooleanWithNullClauses() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); // must/should with null clauses Query query = monthQb .bool() .must( null ) .must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() ) .should( null ) .createQuery(); Assert.assertThat( query, CoreMatchers.instanceOf( BooleanQuery.class ) ); Assert.assertEquals( 1, ( (BooleanQuery) query ).clauses().size() ); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1 ); // must not / filter with null clauses query = monthQb .bool() .must( null ).not() .must( null ).disableScoring() .must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() ) .createQuery(); Assert.assertThat( query, CoreMatchers.instanceOf( BooleanQuery.class ) ); Assert.assertEquals( 1, ( (BooleanQuery) query ).clauses().size() ); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1 ); }
@Test public void must_mustNot() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); HSQuery query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ) .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ).not() .createQuery() ); helper.assertThat( query ).matchesNone(); query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ) .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE2 ).createQuery() ).not() .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_1 ); }
@Test public void mustNot_should() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); // A boolean predicate with mustNot + should clauses: // documents should match only if at least one should clause matches // Non-matching "should" clauses HSQuery query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE2 ).createQuery() ).not() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE3 ).createQuery() ).not() .should( queryBuilder.keyword().onField( "field2" ).matching( FIELD2_VALUE2 ).createQuery() ) .should( queryBuilder.keyword().onField( "field3" ).matching( FIELD3_VALUE3 ).createQuery() ) .createQuery() ); helper.assertThat( query ) .matchesNone(); // One matching and one non-matching "should" clause query = helper.hsQuery( queryBuilder.bool() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ).not() .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE3 ).createQuery() ).not() .should( queryBuilder.keyword().onField( "field2" ).matching( FIELD2_VALUE2 ).createQuery() ) .should( queryBuilder.keyword().onField( "field3" ).matching( FIELD3_VALUE3 ).createQuery() ) .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_2 ); }
@Test public void should_mustNot() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); HSQuery query = helper.hsQuery( queryBuilder.bool() .should( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ) .should( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE3 ).createQuery() ) .must( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ).not() .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_3 ); }
@Override public Query visit(ComparisonExpr comparisonExpr) { PropertyValueExpr propertyValueExpr = (PropertyValueExpr) comparisonExpr.getLeftChild(); ConstantValueExpr constantValueExpr = (ConstantValueExpr) comparisonExpr.getRightChild(); Comparable value = constantValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); switch (comparisonExpr.getComparisonType()) { case NOT_EQUAL: Query q = applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(value).createQuery(); return queryBuilder.bool().must(q).not().createQuery(); case EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(value).createQuery(); case LESS: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .below(value).excludeLimit().createQuery(); case LESS_OR_EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .below(value).createQuery(); case GREATER: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .above(value).excludeLimit().createQuery(); case GREATER_OR_EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .above(value).createQuery(); default: throw new IllegalStateException("Unexpected comparison type: " + comparisonExpr.getComparisonType()); } }
@Override public Query visit(ComparisonExpr comparisonExpr) { PropertyValueExpr propertyValueExpr = (PropertyValueExpr) comparisonExpr.getLeftChild(); ConstantValueExpr constantValueExpr = (ConstantValueExpr) comparisonExpr.getRightChild(); Comparable value = constantValueExpr.getConstantValueAs(propertyValueExpr.getPrimitiveType(), namedParameters); switch (comparisonExpr.getComparisonType()) { case NOT_EQUAL: Query q = applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(value).createQuery(); return queryBuilder.bool().must(q).not().createQuery(); case EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.keyword().onField(propertyValueExpr.getPropertyPath().asStringPath())) .matching(value).createQuery(); case LESS: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .below(value).excludeLimit().createQuery(); case LESS_OR_EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .below(value).createQuery(); case GREATER: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .above(value).excludeLimit().createQuery(); case GREATER_OR_EQUAL: return applyFieldBridge(false, propertyValueExpr.getPropertyPath(), queryBuilder.range().onField(propertyValueExpr.getPropertyPath().asStringPath())) .above(value).createQuery(); default: throw new IllegalStateException("Unexpected comparison type: " + comparisonExpr.getComparisonType()); } }