RangeCondition 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 start = (String) mapper.base(field, lower); String stop = (String) mapper.base(field, upper); query = query(start, stop); } else if (clazz == Integer.class) { Integer start = (Integer) mapper.base(field, lower); Integer stop = (Integer) mapper.base(field, upper); query = query(start, stop); } else if (clazz == Long.class) { Long start = (Long) mapper.base(field, lower); Long stop = (Long) mapper.base(field, upper); query = query(start, stop); } else if (clazz == Float.class) { Float start = (Float) mapper.base(field, lower); Float stop = (Float) mapper.base(field, upper); query = query(start, stop); } else if (clazz == Double.class) { Double start = (Double) mapper.base(field, lower); Double stop = (Double) mapper.base(field, upper); query = query(start, stop); } else { throw new IndexException("Range queries are not supported by mapper '{}'", mapper); } return query; } RangeCondition(Float boost,
                          String field,
                          Object lowerValue,
                          Object upperValue,
                          Boolean includeLower,
                          Boolean includeUpper,
                          Boolean docValues); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); static final boolean DEFAULT_INCLUDE_LOWER; static final boolean DEFAULT_INCLUDE_UPPER; static final boolean DEFAULT_DOC_VALUES; final Object lower; final Object upper; final boolean includeLower; final boolean includeUpper; final boolean docValues; }
@Test public void testStringClose() { Schema schema = schema().mapper("name", stringMapper()).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", "beta", true, true, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermRangeQuery.class, query.getClass()); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertEquals("Query field is wrong", "name", termRangeQuery.getField()); assertEquals("Query lower is wrong", "alpha", termRangeQuery.getLowerTerm().utf8ToString()); assertEquals("Query upper is wrong", "beta", termRangeQuery.getUpperTerm().utf8ToString()); assertEquals("Query include lower is wrong", true, termRangeQuery.includesLower()); assertEquals("Query include upper is wrong", true, termRangeQuery.includesUpper()); }
@Test public void testStringOpen() { Schema schema = schema().mapper("name", stringMapper()).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", null, true, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermRangeQuery.class, query.getClass()); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertEquals("Query field is wrong", "name", termRangeQuery.getField()); assertEquals("Query lower is wrong", "alpha", termRangeQuery.getLowerTerm().utf8ToString()); assertEquals("Query upper is wrong", null, termRangeQuery.getUpperTerm()); assertEquals("Query include lower is wrong", true, termRangeQuery.includesLower()); assertEquals("Query include upper is wrong", false, termRangeQuery.includesUpper()); }
@Test public void testIntegerClose() { Schema schema = schema().mapper("name", integerMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42, 43, false, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", 43, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", false, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testIntegerOpen() { Schema schema = schema().mapper("name", integerMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42, null, true, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", null, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testLongClose() { Schema schema = schema().mapper("name", longMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42L, 43, false, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42L, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", 43L, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", false, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testLongOpen() { Schema schema = schema().mapper("name", longMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42f, null, true, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42L, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", null, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testFloatClose() { Schema schema = schema().mapper("name", floatMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42.42D, 43.42F, false, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42.42F, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", 43.42f, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", false, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testFloatOpen() { Schema schema = schema().mapper("name", floatMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42.42f, null, true, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42.42f, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", null, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testDoubleClose() { Schema schema = schema().mapper("name", doubleMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42.42D, 43.42D, false, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42.42D, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", 43.42D, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", false, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testDoubleOpen() { Schema schema = schema().mapper("name", doubleMapper().boost(1f)).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", 42.42D, null, true, false, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", NumericRangeQuery.class, query.getClass()); NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) query; assertEquals("Query field is wrong", "name", numericRangeQuery.getField()); assertEquals("Query lower is wrong", 42.42D, numericRangeQuery.getMin()); assertEquals("Query upper is wrong", null, numericRangeQuery.getMax()); assertEquals("Query include lower is wrong", true, numericRangeQuery.includesMin()); assertEquals("Query include upper is wrong", false, numericRangeQuery.includesMax()); }
@Test public void testInetV4() { Schema schema = schema().mapper("name", inetMapper()).build(); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "192.168.0.01", "192.168.0.045", true, true, false); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermRangeQuery.class, query.getClass()); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertEquals("Query field is wrong", "name", termRangeQuery.getField()); assertEquals("Query lower is wrong", "192.168.0.1", termRangeQuery.getLowerTerm().utf8ToString()); assertEquals("Query upper is wrong", "192.168.0.45", termRangeQuery.getUpperTerm().utf8ToString()); assertEquals("Query include lower is wrong", true, termRangeQuery.includesLower()); assertEquals("Query include upper is wrong", true, termRangeQuery.includesUpper()); }
@Test public void testInetV6() { Schema schema = schema().mapper("name", inetMapper()).build(); RangeCondition rangeCondition = range("name").boost(0.5f) .lower("2001:DB8:2de::e13") .upper("2001:DB8:02de::e23") .includeLower(true) .includeUpper(true) .build(); Query query = rangeCondition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", TermRangeQuery.class, query.getClass()); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertEquals("Query field is wrong", "name", termRangeQuery.getField()); assertEquals("Query lower is wrong", "2001:db8:2de:0:0:0:0:e13", termRangeQuery.getLowerTerm().utf8ToString()); assertEquals("Query upper is wrong", "2001:db8:2de:0:0:0:0:e23", termRangeQuery.getUpperTerm().utf8ToString()); assertEquals("Query include lower is wrong", true, termRangeQuery.includesLower()); assertEquals("Query include upper is wrong", true, termRangeQuery.includesUpper()); }