PrefixCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { if (mapper.base == String.class) { Term term = new Term(field, value); return new PrefixQuery(term); } else { throw new IndexException("Prefix queries are not supported by mapper '{}'", mapper); } } PrefixCondition(Float boost, String field, String value); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); final String value; }
@Test public void testStringValue() { Schema schema = schema().mapper("name", stringMapper()).build(); PrefixCondition prefixCondition = new PrefixCondition(0.5f, "name", "tr"); Query query = prefixCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", PrefixQuery.class, query.getClass()); PrefixQuery luceneQuery = (PrefixQuery) query; assertEquals("Query field is wrong", "name", luceneQuery.getField()); assertEquals("Query prefix is wrong", "tr", luceneQuery.getPrefix().text()); }
@Test public void testInetV4Value() { Schema schema = schema().mapper("name", inetMapper()).build(); PrefixCondition wildcardCondition = new PrefixCondition(0.5f, "name", "192.168."); Query query = wildcardCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", PrefixQuery.class, query.getClass()); PrefixQuery luceneQuery = (PrefixQuery) query; assertEquals("Query field is wrong", "name", luceneQuery.getField()); assertEquals("Query prefix is wrong", "192.168.", luceneQuery.getPrefix().text()); }
@Test public void testInetV6Value() { Schema schema = schema().mapper("name", inetMapper()).build(); PrefixCondition wildcardCondition = new PrefixCondition(0.5f, "name", "2001:db8:2de:0:0:0:0:e"); Query query = wildcardCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", PrefixQuery.class, query.getClass()); PrefixQuery luceneQuery = (PrefixQuery) query; assertEquals("Query field is wrong", "name", luceneQuery.getField()); assertEquals("Query prefix is wrong", "2001:db8:2de:0:0:0:0:e", luceneQuery.getPrefix().text()); }