Skip to content

Commit 4d4dc5c

Browse files
committed
Refactors TypeQuery
Relates to #10217 Closes #12035 This PR is against the query-refactoring branch.
1 parent 37c6347 commit 4d4dc5c

File tree

4 files changed

+154
-41
lines changed

4 files changed

+154
-41
lines changed

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

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,44 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.lucene.index.Term;
23+
import org.apache.lucene.search.Query;
24+
import org.apache.lucene.search.TermQuery;
25+
import org.apache.lucene.util.BytesRef;
26+
import org.elasticsearch.common.io.stream.StreamInput;
27+
import org.elasticsearch.common.io.stream.StreamOutput;
28+
import org.elasticsearch.common.lucene.BytesRefs;
2229
import org.elasticsearch.common.xcontent.XContentBuilder;
30+
import org.elasticsearch.index.mapper.DocumentMapper;
31+
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
2332

2433
import java.io.IOException;
34+
import java.util.Objects;
2535

2636
public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
2737

2838
public static final String NAME = "type";
29-
private final String type;
30-
static final TypeQueryBuilder PROTOTYPE = new TypeQueryBuilder(null);
39+
40+
private final BytesRef type;
41+
42+
static final TypeQueryBuilder PROTOTYPE = new TypeQueryBuilder((BytesRef) null);
3143

3244
public TypeQueryBuilder(String type) {
45+
this.type = BytesRefs.toBytesRef(type);
46+
}
47+
48+
TypeQueryBuilder(BytesRef type) {
3349
this.type = type;
3450
}
51+
52+
public BytesRef type() {
53+
return this.type;
54+
}
3555

3656
@Override
3757
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
3858
builder.startObject(NAME);
39-
builder.field("value", type);
59+
builder.field("value", type.utf8ToString());
4060
printBoostAndQueryName(builder);
4161
builder.endObject();
4262
}
@@ -45,4 +65,46 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
4565
public String getName() {
4666
return NAME;
4767
}
68+
69+
@Override
70+
protected Query doToQuery(QueryParseContext parseContext) throws IOException {
71+
Query filter;
72+
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
73+
DocumentMapper documentMapper = parseContext.mapperService().documentMapper(type.utf8ToString());
74+
if (documentMapper == null) {
75+
filter = new TermQuery(new Term(TypeFieldMapper.NAME, type));
76+
} else {
77+
filter = documentMapper.typeFilter();
78+
}
79+
return filter;
80+
}
81+
82+
@Override
83+
public QueryValidationException validate() {
84+
QueryValidationException validationException = null;
85+
if (type == null) {
86+
validationException = addValidationError("[type] cannot be null", validationException);
87+
}
88+
return validationException;
89+
}
90+
91+
@Override
92+
protected TypeQueryBuilder doReadFrom(StreamInput in) throws IOException {
93+
return new TypeQueryBuilder(in.readBytesRef());
94+
}
95+
96+
@Override
97+
protected void doWriteTo(StreamOutput out) throws IOException {
98+
out.writeBytesRef(type);
99+
}
100+
101+
@Override
102+
protected int doHashCode() {
103+
return Objects.hash(type);
104+
}
105+
106+
@Override
107+
protected boolean doEquals(TypeQueryBuilder other) {
108+
return Objects.equals(type, other.type);
109+
}
48110
}

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@
1919

2020
package org.elasticsearch.index.query;
2121

22-
import org.apache.lucene.index.Term;
23-
import org.apache.lucene.search.Query;
24-
import org.apache.lucene.search.TermQuery;
2522
import org.apache.lucene.util.BytesRef;
2623
import org.elasticsearch.common.inject.Inject;
2724
import org.elasticsearch.common.xcontent.XContentParser;
28-
import org.elasticsearch.index.mapper.DocumentMapper;
29-
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
3025

3126
import java.io.IOException;
3227

33-
public class TypeQueryParser extends BaseQueryParserTemp {
28+
public class TypeQueryParser extends BaseQueryParser {
3429

3530
@Inject
3631
public TypeQueryParser() {
@@ -42,11 +37,13 @@ public String[] names() {
4237
}
4338

4439
@Override
45-
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
40+
public QueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, QueryParsingException {
4641
XContentParser parser = parseContext.parser();
42+
BytesRef type = null;
43+
4744
String queryName = null;
4845
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
49-
BytesRef type = null;
46+
5047
String currentFieldName = null;
5148
XContentParser.Token token;
5249
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@@ -68,22 +65,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
6865
if (type == null) {
6966
throw new QueryParsingException(parseContext, "[type] filter needs to be provided with a value for the type");
7067
}
71-
72-
Query filter;
73-
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
74-
DocumentMapper documentMapper = parseContext.mapperService().documentMapper(type.utf8ToString());
75-
if (documentMapper == null) {
76-
filter = new TermQuery(new Term(TypeFieldMapper.NAME, type));
77-
} else {
78-
filter = documentMapper.typeFilter();
79-
}
80-
if (queryName != null) {
81-
parseContext.addNamedQuery(queryName, filter);
82-
}
83-
if (filter != null) {
84-
filter.setBoost(boost);
85-
}
86-
return filter;
68+
return new TypeQueryBuilder(type)
69+
.boost(boost)
70+
.queryName(queryName);
8771
}
8872

8973
@Override

core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,7 @@ public static void afterClass() throws Exception {
160160
@Before
161161
public void beforeTest() {
162162
//set some random types to be queried as part the search request, before each test
163-
String[] types;
164-
if (currentTypes.length > 0 && randomBoolean()) {
165-
int numberOfQueryTypes = randomIntBetween(1, currentTypes.length);
166-
types = new String[numberOfQueryTypes];
167-
for (int i = 0; i < numberOfQueryTypes; i++) {
168-
types[i] = randomFrom(currentTypes);
169-
}
170-
} else {
171-
if (randomBoolean()) {
172-
types = new String[] { MetaData.ALL };
173-
} else {
174-
types = new String[0];
175-
}
176-
}
163+
String[] types = getRandomTypes();
177164
//some query (e.g. range query) have a different behaviour depending on whether the current search context is set or not
178165
//which is why we randomly set the search context, which will internally also do QueryParseContext.setTypes(types)
179166
if (randomBoolean()) {
@@ -348,4 +335,26 @@ protected static String getRandomRewriteMethod() {
348335
}
349336
return rewrite;
350337
}
338+
339+
protected String[] getRandomTypes() {
340+
String[] types;
341+
if (currentTypes.length > 0 && randomBoolean()) {
342+
int numberOfQueryTypes = randomIntBetween(1, currentTypes.length);
343+
types = new String[numberOfQueryTypes];
344+
for (int i = 0; i < numberOfQueryTypes; i++) {
345+
types[i] = randomFrom(currentTypes);
346+
}
347+
} else {
348+
if (randomBoolean()) {
349+
types = new String[] { MetaData.ALL };
350+
} else {
351+
types = new String[0];
352+
}
353+
}
354+
return types;
355+
}
356+
357+
protected String getRandomType() {
358+
return (currentTypes.length == 0) ? MetaData.ALL : randomFrom(currentTypes);
359+
}
351360
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.index.Term;
23+
import org.apache.lucene.search.Query;
24+
import org.apache.lucene.search.TermQuery;
25+
import org.elasticsearch.index.mapper.DocumentMapper;
26+
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
27+
import org.junit.Test;
28+
29+
import java.io.IOException;
30+
31+
import static org.hamcrest.Matchers.is;
32+
33+
public class TypeQueryBuilderTest extends BaseQueryTestCase<TypeQueryBuilder> {
34+
35+
@Override
36+
protected TypeQueryBuilder doCreateTestQueryBuilder() {
37+
return new TypeQueryBuilder(getRandomType());
38+
}
39+
40+
@Override
41+
protected Query doCreateExpectedQuery(TypeQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
42+
Query expectedQuery;
43+
//LUCENE 4 UPGRADE document mapper should use bytesref as well?
44+
DocumentMapper documentMapper = context.mapperService().documentMapper(queryBuilder.type().utf8ToString());
45+
if (documentMapper == null) {
46+
expectedQuery = new TermQuery(new Term(TypeFieldMapper.NAME, queryBuilder.type()));
47+
} else {
48+
expectedQuery = documentMapper.typeFilter();
49+
}
50+
return expectedQuery;
51+
}
52+
53+
@Test
54+
public void testValidate() {
55+
TypeQueryBuilder typeQueryBuilder = new TypeQueryBuilder((String) null);
56+
assertThat(typeQueryBuilder.validate().validationErrors().size(), is(1));
57+
}
58+
}

0 commit comments

Comments
 (0)