MatchCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { if (docValues && !mapper.docValues) { throw new IndexException("Field '{}' does not support doc_values", mapper.field); } Class<?> clazz = mapper.base; Query query; if (clazz == String.class) { String base = (String) mapper.base(field, value); if (mapper instanceof TextMapper) { QueryBuilder queryBuilder = new QueryBuilder(analyzer); query = queryBuilder.createPhraseQuery(field, base, 0); } else { query = query(base); } if (query == null) { query = new BooleanQuery.Builder().build(); } } else if (clazz == Integer.class) { query = query((Integer) mapper.base(field, value)); } else if (clazz == Long.class) { query = query((Long) mapper.base(field, value)); } else if (clazz == Float.class) { query = query((Float) mapper.base(field, value)); } else if (clazz == Double.class) { query = query((Double) mapper.base(field, value)); } else { throw new IndexException("Match queries are not supported by mapper '{}'", mapper); } return query; } MatchCondition(Float boost, String field, Object value, Boolean docValues); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); static final boolean DEFAULT_DOC_VALUES; final Object value; final boolean docValues; }
@Test public void testString() { Schema schema = schema().mapper("name", stringMapper()).build(); MatchCondition matchCondition = new MatchCondition(0.5f, "name", "value", false); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); TermQuery termQuery = (TermQuery) query; assertEquals("Query value is wrong", "value", termQuery.getTerm().bytes().utf8ToString()); }
@Test public void testStringStopwords() { Schema schema = schema().mapper("name", textMapper().analyzer("english")).build(); MatchCondition matchCondition = new MatchCondition(0.5f, "name", "the", false); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); }
@Test public void testInteger() { Schema schema = schema().mapper("name", integerMapper()).build(); MatchCondition matchCondition = match("name", 42).boost(0.5f).build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); Term term = ((TermQuery) query).getTerm(); assertEquals("Query value is wrong", "60080000002a", ByteBufferUtils.toHex(term.bytes())); }
@Test public void testLong() { Schema schema = schema().mapper("name", longMapper()).build(); MatchCondition matchCondition = match("name", 42L).build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); Term term = ((TermQuery) query).getTerm(); assertEquals("Query value is wrong", "200100000000000000002a", ByteBufferUtils.toHex(term.bytes())); }
@Test public void testFloat() { Schema schema = schema().mapper("name", floatMapper()).build(); MatchCondition matchCondition = match("name", 42.42F).build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query value is wrong", 42.42F, numericRangeQuery.getMin()); assertEquals("Query value is wrong", 42.42F, numericRangeQuery.getMax()); assertEquals("Query value is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query value is wrong", true, numericRangeQuery.includesMax()); }
@Test public void testDouble() { Schema schema = schema().mapper("name", doubleMapper()).build(); MatchCondition matchCondition = match("name", 42.42D).build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query value is wrong", 42.42D, numericRangeQuery.getMin()); assertEquals("Query value is wrong", 42.42D, numericRangeQuery.getMax()); assertEquals("Query value is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query value is wrong", true, numericRangeQuery.includesMax()); }
@Test public void testBlob() { Schema schema = schema().mapper("name", blobMapper()).build(); MatchCondition matchCondition = match("name", "0Fa1").build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); TermQuery termQuery = (TermQuery) query; assertEquals("Query value is wrong", "0fa1", termQuery.getTerm().bytes().utf8ToString()); }
@Test public void testInetV4() { Schema schema = schema().mapper("name", inetMapper()).build(); MatchCondition matchCondition = match("name", "192.168.0.01").build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); TermQuery termQuery = (TermQuery) query; assertEquals("Query value is wrong", "192.168.0.1", termQuery.getTerm().bytes().utf8ToString()); }
@Test public void testInetV6() { Schema schema = schema().mapper("name", inetMapper()).build(); MatchCondition matchCondition = match("name", "2001:DB8:2de::0e13").build(); Query query = matchCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermQuery.class, query.getClass()); TermQuery termQuery = (TermQuery) query; assertEquals("Query value is wrong", "2001:db8:2de:0:0:0:0:e13", termQuery.getTerm().bytes().utf8ToString()); }
@Test(expected = IndexException.class) public void testUnsupportedMapper() { final MockedMapper mapper = new MockedMapper(); Schema schema = schema().mapper("field", new MockedMapperBuilder(mapper)).build(); MatchCondition matchCondition = match("field", "2001:DB8:2de::0e13").build(); matchCondition.doQuery(schema); }