RegexpCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { if (mapper.base == String.class) { Term term = new Term(field, value); return new RegexpQuery(term); } else { throw new IndexException("Regexp queries are not supported by mapper '{}'", mapper); } } RegexpCondition(Float boost, String field, String value); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); final String value; }
@Test public void testBlankValue() { Schema schema = schema().mapper("name", stringMapper()).build(); RegexpCondition condition = new RegexpCondition(0.5f, "name", " "); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", RegexpQuery.class, query.getClass()); RegexpQuery regexQuery = (RegexpQuery) query; assertEquals("Query field is wrong", "name", regexQuery.getField()); }
@Test public void testString() { Schema schema = schema().mapper("name", stringMapper()).build(); RegexpCondition condition = new RegexpCondition(0.5f, "name", "tr*"); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", RegexpQuery.class, query.getClass()); RegexpQuery regexQuery = (RegexpQuery) query; assertEquals("Query field is wrong", "name", regexQuery.getField()); }
@Test public void testInetV4() { Schema schema = schema().mapper("name", inetMapper()).build(); RegexpCondition condition = new RegexpCondition(0.5f, "name", "192.168.*"); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", RegexpQuery.class, query.getClass()); RegexpQuery regexQuery = (RegexpQuery) query; assertEquals("Query field is wrong", "name", regexQuery.getField()); }
@Test public void testInetV6() { Schema schema = schema().mapper("name", inetMapper()).build(); RegexpCondition regexpCondition = new RegexpCondition(0.5f, "name", "2001:db8:2de:0:0:0:0:e*"); Query query = regexpCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", RegexpQuery.class, query.getClass()); RegexpQuery regexQuery = (RegexpQuery) query; assertEquals("Query field is wrong", "name", regexQuery.getField()); }