Skip to content

Commit b3afee5

Browse files
committed
Switch back to using name
1 parent 7176455 commit b3afee5

File tree

8 files changed

+30
-88
lines changed

8 files changed

+30
-88
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DocValueFetcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
*/
2020
public final class DocValueFetcher implements ValueFetcher {
2121
private final DocValueFormat format;
22-
private final MappedFieldType fieldType;
22+
private final String field;
2323

24-
public DocValueFetcher(DocValueFormat format, MappedFieldType fieldType) {
24+
public DocValueFetcher(DocValueFormat format, String field) {
2525
this.format = format;
26-
this.fieldType = fieldType;
26+
this.field = field;
2727
}
2828

2929
@Override
3030
public List<Object> fetchValues(ValuesLookup lookup) throws IOException {
31-
return lookup.docValues(fieldType, format);
31+
return lookup.docValues(field, format);
3232
}
3333

3434
}

server/src/main/java/org/elasticsearch/runtimefields/mapper/AbstractScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected final void checkAllowExpensiveQueries(SearchExecutionContext context)
200200

201201
@Override
202202
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
203-
return new DocValueFetcher(docValueFormat(format, null), this);
203+
return new DocValueFetcher(docValueFormat(format, null), name());
204204
}
205205

206206
@Override

server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchDocValuesPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public FetchSubPhaseProcessor getProcessor(FetchContext context) {
4747
}
4848
ValueFetcher fetcher = new DocValueFetcher(
4949
ft.docValueFormat(fieldAndFormat.format, null),
50-
ft
50+
fieldAndFormat.field
5151
);
5252
fields.add(new DocValueField(fieldAndFormat.field, fetcher));
5353
}

server/src/main/java/org/elasticsearch/search/lookup/LeafDocLookup.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ public int hashCode() {
6969
}
7070
}
7171

72-
public ScriptDocValues<?> get(MappedFieldType fieldType, DocValueFormat format) {
73-
FormatKey key = new FormatKey(fieldType.name(), format);
72+
public ScriptDocValues<?> get(String name, DocValueFormat format) {
73+
FormatKey key = new FormatKey(name, format);
7474
ScriptDocValues<?> scriptValues = localCacheFormattedData.get(key);
7575
if (scriptValues == null) {
76+
MappedFieldType fieldType = fieldTypeLookup.apply(name);
77+
if (fieldType == null) {
78+
throw new IllegalArgumentException("Unknown field [" + name + "]");
79+
}
7680
// load fielddata on behalf of the script: otherwise it would need additional permissions
7781
// to deal with pagedbytes/ramusagestimator/etc
7882
scriptValues = AccessController.doPrivileged(new PrivilegedAction<ScriptDocValues<?>>() {

server/src/main/java/org/elasticsearch/search/lookup/LeafSearchLookup.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.elasticsearch.search.lookup;
1010

1111
import org.apache.lucene.index.LeafReaderContext;
12-
import org.elasticsearch.index.mapper.MappedFieldType;
1312
import org.elasticsearch.search.DocValueFormat;
1413

1514
import java.util.ArrayList;
@@ -56,8 +55,8 @@ public LeafDocLookup doc() {
5655
}
5756

5857
@Override
59-
public List<Object> docValues(MappedFieldType fieldType, DocValueFormat format) {
60-
return new ArrayList<>(this.docMap.get(fieldType, format));
58+
public List<Object> docValues(String field, DocValueFormat format) {
59+
return new ArrayList<>(this.docMap.get(field, format));
6160
}
6261

6362
public void setDocument(int docId) {

server/src/main/java/org/elasticsearch/search/lookup/ValuesLookup.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface ValuesLookup {
3030
/**
3131
* Returns a LeafDocLookup positioned on the current document
3232
*/
33-
List<Object> docValues(MappedFieldType fieldType, DocValueFormat format);
33+
List<Object> docValues(String field, DocValueFormat format);
3434

3535
/**
3636
* Override the source exposed by a ValuesLookup
@@ -46,8 +46,8 @@ public SourceLookup source() {
4646
}
4747

4848
@Override
49-
public List<Object> docValues(MappedFieldType fieldType, DocValueFormat format) {
50-
return in.docValues(fieldType, format);
49+
public List<Object> docValues(String field, DocValueFormat format) {
50+
return in.docValues(field, format);
5151
}
5252
};
5353
}
@@ -65,7 +65,7 @@ public SourceLookup source() {
6565
}
6666

6767
@Override
68-
public List<Object> docValues(MappedFieldType fieldType, DocValueFormat format) {
68+
public List<Object> docValues(String field, DocValueFormat format) {
6969
throw new UnsupportedOperationException("FieldData is not available from a source-only lookup");
7070
}
7171
};
@@ -84,7 +84,7 @@ public SourceLookup source() {
8484
}
8585

8686
@Override
87-
public List<Object> docValues(MappedFieldType fieldType, DocValueFormat format) {
87+
public List<Object> docValues(String field, DocValueFormat format) {
8888
throw new UnsupportedOperationException("FieldData is not available from a source-only lookup");
8989
}
9090
};

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ protected final List<?> fetchFromDocValues(MapperService mapperService, MappedFi
305305
iw.addDocument(mapperService.documentMapper().parse(source(b -> b.field(ft.name(), sourceValue))).rootDoc());
306306
}, iw -> {
307307
SearchLookup lookup = new SearchLookup(mapperService::fieldType, fieldDataLookup);
308-
ValueFetcher valueFetcher = new DocValueFetcher(format, ft);
308+
ValueFetcher valueFetcher = new DocValueFetcher(format, ft.name());
309309
IndexSearcher searcher = newSearcher(iw);
310310
LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
311311
LeafSearchLookup leaf = lookup.getLeafSearchLookup(context);

x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlattenedFieldMapper.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@
1111
import org.apache.lucene.index.LeafReaderContext;
1212
import org.apache.lucene.index.OrdinalMap;
1313
import org.apache.lucene.index.Term;
14-
import org.apache.lucene.search.AutomatonQuery;
1514
import org.apache.lucene.search.MultiTermQuery;
1615
import org.apache.lucene.search.PrefixQuery;
1716
import org.apache.lucene.search.Query;
1817
import org.apache.lucene.search.SortField;
19-
import org.apache.lucene.search.TermInSetQuery;
20-
import org.apache.lucene.search.TermQuery;
21-
import org.apache.lucene.search.TermRangeQuery;
2218
import org.apache.lucene.util.BytesRef;
23-
import org.elasticsearch.ElasticsearchException;
2419
import org.elasticsearch.common.lucene.Lucene;
2520
import org.elasticsearch.common.lucene.search.AutomatonQueries;
2621
import org.elasticsearch.common.unit.Fuzziness;
@@ -58,13 +53,10 @@
5853

5954
import java.io.IOException;
6055
import java.util.Arrays;
61-
import java.util.Collection;
6256
import java.util.List;
6357
import java.util.Map;
6458
import java.util.function.Supplier;
6559

66-
import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES;
67-
6860
/**
6961
* A field mapper that accepts a JSON object and flattens it into a single field. This data type
7062
* can be a useful alternative to an 'object' mapping when the object has a large, unknown set
@@ -170,19 +162,17 @@ public FlattenedFieldMapper build(ContentPath contentPath) {
170162
*/
171163
public static final class KeyedFlattenedFieldType extends StringFieldType {
172164
private final String key;
173-
private final String root;
174165

175-
public KeyedFlattenedFieldType(String root, boolean indexed, boolean hasDocValues, String key,
166+
public KeyedFlattenedFieldType(String name, boolean indexed, boolean hasDocValues, String key,
176167
boolean splitQueriesOnWhitespace, Map<String, String> meta) {
177-
super(root + "." + key, indexed, false, hasDocValues,
168+
super(name, indexed, false, hasDocValues,
178169
splitQueriesOnWhitespace ? TextSearchInfo.WHITESPACE_MATCH_ONLY : TextSearchInfo.SIMPLE_MATCH_ONLY,
179170
meta);
180171
this.key = key;
181-
this.root = root;
182172
}
183173

184-
private KeyedFlattenedFieldType(String root, String key, RootFlattenedFieldType ref) {
185-
this(root, ref.isSearchable(), ref.hasDocValues(), key, ref.splitQueriesOnWhitespace, ref.meta());
174+
private KeyedFlattenedFieldType(String name, String key, RootFlattenedFieldType ref) {
175+
this(name, ref.isSearchable(), ref.hasDocValues(), key, ref.splitQueriesOnWhitespace, ref.meta());
186176
}
187177

188178
@Override
@@ -196,7 +186,7 @@ public String key() {
196186

197187
@Override
198188
public Query existsQuery(SearchExecutionContext context) {
199-
Term term = new Term(root, FlattenedFieldParser.createKeyedValue(key, ""));
189+
Term term = new Term(name(), FlattenedFieldParser.createKeyedValue(key, ""));
200190
return new PrefixQuery(term);
201191
}
202192

@@ -215,15 +205,8 @@ public Query rangeQuery(Object lowerTerm,
215205
"] fields must include both an upper and a lower bound.");
216206
}
217207

218-
if (context.allowExpensiveQueries() == false) {
219-
throw new ElasticsearchException("[range] queries on [text] or [keyword] fields cannot be executed when '" +
220-
ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
221-
}
222-
failIfNotIndexed();
223-
return new TermRangeQuery(root,
224-
indexedValueForSearch(lowerTerm),
225-
indexedValueForSearch(upperTerm),
226-
includeLower, includeUpper);
208+
return super.rangeQuery(lowerTerm, upperTerm,
209+
includeLower, includeUpper, context);
227210
}
228211

229212
@Override
@@ -251,51 +234,7 @@ public Query wildcardQuery(String value,
251234

252235
@Override
253236
public Query termQueryCaseInsensitive(Object value, SearchExecutionContext context) {
254-
return AutomatonQueries.caseInsensitiveTermQuery(new Term(root, indexedValueForSearch(value)));
255-
}
256-
257-
@Override
258-
public Query termQuery(Object value, SearchExecutionContext context) {
259-
failIfNotIndexed();
260-
return new TermQuery(new Term(root, indexedValueForSearch(value)));
261-
}
262-
263-
@Override
264-
public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
265-
failIfNotIndexed();
266-
BytesRef[] bytesRefs = values.stream().map(this::indexedValueForSearch).toArray(BytesRef[]::new);
267-
return new TermInSetQuery(root, bytesRefs);
268-
}
269-
270-
@Override
271-
public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitive, SearchExecutionContext context) {
272-
if (context.allowExpensiveQueries() == false) {
273-
throw new ElasticsearchException("[prefix] queries cannot be executed when '" +
274-
ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text " +
275-
"fields please enable [index_prefixes].");
276-
}
277-
failIfNotIndexed();
278-
if (caseInsensitive) {
279-
AutomatonQuery query = AutomatonQueries.caseInsensitivePrefixQuery((new Term(root, indexedValueForSearch(value))));
280-
if (method != null) {
281-
query.setRewriteMethod(method);
282-
}
283-
return query;
284-
285-
}
286-
PrefixQuery query = new PrefixQuery(new Term(root, indexedValueForSearch(value)));
287-
if (method != null) {
288-
query.setRewriteMethod(method);
289-
}
290-
return query;
291-
}
292-
293-
@Override
294-
protected void failIfNotIndexed() {
295-
if (isIndexed == false) {
296-
// we throw an IAE rather than an ISE so that it translates to a 4xx code rather than 5xx code on the http layer
297-
throw new IllegalArgumentException("Cannot search on field [" + root + "] since it is not indexed.");
298-
}
237+
return AutomatonQueries.caseInsensitiveTermQuery(new Term(name(), indexedValueForSearch(value)));
299238
}
300239

301240
@Override
@@ -314,7 +253,7 @@ public BytesRef indexedValueForSearch(Object value) {
314253
@Override
315254
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) {
316255
failIfNoDocValues();
317-
return new KeyedFlattenedFieldData.Builder(root + "._keyed", key, CoreValuesSourceType.KEYWORD);
256+
return new KeyedFlattenedFieldData.Builder(name(), key, CoreValuesSourceType.KEYWORD);
318257
}
319258

320259
@Override
@@ -501,7 +440,7 @@ public RootFlattenedFieldType fieldType() {
501440

502441
@Override
503442
public KeyedFlattenedFieldType keyedFieldType(String key) {
504-
return new KeyedFlattenedFieldType(name(), key, fieldType());
443+
return new KeyedFlattenedFieldType(keyedFieldName(), key, fieldType());
505444
}
506445

507446
public String keyedFieldName() {

0 commit comments

Comments
 (0)