diff --git a/src/main/java/org/elasticsearch/index/query/MultiTermQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/MultiTermQueryBuilder.java index 7ca07889087..87dda19c0c9 100644 --- a/src/main/java/org/elasticsearch/index/query/MultiTermQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/MultiTermQueryBuilder.java @@ -1,5 +1,26 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.elasticsearch.index.query; +/** + * + */ public interface MultiTermQueryBuilder extends QueryBuilder{ } diff --git a/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java index dc950e1e141..2394dc657ed 100644 --- a/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilder.java @@ -1,9 +1,30 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.elasticsearch.index.query; import java.io.IOException; import org.elasticsearch.common.xcontent.XContentBuilder; +/** + * + */ public class SpanMultiTermQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder{ private MultiTermQueryBuilder multiTermQueryBuilder; diff --git a/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryParser.java b/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryParser.java index bb4c5049a5f..c5e2518dd57 100644 --- a/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/SpanMultiTermQueryParser.java @@ -1,3 +1,21 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.elasticsearch.index.query; import java.io.IOException; @@ -11,6 +29,9 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser.Token; +/** + * + */ public class SpanMultiTermQueryParser implements QueryParser { public static final String NAME = "span_multi_term"; @@ -26,57 +47,25 @@ public class SpanMultiTermQueryParser implements QueryParser { } @Override - @Nullable - public Query parse(QueryParseContext parseContext) throws IOException, - QueryParsingException { - XContentParser parser = parseContext.parser(); - Token token = parser.nextToken(); - checkCorrectField(parseContext, parser, token); - token = parser.nextToken(); - checkHasObject(parseContext, parser.currentToken()); - Query ret = new SpanMultiTermQueryWrapper(getSubQuery( - parseContext, parser)); - parser.nextToken(); - return ret; - } + public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { + XContentParser parser = parseContext.parser(); + Token token = parser.nextToken(); + if (!MATCH_NAME.equals(parser.currentName()) || token != XContentParser.Token.FIELD_NAME) { + throw new QueryParsingException(parseContext.index(), "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause"); + } - private void checkCorrectField(QueryParseContext parseContext, - XContentParser parser, Token token) throws IOException { - if (!MATCH_NAME.equals(parser.currentName()) - || token != XContentParser.Token.FIELD_NAME) { - throwInvalidClause(parseContext); - } - } + token = parser.nextToken(); + if (token != XContentParser.Token.START_OBJECT) { + throw new QueryParsingException(parseContext.index(), "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause"); + } - private MultiTermQuery getSubQuery(QueryParseContext parseContext, - XContentParser parser) throws IOException { - return tryParseSubQuery(parseContext, parser); - } + Query subQuery = parseContext.parseInnerQuery(); + if (!(subQuery instanceof MultiTermQuery)) { + throw new QueryParsingException(parseContext.index(), "spanMultiTerm [" + MATCH_NAME + "] must be of type multi term query"); + } - private MultiTermQuery tryParseSubQuery(QueryParseContext parseContext, - XContentParser parser) throws IOException { - Query subQuery = parseContext.parseInnerQuery(); - if (!(subQuery instanceof MultiTermQuery)) { - throwInvalidSub(parseContext); - } - return (MultiTermQuery) subQuery; - } - - private void throwInvalidSub(QueryParseContext parseContext) { - throw new QueryParsingException(parseContext.index(), "spanMultiTerm [" - + MATCH_NAME + "] must be of type multi term query"); - } - - private void checkHasObject(QueryParseContext parseContext, Token token) { - if (token != XContentParser.Token.START_OBJECT) { - throwInvalidClause(parseContext); - } - } - - private void throwInvalidClause(QueryParseContext parseContext) { - throw new QueryParsingException(parseContext.index(), - "spanMultiTerm must have [" + MATCH_NAME - + "] multi term query clause"); - } + parser.nextToken(); + return new SpanMultiTermQueryWrapper((MultiTermQuery) subQuery); + } } diff --git a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java index 9c5ea92634b..e71b7f2c73c 100644 --- a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java +++ b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesModule.java @@ -103,6 +103,7 @@ public class IndicesQueriesModule extends AbstractModule { qpBinders.addBinding().to(IndicesQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(CommonTermsQueryParser.class).asEagerSingleton(); qpBinders.addBinding().to(SpanMultiTermQueryParser.class).asEagerSingleton(); + if (ShapesAvailability.JTS_AVAILABLE) { qpBinders.addBinding().to(GeoShapeQueryParser.class).asEagerSingleton(); } diff --git a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java index 192eda9a7b5..ada32595acd 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/query/SimpleIndexQueryParserTests.java @@ -1539,59 +1539,75 @@ public class SimpleIndexQueryParserTests { assertThat(((SpanTermQuery) spanOrQuery.getClauses()[2]).getTerm(), equalTo(new Term("age", longToPrefixCoded(36, 0)))); } - @Test - public void testSpanMultiTermWildcardQuery() throws IOException { - WildcardQuery expectedWrapped = new WildcardQuery(new Term("user", "ki*y")); - expectedWrapped.setBoost(1.08f); - testSpanMultiTerm("/org/elasticsearch/test/unit/index/query/span-multi-term-wildcard.json", expectedWrapped); - } - - @Test - public void testSpanMultiTermPrefixQuery() throws IOException { - PrefixQuery expectedWrapped = new PrefixQuery(new Term("user", "ki")); - expectedWrapped.setBoost(1.08f); - testSpanMultiTerm("/org/elasticsearch/test/unit/index/query/span-multi-term-prefix.json", expectedWrapped); - } - - @Test - public void testSpanMultiTermFuzzyTermQuery() throws IOException { - IndexQueryParserService queryParser = queryParser(); - String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-fuzzy-term.json"); - Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); - SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; - assertThat(wrapper.getField(), equalTo("user")); - } - - @Test - public void testSpanMultiTermFuzzyRangeQuery() throws IOException { - NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 7l, 17l, true, true); - expectedWrapped.setBoost(2.0f); - testSpanMultiTerm("/org/elasticsearch/test/unit/index/query/span-multi-term-fuzzy-range.json", expectedWrapped); - } - - @Test - public void testSpanMultiTermNumericRangeQuery() throws IOException { - NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false); - expectedWrapped.setBoost(2.0f); - testSpanMultiTerm("/org/elasticsearch/test/unit/index/query/span-multi-term-range-numeric.json", expectedWrapped); - } - @Test - - public void testSpanMultiTermTermRangeQuery() throws IOException { - NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false); - expectedWrapped.setBoost(2.0f); - testSpanMultiTerm("/org/elasticsearch/test/unit/index/query/span-multi-term-range-numeric.json", expectedWrapped); - } - - private void testSpanMultiTerm(String file, MultiTermQuery expectedWrapped) throws IOException{ - IndexQueryParserService queryParser = queryParser(); - String query = copyToStringFromClasspath(file); - Query parsedQuery = queryParser.parse(query).query(); - assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); - SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; - assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); - } + @Test + public void testSpanMultiTermWildcardQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-wildcard.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + WildcardQuery expectedWrapped = new WildcardQuery(new Term("user", "ki*y")); + expectedWrapped.setBoost(1.08f); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); + } + + @Test + public void testSpanMultiTermPrefixQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-prefix.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + PrefixQuery expectedWrapped = new PrefixQuery(new Term("user", "ki")); + expectedWrapped.setBoost(1.08f); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); + } + + @Test + public void testSpanMultiTermFuzzyTermQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-fuzzy-term.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper.getField(), equalTo("user")); + } + + @Test + public void testSpanMultiTermFuzzyRangeQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-fuzzy-range.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 7l, 17l, true, true); + expectedWrapped.setBoost(2.0f); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); + } + + @Test + public void testSpanMultiTermNumericRangeQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-range-numeric.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false); + expectedWrapped.setBoost(2.0f); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); + } + + @Test + public void testSpanMultiTermTermRangeQuery() throws IOException { + IndexQueryParserService queryParser = queryParser(); + String query = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/query/span-multi-term-range-numeric.json"); + Query parsedQuery = queryParser.parse(query).query(); + assertThat(parsedQuery, instanceOf(SpanMultiTermQueryWrapper.class)); + NumericRangeQuery expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false); + expectedWrapped.setBoost(2.0f); + SpanMultiTermQueryWrapper wrapper = (SpanMultiTermQueryWrapper) parsedQuery; + assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper(expectedWrapped))); + } @Test public void testQueryFilterBuilder() throws Exception {