Refactoring SpanMultiTermQuery support

* Added license headers where needed
* Refactored SpanMultiTermQueryParser
* Refactored tests to adhere to other tests
This commit is contained in:
Alexander Reelsen 2013-05-03 15:58:03 +02:00
parent e30aa6b221
commit 70355f693f
5 changed files with 150 additions and 102 deletions

View File

@ -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{
}

View File

@ -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;

View File

@ -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<MultiTermQuery>(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>((MultiTermQuery) subQuery);
}
}

View File

@ -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();
}

View File

@ -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<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper.getField(), equalTo("user"));
}
@Test
public void testSpanMultiTermFuzzyRangeQuery() throws IOException {
NumericRangeQuery<Long> 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<Long> 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<Long> 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<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(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<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(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<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(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<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) 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<Long> expectedWrapped = NumericRangeQuery.newLongRange("age", 7l, 17l, true, true);
expectedWrapped.setBoost(2.0f);
SpanMultiTermQueryWrapper<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(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<Long> expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false);
expectedWrapped.setBoost(2.0f);
SpanMultiTermQueryWrapper<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(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<Long> expectedWrapped = NumericRangeQuery.newLongRange("age", 10l, 20l, true, false);
expectedWrapped.setBoost(2.0f);
SpanMultiTermQueryWrapper<MultiTermQuery> wrapper = (SpanMultiTermQueryWrapper<MultiTermQuery>) parsedQuery;
assertThat(wrapper, equalTo(new SpanMultiTermQueryWrapper<MultiTermQuery>(expectedWrapped)));
}
@Test
public void testQueryFilterBuilder() throws Exception {