Skip to content

Commit aadfd17

Browse files
committed
Query refactoring: refactored LimitQueryBuilder and Parser and added 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 #11551
1 parent 42acee3 commit aadfd17

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

core/src/main/java/org/elasticsearch/index/query/LimitQueryBuilder.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.search.Query;
2223
import org.elasticsearch.action.search.SearchRequestBuilder;
24+
import org.elasticsearch.common.io.stream.StreamInput;
25+
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.common.lucene.search.Queries;
2327
import org.elasticsearch.common.xcontent.XContentBuilder;
2428

2529
import java.io.IOException;
@@ -28,7 +32,7 @@
2832
* @deprecated Use {@link SearchRequestBuilder#setTerminateAfter(int)} instead.
2933
*/
3034
@Deprecated
31-
public class LimitQueryBuilder extends QueryBuilder {
35+
public class LimitQueryBuilder extends QueryBuilder<LimitQueryBuilder> {
3236

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

52+
@Override
53+
public Query toQuery(QueryParseContext parseContext) {
54+
// this filter is deprecated and parses to a filter that matches everything
55+
return Queries.newMatchAllQuery();
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (this == o) {
61+
return true;
62+
}
63+
if (o == null || getClass() != o.getClass()) {
64+
return false;
65+
}
66+
LimitQueryBuilder that = (LimitQueryBuilder) o;
67+
return Integer.compare(that.limit, limit) == 0;
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Integer.hashCode(limit);
73+
}
74+
75+
@Override
76+
public LimitQueryBuilder readFrom(StreamInput in) throws IOException {
77+
LimitQueryBuilder limitQueryBuilder = new LimitQueryBuilder(in.readInt());
78+
return limitQueryBuilder;
79+
}
80+
81+
@Override
82+
public void writeTo(StreamOutput out) throws IOException {
83+
out.writeInt(this.limit);
84+
}
85+
4886
@Override
4987
public String queryId() {
5088
return NAME;
5189
}
52-
}
90+
}

core/src/main/java/org/elasticsearch/index/query/LimitQueryParser.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
package org.elasticsearch.index.query;
2121

22-
import org.apache.lucene.search.Query;
2322
import org.elasticsearch.common.inject.Inject;
24-
import org.elasticsearch.common.lucene.search.Queries;
2523
import org.elasticsearch.common.xcontent.XContentParser;
2624

2725
import java.io.IOException;
2826

2927
@Deprecated
30-
public class LimitQueryParser extends BaseQueryParserTemp {
28+
public class LimitQueryParser extends BaseQueryParser {
3129

3230
@Inject
3331
public LimitQueryParser() {
@@ -39,7 +37,7 @@ public String[] names() {
3937
}
4038

4139
@Override
42-
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
40+
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
4341
XContentParser parser = parseContext.parser();
4442

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

64-
// this filter is deprecated and parses to a filter that matches everything
65-
return Queries.newMatchAllQuery();
62+
return new LimitQueryBuilder(limit);
6663
}
6764

6865
@Override
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.query;
21+
22+
import org.apache.lucene.search.Query;
23+
import org.elasticsearch.common.lucene.search.Queries;
24+
25+
public class LimitQueryBuilderTest extends BaseQueryTestCase<LimitQueryBuilder> {
26+
27+
@Override
28+
protected Query createExpectedQuery(LimitQueryBuilder queryBuilder, QueryParseContext context) {
29+
// this filter is deprecated and parses to a filter that matches everything
30+
return Queries.newMatchAllQuery();
31+
}
32+
33+
@Override
34+
protected LimitQueryBuilder createEmptyQueryBuilder() {
35+
return new LimitQueryBuilder(0);
36+
}
37+
38+
/**
39+
* @return a LimitQueryBuilder with random limit between 0 and 20
40+
*/
41+
@Override
42+
protected LimitQueryBuilder createTestQueryBuilder() {
43+
LimitQueryBuilder query = new LimitQueryBuilder(randomIntBetween(0, 20));
44+
return query;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)