WildcardCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { if (mapper.base == String.class) { Term term = new Term(field, value); return new WildcardQuery(term); } else { throw new IndexException("Wildcard queries are not supported by mapper '{}'", mapper); } } WildcardCondition(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(); WildcardCondition wildcardCondition = new WildcardCondition(0.5f, "name", " "); Query query = wildcardCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Expected wildcard query", WildcardQuery.class, query.getClass()); WildcardQuery wildcardQuery = (WildcardQuery) query; assertEquals("Field name is not properly set", "name", wildcardQuery.getField()); assertEquals("Term text is not properly set", " ", wildcardQuery.getTerm().text()); }
@Test public void testStringValue() { Schema schema = schema().mapper("name", stringMapper()).build(); WildcardCondition wildcardCondition = new WildcardCondition(0.5f, "name", "tr*"); Query query = wildcardCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Expected wildcard query", WildcardQuery.class, query.getClass()); WildcardQuery wildcardQuery = (WildcardQuery) query; assertEquals("Field name is not properly set", "name", wildcardQuery.getField()); assertEquals("Term text is not properly set", "tr*", wildcardQuery.getTerm().text()); }
@Test public void testInetV4Value() { Schema schema = schema().mapper("name", inetMapper()).build(); WildcardCondition wildcardCondition = new WildcardCondition(0.5f, "name", "192.168.*"); Query query = wildcardCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Expected wildcard query", WildcardQuery.class, query.getClass()); WildcardQuery wildcardQuery = (WildcardQuery) query; assertEquals("Field name is not properly set", "name", wildcardQuery.getField()); assertEquals("Term text is not properly set", "192.168.*", wildcardQuery.getTerm().text()); }
@Test public void testInetV6Value() { Schema schema = schema().mapper("name", inetMapper()).build(); WildcardCondition condition = new WildcardCondition(0.5f, "name", "2001:db8:2de:0:0:0:0:e*"); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Expected wildcard query", WildcardQuery.class, query.getClass()); WildcardQuery wildcardQuery = (WildcardQuery) query; assertEquals("Field name is not properly set", "name", wildcardQuery.getField()); assertEquals("Term text is not properly set", "2001:db8:2de:0:0:0:0:e*", wildcardQuery.getTerm().text()); }