BooleanCondition extends Condition { @Override public synchronized BooleanQuery doQuery(Schema schema) { int oldMaxClauses= BooleanQuery.getMaxClauseCount(); BooleanQuery.setMaxClauseCount(maxClauses); BooleanQuery.Builder builder = new BooleanQuery.Builder(); must.forEach(condition -> builder.add(condition.query(schema), MUST)); should.forEach(condition -> builder.add(condition.query(schema), SHOULD)); not.forEach(condition -> builder.add(condition.query(schema), MUST_NOT)); if (must.isEmpty() && should.isEmpty() && !not.isEmpty()) { logger.warn("Performing resource-intensive pure negation query {}", this); builder.add(new MatchAllDocsQuery(), FILTER); } BooleanQuery out=builder.build(); BooleanQuery.setMaxClauseCount(oldMaxClauses); return out; } BooleanCondition(Float boost,
                            List<Condition> must,
                            List<Condition> should,
                            List<Condition> not,
                            Integer maxClauses); @Override synchronized BooleanQuery doQuery(Schema schema); Set<String> postProcessingFields(); @Override MoreObjects.ToStringHelper toStringHelper(); final List<Condition> must; final List<Condition> should; final List<Condition> not; }
@Test public void testQuery() { Schema schema = schema().mapper("name", stringMapper()) .mapper("color", stringMapper()) .mapper("country", stringMapper()) .mapper("age", integerMapper()) .defaultAnalyzer("default") .build(); BooleanCondition condition = bool().must(match("name", "jonathan"), range("age").lower(18).includeLower(true)) .should(match("color", "green"), match("color", "blue")) .not(match("country", "england")) .boost(0.4f) .build(); BooleanQuery query = condition.doQuery(schema); assertEquals("Query count clauses is wrong", 5, query.clauses().size()); }
@Test public void testQueryEmpty() { Schema schema = schema().build(); BooleanCondition condition = bool().boost(0.4).build(); BooleanQuery query = condition.doQuery(schema); assertEquals("Query count clauses is wrong", 0, query.clauses().size()); }
@Test public void testQueryPureNot() { Schema schema = schema().mapper("name", stringMapper()).build(); BooleanCondition condition = bool().not(match("name", "jonathan")).boost(0.4).build(); BooleanQuery query = condition.doQuery(schema); assertEquals("Query count clauses is wrong", 2, query.clauses().size()); }
@Test public void testOverMaxClauses() { Schema schema = schema().mapper("name1", stringMapper()) .mapper("name2", stringMapper()) .mapper("name3", stringMapper()) .mapper("name4", stringMapper()) .mapper("name5", stringMapper()) .mapper("name6", stringMapper()).build(); BooleanCondition condition = bool().maxClauses(5) .must(match("name1","value1")) .must(match("name2","value2")) .must(match("name3","value3")) .must(match("name4","value4")) .must(match("name5","value5")) .must(match("name6","value6")).build(); try { condition.doQuery(schema); } catch (org.apache.lucene.search.BooleanQuery.TooManyClauses e) { assertTrue(true); return; } fail("Creating a booleanQuery with more clauses than limited should throw an Exception"); }
@Test public void testInMaxClauses() { Schema schema = schema().mapper("name1", stringMapper()) .mapper("name2", stringMapper()) .mapper("name3", stringMapper()) .mapper("name4", stringMapper()) .mapper("name5", stringMapper()) .mapper("name6", stringMapper()).build(); BooleanCondition condition = bool().maxClauses(10) .must(match("name1","value1")) .must(match("name2","value2")) .must(match("name3","value3")) .must(match("name4","value4")) .must(match("name5","value5")) .must(match("name6","value6")).build(); assertEquals("Query count clauses is wrong", 6, condition.doQuery(schema).clauses().size()); }