BooleanQueryConverter implements QueryConverter { @Override public String convert() { Iterator<BooleanClause> clauses = this.booleanQuery.clauses().iterator(); StringBuilder queryString = new StringBuilder(); while (clauses.hasNext()) { BooleanClause clause = clauses.next(); String queryResult = icklConverter.convert(clause.getQuery()); if (BooleanQuery.class.isAssignableFrom(clause.getQuery().getClass())) { queryResult = "(" + queryResult + ")"; } if (clause.isProhibited()) { queryResult = "NOT " + queryResult; } if (clauses.hasNext()) { String operator = this.getBooleanOperator(clause); queryResult = queryResult + " " + operator + " "; } queryString.append(queryResult); } return queryString.toString().trim(); } BooleanQueryConverter(BooleanQuery booleanQuery,
                                 IckleConverter icklConverter); @Override String convert();  }
@Test public void testSingleBooleanTermQuery() { TermQuery query = new TermQuery(new Term("a.term", "theResult")); BooleanQuery booleanQuery = new BooleanQuery.Builder() .add(query, BooleanClause.Occur.MUST) .build(); BooleanQueryConverter converter = new BooleanQueryConverter(booleanQuery, new IckleConverter()); String queryString = converter.convert(); assertThat(queryString).isEqualTo("a__term:'theResult'"); }
@Test public void testSingleClauseBooleanTermQuery() { TermQuery query = new TermQuery(new Term("a.term", "theResult")); BooleanQuery booleanQuery = new BooleanQuery.Builder() .add(query, BooleanClause.Occur.MUST) .build(); BooleanQueryConverter converter = new BooleanQueryConverter(booleanQuery, new IckleConverter()); String queryString = converter.convert(); assertThat(queryString).isEqualTo("a__term:'theResult'"); }
@Test public void testNotBooleanQuery() { TermQuery query = new TermQuery(new Term("a.term", "theResult")); BooleanQuery booleanQuery = new BooleanQuery.Builder() .add(query, BooleanClause.Occur.MUST_NOT) .build(); BooleanQueryConverter converter = new BooleanQueryConverter(booleanQuery, new IckleConverter()); String queryString = converter.convert(); assertThat(queryString).isEqualTo("NOT a__term:'theResult'"); }
@Test public void testMultipleBooleanTermQuery() { TermQuery query = new TermQuery(new Term("a.term", "theResult")); TermQuery query2 = new TermQuery(new Term("a.term.other", "123")); BooleanQuery orBooleanQuery = new BooleanQuery.Builder() .add(query, BooleanClause.Occur.SHOULD) .add(query2, BooleanClause.Occur.MUST_NOT).build(); BooleanQuery booleanQuery = new BooleanQuery.Builder() .add(query, BooleanClause.Occur.MUST) .add(query2, BooleanClause.Occur.MUST) .add(orBooleanQuery, BooleanClause.Occur.MUST) .build(); BooleanQueryConverter converter = new BooleanQueryConverter(booleanQuery, new IckleConverter()); String queryString = converter.convert(); assertThat(queryString).isEqualTo("a__term:'theResult' AND a__term__other:123 AND (a__term:'theResult' OR NOT a__term__other:123)"); }