PhraseCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { if (mapper.base == String.class) { QueryBuilder queryBuilder = new QueryBuilder(analyzer); Query query = queryBuilder.createPhraseQuery(field, value, slop); if (query == null) { query = new BooleanQuery.Builder().build(); } return query; } else { throw new IndexException("Phrase queries are not supported by mapper '{}'", mapper); } } PhraseCondition(Float boost, String field, String value, Integer slop); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); static final int DEFAULT_SLOP; final String value; final int slop; }
@Test public void testPhraseQuery() { Schema schema = schema().mapper("name", textMapper().analyzer("spanish")).build(); String value = "hola adios the a"; PhraseCondition condition = new PhraseCondition(0.5f, "name", value, 2); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", PhraseQuery.class, query.getClass()); PhraseQuery luceneQuery = (PhraseQuery) query; assertEquals("Query terms are wrong", 3, luceneQuery.getTerms().length); assertEquals("Query slop is wrong", 2, luceneQuery.getSlop()); }