From 5f7d0ce36e9bad1592e6b04f465c201a6a272c0a Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 4 Apr 2010 22:14:45 +0300 Subject: [PATCH] Query DSL: Range - support lt/lte/gt/gte, closes #114. --- .../query/json/RangeJsonFilterParser.java | 21 ++++++++++++++ .../query/json/RangeJsonQueryParser.java | 28 +++++++++++++++++++ .../json/SimpleJsonIndexQueryParserTests.java | 14 ++++++++++ .../index/query/json/range2.json | 5 ++++ 4 files changed, 68 insertions(+) create mode 100644 modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/range2.json diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java index dd4da71adc9..c0573a2e1f7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonFilterParser.java @@ -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; } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java index 71eef18986a..a225d364b3c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/RangeJsonQueryParser.java @@ -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; } } } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java index 4aa9bcdf5e2..35eb2bdec6e 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/SimpleJsonIndexQueryParserTests.java @@ -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))); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/range2.json b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/range2.json new file mode 100644 index 00000000000..ddb08a509c1 --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/json/range2.json @@ -0,0 +1,5 @@ +{ + range : { + age : { gte : "23", lt : "54" } + } +} \ No newline at end of file