BooleanJunction<BooleanJunction> tempJunction = queryBuilder.bool(); for (Integer categoryId : categoryIds) { tempJunction.should(new TermQuery(new Term("categoryId", categoryId.toString()))); BooleanJunction<BooleanJunction> tempJunction = queryBuilder.bool(); for (String modelId : modelIds) { tempJunction.should(new TermQuery(new Term("modelId", modelId)));
BooleanJunction<BooleanJunction> tempJunction = queryBuilder.bool(); for (Integer categoryId : categoryIds) { tempJunction.should(new TermQuery(new Term("categoryId", categoryId.toString()))); BooleanJunction<BooleanJunction> tempJunction = queryBuilder.bool(); for (String modelId : modelIds) { tempJunction.should(new TermQuery(new Term("modelId", modelId)));
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldKeywordsWithAttributesOnFields(Map<String, String> eqSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.keyword() .onField(field) .matching(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } return combinedQuery; }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldPhraseWithAttributesOnFields(Map<String, String> eqSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } return combinedQuery; }
@Override public Query getQuery() { BooleanJunction<BooleanJunction> booleanJunction = builder.bool(); for ( Predicate<Query> predicate : children ) { booleanJunction.should( predicate.getQuery() ); } return booleanJunction.createQuery(); } }
@Override public Query visit(OrExpr orExpr) { BooleanJunction<BooleanJunction> booleanJunction = queryBuilder.bool(); for (BooleanExpr c : orExpr.getChildren()) { Query transformedChild = c.acceptVisitor(this); booleanJunction.should(transformedChild); } return booleanJunction.createQuery(); }
@Override public Query visit(OrExpr orExpr) { BooleanJunction<BooleanJunction> booleanJunction = queryBuilder.bool(); for (BooleanExpr c : orExpr.getChildren()) { Query transformedChild = c.acceptVisitor(this); booleanJunction.should(transformedChild); } return booleanJunction.createQuery(); }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldWMustPhraseWithAttributesOnFields(Map<String, String> eqSentenceOnField,Map<String, String> eqMustSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } combinedQuery.must(combinedQuery.createQuery()); //combinedQuery.setMinimumShouldMatch(2); for(String field : eqMustSentenceOnField.keySet()){ String value = eqMustSentenceOnField.get(field); Query luceneQuery = qb.phrase() .onField(field) .sentence(value) .createQuery(); combinedQuery.must(luceneQuery); //must(luceneQuery); } return combinedQuery; }
@SuppressWarnings("rawtypes") private BooleanJunction<BooleanJunction> addShouldWMustKeywordsWithAttributesOnFields(Map<String, String> eqSentenceOnField,Map<String, String> eqMustSentenceOnField, QueryBuilder qb, BooleanJunction<BooleanJunction> combinedQuery) { for(String field : eqSentenceOnField.keySet()){ String value = eqSentenceOnField.get(field); Query luceneQuery = qb.keyword() .onField(field) .matching(value) .createQuery(); combinedQuery.should(luceneQuery); //must(luceneQuery); } combinedQuery.must(combinedQuery.createQuery()); //combinedQuery.setMinimumShouldMatch(2); for(String field : eqMustSentenceOnField.keySet()){ String value = eqMustSentenceOnField.get(field); Query luceneQuery = qb.keyword() .onField(field) .matching(value) .createQuery(); combinedQuery.must(luceneQuery); //must(luceneQuery); } return combinedQuery; }
@SuppressWarnings("rawtypes") private FullTextQuery createFullTextQueryForfindSetOfExactByAttributes( Set<Map<String, String>> setEqSentenceOnFields) { FullTextSession fullTextSession = getFullTextSession(); QueryBuilder qb = getQueryBuilder(fullTextSession); BooleanJunction<BooleanJunction> combinedQuery = qb.bool(); for(Map<String, String> eqSentenceOnField : setEqSentenceOnFields){ BooleanJunction<BooleanJunction> subCombinedQuery = qb.bool(); subCombinedQuery = addMustPhraseWithAttributesOnFields(eqSentenceOnField, qb, subCombinedQuery); combinedQuery.should(subCombinedQuery.createQuery()); } FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(combinedQuery.createQuery()); return fullTextQuery; }
@SuppressWarnings("rawtypes") private FullTextQuery createFullTextQueryForfindSetOfStartingUsingWildcardAndSetOfExactByAttributes( Set<Map<String, String>> setOfStartSentenceOnFields, Set<Map<String, String>> setOfEqSentenceOnFields) { FullTextSession fullTextSession = getFullTextSession(); QueryBuilder qb = getQueryBuilder(fullTextSession); BooleanJunction<BooleanJunction> combinedQuery = qb.bool(); for(Map<String, String> startSentenceOnField : setOfStartSentenceOnFields){ for(Map<String, String> eqSentenceOnField : setOfEqSentenceOnFields){ BooleanJunction<BooleanJunction> subCombinedQuery = qb.bool(); subCombinedQuery = addMustPhraseWithAttributesOnFields(eqSentenceOnField, qb, subCombinedQuery); subCombinedQuery = addMustWildcardWithStartingAttributesOnFields(startSentenceOnField, qb, subCombinedQuery); combinedQuery.should(subCombinedQuery.createQuery()); } } FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(combinedQuery.createQuery()); return fullTextQuery; }
@SuppressWarnings("rawtypes") private FullTextQuery createFullTextQueryForfindSetOfMultiFieldSameAttributesAndSetOfExactByAttributes( Set<Map<String, Set<String>>> setAttributeForMultipleFieldsMaps, Set<Map<String, String>> setEqSentenceOnFields) { FullTextSession fullTextSession = getFullTextSession(); QueryBuilder qb = getQueryBuilder(fullTextSession); BooleanJunction<BooleanJunction> combinedQuery = qb.bool(); for(Map<String, Set<String>> attributeForMultipleFieldsMap : setAttributeForMultipleFieldsMaps){ for(Map<String, String> eqSentenceOnField : setEqSentenceOnFields){ BooleanJunction<BooleanJunction> subCombinedQuery = qb.bool(); subCombinedQuery = addMustPhraseWithAttributesOnFields(eqSentenceOnField, qb, subCombinedQuery); subCombinedQuery = addMustPhraseWithMultiFieldOnAttribute(attributeForMultipleFieldsMap, qb, subCombinedQuery); combinedQuery.should(subCombinedQuery.createQuery()); } } FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(combinedQuery.createQuery()); return fullTextQuery; }
public void testBooleanQueriesOnMultipleTables() throws ParseException { loadTestingData(); AnotherGrassEater anotherGrassEater = new AnotherGrassEater("Another grass-eater", "Eats grass"); cache.put("key4", anotherGrassEater); Query subQuery = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().range() .onField("age").below(20).createQuery(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(AnotherGrassEater.class).get().bool() .should(createQueryParser("name").parse("grass")).should(subQuery).createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(person1); assert found.contains(anotherGrassEater); }
public void testBooleanQueriesShould() throws ParseException { loadTestingData(); Query subQuery = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().range() .onField("age").below(20).createQuery(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(createQueryParser("name").parse("Goat")).should(subQuery).createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.contains(person1); assert found.contains(person2); assert found.contains(person3); subQuery = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().range() .onField("age").below(20).excludeLimit().createQuery(); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(createQueryParser("name").parse("Goat")).should(subQuery).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(2, found.size()); assert found.contains(person2); assert found.contains(person3); }
@Test public void testQueryCustomization() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //combined query, January and February both contain whitening but February in a longer text Query query = monthQb .bool() .should( monthQb.keyword().onField( "mythology" ).matching( "whitening" ).createQuery() ) .should( monthQb.keyword().onField( "history" ).matching( "whitening" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //boosted query, January and February both contain whitening but February in a longer text //since history is boosted, February should come first though query = monthQb .bool() .should( monthQb.keyword().onField( "mythology" ).matching( "whitening" ).createQuery() ) .should( monthQb.keyword().onField( "history" ).boostedTo( 30 ).matching( "whitening" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 1 ); //FIXME add other method tests besides boostedTo }
@Test public void should() { QueryBuilder queryBuilder = helper.queryBuilder( IndexedEntity.class ); HSQuery query = helper.hsQuery( queryBuilder.bool() .should( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ) .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_1 ); query = helper.hsQuery( queryBuilder.bool() .should( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE1 ).createQuery() ) .should( queryBuilder.keyword().onField( "field1" ).matching( FIELD1_VALUE2 ).createQuery() ) .createQuery() ); helper.assertThat( query ) .matchesUnorderedIds( DOCUMENT_1, DOCUMENT_2 ); }
@Test public void testBoostOnTermQuery() { QueryBuilder qb = helper.queryBuilder( Coffee.class ); Query query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 40f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "summary" ).boostedTo( 1f ).matching( "VELVETY" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Kazaar", "Dharkan" ); query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 1f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "summary" ).boostedTo( 40f ).matching( "VELVETY" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Dharkan", "Kazaar" ); }
@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 ); }
@Test public void testBoostOnNumericQuery() { QueryBuilder qb = helper.queryBuilder( Coffee.class ); Query query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 40f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "intensity" ).boostedTo( 1f ).matching( 11 ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Kazaar", "Dharkan" ); query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 1f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "intensity" ).boostedTo( 40f ).matching( 11 ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Dharkan", "Kazaar" ); }
@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 ); }