LuceneCondition extends Condition { @Override public Query doQuery(Schema schema) { try { Analyzer analyzer = schema.analyzer; QueryParser queryParser = new QueryParser(defaultField, analyzer); queryParser.setAllowLeadingWildcard(true); queryParser.setLowercaseExpandedTerms(false); return queryParser.parse(query); } catch (ParseException e) { throw new IndexException("Error while parsing lucene syntax query: {}", e.getMessage()); } } LuceneCondition(Float boost, String defaultField, String query); Set<String> postProcessingFields(); @Override Query doQuery(Schema schema); @Override MoreObjects.ToStringHelper toStringHelper(); static final String DEFAULT_FIELD; final String query; final String defaultField; }
@Test public void testQuery() { Schema schema = schema().defaultAnalyzer("english").build(); LuceneCondition condition = new LuceneCondition(0.7f, "field_1", "field_2:houses"); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); TermQuery termQuery = (TermQuery) query; assertEquals("Query term is wrong", "hous", termQuery.getTerm().bytes().utf8ToString()); }