Skip to content

Commit 1107df9

Browse files
committed
query parser should take into account using all, since all creates a specialized "term query" which boosts based on the boost level associated with a term. Generalized it so mappers can control when query is used for term query.
1 parent a8be04b commit 1107df9

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.analysis.Analyzer;
2323
import org.apache.lucene.document.Field;
2424
import org.apache.lucene.document.Fieldable;
25+
import org.apache.lucene.index.Term;
2526
import org.apache.lucene.search.Filter;
2627
import org.apache.lucene.search.Query;
2728
import org.apache.lucene.util.StringHelper;
@@ -143,8 +144,16 @@ public String fullName() {
143144
*/
144145
boolean useFieldQueryWithQueryString();
145146

147+
/**
148+
* A field query for the specified value.
149+
*/
146150
Query fieldQuery(String value);
147151

152+
/**
153+
* A term query to use when parsing a query string. Can return <tt>null</tt>.
154+
*/
155+
Query queryStringTermQuery(Term term);
156+
148157
Filter fieldFilter(String value);
149158

150159
/**

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonAllFieldMapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
import org.apache.lucene.analysis.TokenStream;
2424
import org.apache.lucene.document.Field;
2525
import org.apache.lucene.document.Fieldable;
26+
import org.apache.lucene.index.Term;
27+
import org.apache.lucene.search.Query;
2628
import org.elasticsearch.index.analysis.NamedAnalyzer;
2729
import org.elasticsearch.index.mapper.AllFieldMapper;
2830
import org.elasticsearch.index.mapper.DocumentMapper;
2931
import org.elasticsearch.index.mapper.MergeMappingException;
3032
import org.elasticsearch.util.json.JsonBuilder;
3133
import org.elasticsearch.util.lucene.Lucene;
3234
import org.elasticsearch.util.lucene.all.AllAnalyzer;
35+
import org.elasticsearch.util.lucene.all.AllTermQuery;
3336

3437
import java.io.IOException;
3538

@@ -106,6 +109,10 @@ public boolean enabled() {
106109
return this.enabled;
107110
}
108111

112+
@Override public Query queryStringTermQuery(Term term) {
113+
return new AllTermQuery(term);
114+
}
115+
109116
@Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException {
110117
if (!enabled) {
111118
return null;

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ protected JsonFieldMapper(Names names, Field.Index index, Field.Store store, Fie
308308
return new TermQuery(new Term(names.indexName(), indexedValue(value)));
309309
}
310310

311+
@Override public Query queryStringTermQuery(Term term) {
312+
return null;
313+
}
314+
311315
@Override public Filter fieldFilter(String value) {
312316
return new TermFilter(new Term(names.indexName(), indexedValue(value)));
313317
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/MapperQueryParser.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index.query.support;
2121

2222
import org.apache.lucene.analysis.Analyzer;
23+
import org.apache.lucene.index.Term;
2324
import org.apache.lucene.queryParser.ParseException;
2425
import org.apache.lucene.queryParser.QueryParser;
2526
import org.apache.lucene.search.BooleanClause;
@@ -52,6 +53,8 @@ public class MapperQueryParser extends QueryParser {
5253

5354
private final FilterCache filterCache;
5455

56+
private FieldMapper currentMapper;
57+
5558
public MapperQueryParser(String defaultField, Analyzer analyzer,
5659
@Nullable MapperService mapperService,
5760
@Nullable FilterCache filterCache) {
@@ -61,17 +64,28 @@ public MapperQueryParser(String defaultField, Analyzer analyzer,
6164
setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
6265
}
6366

67+
@Override protected Query newTermQuery(Term term) {
68+
if (currentMapper != null) {
69+
Query termQuery = currentMapper.queryStringTermQuery(term);
70+
if (termQuery != null) {
71+
return termQuery;
72+
}
73+
}
74+
return super.newTermQuery(term);
75+
}
76+
6477
@Override public Query getFieldQuery(String field, String queryText) throws ParseException {
78+
currentMapper = null;
6579
if (mapperService != null) {
6680
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
6781
if (fieldMappers != null) {
68-
FieldMapper mapper = fieldMappers.fieldMappers().mapper();
69-
if (mapper != null) {
82+
currentMapper = fieldMappers.fieldMappers().mapper();
83+
if (currentMapper != null) {
7084
Query query;
71-
if (mapper.useFieldQueryWithQueryString()) {
72-
query = fieldMappers.fieldMappers().mapper().fieldQuery(queryText);
85+
if (currentMapper.useFieldQueryWithQueryString()) {
86+
query = currentMapper.fieldQuery(queryText);
7387
} else {
74-
query = super.getFieldQuery(mapper.names().indexName(), queryText);
88+
query = super.getFieldQuery(currentMapper.names().indexName(), queryText);
7589
}
7690
return wrapSmartNameQuery(query, fieldMappers, filterCache);
7791
}
@@ -87,11 +101,13 @@ public MapperQueryParser(String defaultField, Analyzer analyzer,
87101
if ("*".equals(part2)) {
88102
part2 = null;
89103
}
104+
currentMapper = null;
90105
if (mapperService != null) {
91106
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
92107
if (fieldMappers != null) {
93-
if (fieldMappers.fieldMappers().mapper() != null) {
94-
Query rangeQuery = fieldMappers.fieldMappers().mapper().rangeQuery(part1, part2, inclusive, inclusive);
108+
currentMapper = fieldMappers.fieldMappers().mapper();
109+
if (currentMapper != null) {
110+
Query rangeQuery = currentMapper.rangeQuery(part1, part2, inclusive, inclusive);
95111
return wrapSmartNameQuery(rangeQuery, fieldMappers, filterCache);
96112
}
97113
}
@@ -101,11 +117,13 @@ public MapperQueryParser(String defaultField, Analyzer analyzer,
101117

102118
@Override protected Query getPrefixQuery(String field, String termStr) throws ParseException {
103119
String indexedNameField = field;
120+
currentMapper = null;
104121
if (mapperService != null) {
105122
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
106123
if (fieldMappers != null) {
107-
if (fieldMappers.fieldMappers().mapper() != null) {
108-
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName();
124+
currentMapper = fieldMappers.fieldMappers().mapper();
125+
if (currentMapper != null) {
126+
indexedNameField = currentMapper.names().indexName();
109127
}
110128
return wrapSmartNameQuery(super.getPrefixQuery(indexedNameField, termStr), fieldMappers, filterCache);
111129
}
@@ -115,11 +133,13 @@ public MapperQueryParser(String defaultField, Analyzer analyzer,
115133

116134
@Override protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {
117135
String indexedNameField = field;
136+
currentMapper = null;
118137
if (mapperService != null) {
119138
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
120139
if (fieldMappers != null) {
121-
if (fieldMappers.fieldMappers().mapper() != null) {
122-
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName();
140+
currentMapper = fieldMappers.fieldMappers().mapper();
141+
if (currentMapper != null) {
142+
indexedNameField = currentMapper.names().indexName();
123143
}
124144
return wrapSmartNameQuery(super.getFuzzyQuery(indexedNameField, termStr, minSimilarity), fieldMappers, filterCache);
125145
}
@@ -129,11 +149,13 @@ public MapperQueryParser(String defaultField, Analyzer analyzer,
129149

130150
@Override protected Query getWildcardQuery(String field, String termStr) throws ParseException {
131151
String indexedNameField = field;
152+
currentMapper = null;
132153
if (mapperService != null) {
133154
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
134155
if (fieldMappers != null) {
135-
if (fieldMappers.fieldMappers().mapper() != null) {
136-
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName();
156+
currentMapper = fieldMappers.fieldMappers().mapper();
157+
if (currentMapper != null) {
158+
indexedNameField = currentMapper.names().indexName();
137159
}
138160
return wrapSmartNameQuery(super.getWildcardQuery(indexedNameField, termStr), fieldMappers, filterCache);
139161
}

0 commit comments

Comments
 (0)