Query DSL: Range - support lt/lte/gt/gte, closes #114.

This commit is contained in:
kimchy 2010-04-04 22:14:45 +03:00
parent 33086fb98d
commit 5f7d0ce36e
4 changed files with 68 additions and 0 deletions

View File

@ -100,6 +100,27 @@ public class RangeJsonFilterParser extends AbstractIndexComponent implements Jso
} else {
includeUpper = token == JsonToken.VALUE_TRUE;
}
} else if ("gte".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
from = null;
} else {
from = jp.getText();
}
includeLower = true;
} else if ("lt".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
to = null;
} else {
to = jp.getText();
}
includeUpper = false;
} else if ("lte".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
to = null;
} else {
to = jp.getText();
}
includeUpper = true;
}
}
}

View File

@ -103,6 +103,34 @@ public class RangeJsonQueryParser extends AbstractIndexComponent implements Json
} else {
boost = jp.getFloatValue();
}
} else if ("gt".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
from = null;
} else {
from = jp.getText();
}
includeLower = false;
} else if ("gte".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
from = null;
} else {
from = jp.getText();
}
includeLower = true;
} else if ("lt".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
to = null;
} else {
to = jp.getText();
}
includeUpper = false;
} else if ("lte".equals(currentFieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
to = null;
} else {
to = jp.getText();
}
includeUpper = true;
}
}
}

View File

@ -379,6 +379,20 @@ public class SimpleJsonIndexQueryParserTests {
assertThat(rangeQuery.includesMax(), equalTo(false));
}
@Test public void testRange2Query() throws IOException {
IndexQueryParser queryParser = newQueryParser();
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/json/range2.json");
Query parsedQuery = queryParser.parse(query);
// since age is automatically registered in data, we encode it as numeric
assertThat(parsedQuery, instanceOf(NumericRangeQuery.class));
NumericRangeQuery rangeQuery = (NumericRangeQuery) parsedQuery;
assertThat(rangeQuery.getField(), equalTo("age"));
assertThat(rangeQuery.getMin().intValue(), equalTo(23));
assertThat(rangeQuery.getMax().intValue(), equalTo(54));
assertThat(rangeQuery.includesMin(), equalTo(true));
assertThat(rangeQuery.includesMax(), equalTo(false));
}
@Test public void testRangeFilteredQueryBuilder() throws IOException {
IndexQueryParser queryParser = newQueryParser();
Query parsedQuery = queryParser.parse(filtered(termQuery("name.first", "shay"), rangeFilter("age").from(23).to(54).includeLower(true).includeUpper(false)));

View File

@ -0,0 +1,5 @@
{
range : {
age : { gte : "23", lt : "54" }
}
}