Skip to content

Commit

Permalink
Query refactoring: refactored LimitQueryBuilder and Parser and added …
Browse files Browse the repository at this point in the history
…test

Split the parse method into a parsing and a query building part, adding serialization
and hashCode(), equals() for better testing. Add basic unit test for Builder and Parser.

Closes elastic#11551
  • Loading branch information
cbuescher committed Jun 9, 2015
1 parent 536492a commit ba64233
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package org.elasticsearch.index.query;

import org.apache.lucene.search.Query;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -28,7 +32,7 @@
* @deprecated Use {@link SearchRequestBuilder#setTerminateAfter(int)} instead.
*/
@Deprecated
public class LimitQueryBuilder extends QueryBuilder {
public class LimitQueryBuilder extends QueryBuilder<LimitQueryBuilder> {

public static final String NAME = "limit";
private final int limit;
Expand All @@ -45,8 +49,42 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.endObject();
}

@Override
public Query toQuery(QueryParseContext parseContext) {
// this filter is deprecated and parses to a filter that matches everything
return Queries.newMatchAllQuery();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LimitQueryBuilder that = (LimitQueryBuilder) o;
return Integer.compare(that.limit, limit) == 0;
}

@Override
public int hashCode() {
return Integer.hashCode(limit);
}

@Override
public LimitQueryBuilder readFrom(StreamInput in) throws IOException {
LimitQueryBuilder limitQueryBuilder = new LimitQueryBuilder(in.readInt());
return limitQueryBuilder;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeInt(this.limit);
}

@Override
public String queryId() {
return NAME;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@

package org.elasticsearch.index.query;

import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;

@Deprecated
public class LimitQueryParser extends BaseQueryParserTemp {
public class LimitQueryParser extends BaseQueryParser {

@Inject
public LimitQueryParser() {
Expand All @@ -39,7 +37,7 @@ public String[] names() {
}

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();

int limit = -1;
Expand All @@ -61,8 +59,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
throw new QueryParsingException(parseContext, "No value specified for limit query");
}

// this filter is deprecated and parses to a filter that matches everything
return Queries.newMatchAllQuery();
return new LimitQueryBuilder(limit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to Elasticsearch 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 org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.search.Queries;

public class LimitQueryBuilderTest extends BaseQueryTestCase<LimitQueryBuilder> {

@Override
protected Query createExpectedQuery(LimitQueryBuilder queryBuilder, QueryParseContext context) {
// this filter is deprecated and parses to a filter that matches everything
return Queries.newMatchAllQuery();
}

@Override
protected LimitQueryBuilder createEmptyQueryBuilder() {
return new LimitQueryBuilder(0);
}

/**
* @return a LimitQueryBuilder with random limit between 0 and 20
*/
@Override
protected LimitQueryBuilder createTestQueryBuilder() {
LimitQueryBuilder query = new LimitQueryBuilder(randomIntBetween(0, 20));
return query;
}

}

0 comments on commit ba64233

Please sign in to comment.