-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple 'fetch fields' phase. (#55639)
Currently the phase just looks up each field name in the _source and returns its values in the 'fields' section of the response. There are several aspects that need improvement -- this PR just lays out the initial class structure and tests.
- Loading branch information
1 parent
fb2e3f8
commit 1aa61bb
Showing
19 changed files
with
527 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
setup: | ||
- skip: | ||
version: " - 7.99.99" | ||
reason: "fields retrieval is currently only implemented on master" | ||
- do: | ||
indices.create: | ||
index: test | ||
body: | ||
mappings: | ||
properties: | ||
keyword: | ||
type: keyword | ||
integer_range: | ||
type: integer_range | ||
|
||
- do: | ||
index: | ||
index: test | ||
id: 1 | ||
body: | ||
keyword: [ "first", "second" ] | ||
integer_range: | ||
gte: 0 | ||
lte: 42 | ||
|
||
- do: | ||
indices.refresh: | ||
index: [ test ] | ||
|
||
--- | ||
"Test basic field retrieval": | ||
- do: | ||
search: | ||
index: test | ||
body: | ||
fields: [keyword, integer_range] | ||
|
||
- is_true: hits.hits.0._id | ||
- is_true: hits.hits.0._source | ||
|
||
- match: { hits.hits.0.fields.keyword.0: first } | ||
- match: { hits.hits.0.fields.keyword.1: second } | ||
|
||
- match: { hits.hits.0.fields.integer_range.0.gte: 0 } | ||
- match: { hits.hits.0.fields.integer_range.0.lte: 42 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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.search.fetch.subphase; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* The context needed to retrieve fields. | ||
*/ | ||
public class FetchFieldsContext { | ||
|
||
private final List<String> fields; | ||
|
||
public FetchFieldsContext(List<String> fields) { | ||
this.fields = fields; | ||
} | ||
|
||
public List<String> fields() { | ||
return this.fields; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* 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.search.fetch.subphase; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.ReaderUtil; | ||
import org.elasticsearch.common.document.DocumentField; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.index.mapper.DocumentMapper; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.fetch.FetchSubPhase; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
import org.elasticsearch.search.lookup.SourceLookup; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A fetch sub-phase for high-level field retrieval. Given a list of fields, it | ||
* retrieves the field values from _source and returns them as document fields. | ||
*/ | ||
public final class FetchFieldsPhase implements FetchSubPhase { | ||
|
||
@Override | ||
public void hitsExecute(SearchContext context, SearchHit[] hits) { | ||
hitsExecute(context, hit -> getSourceLookup(context, hit), hits); | ||
} | ||
|
||
// Visible for testing. | ||
@SuppressWarnings("unchecked") | ||
void hitsExecute(SearchContext context, | ||
Function<SearchHit, SourceLookup> sourceProvider, | ||
SearchHit[] hits) { | ||
FetchFieldsContext fetchFieldsContext = context.fetchFieldsContext(); | ||
if (fetchFieldsContext == null || fetchFieldsContext.fields().isEmpty()) { | ||
return; | ||
} | ||
|
||
DocumentMapper documentMapper = context.mapperService().documentMapper(); | ||
if (documentMapper.sourceMapper().enabled() == false) { | ||
throw new IllegalArgumentException("Unable to retrieve the requested [fields] since _source is " + | ||
"disabled in the mappings for index [" + context.indexShard().shardId().getIndexName() + "]"); | ||
} | ||
|
||
Set<String> fields = new HashSet<>(); | ||
for (String fieldPattern : context.fetchFieldsContext().fields()) { | ||
if (documentMapper.objectMappers().containsKey(fieldPattern)) { | ||
continue; | ||
} | ||
Collection<String> concreteFields = context.mapperService().simpleMatchToFullName(fieldPattern); | ||
fields.addAll(concreteFields); | ||
} | ||
|
||
for (SearchHit hit : hits) { | ||
SourceLookup sourceLookup = sourceProvider.apply(hit); | ||
Map<String, Object> valuesByField = extractValues(sourceLookup, fields); | ||
|
||
for (Map.Entry<String, Object> entry : valuesByField.entrySet()) { | ||
String field = entry.getKey(); | ||
Object value = entry.getValue(); | ||
List<Object> values = value instanceof List | ||
? (List<Object>) value | ||
: List.of(value); | ||
|
||
DocumentField documentField = new DocumentField(field, values); | ||
hit.setDocumentField(field, documentField); | ||
} | ||
} | ||
} | ||
|
||
private SourceLookup getSourceLookup(SearchContext context, SearchHit hit) { | ||
SourceLookup sourceLookup = context.lookup().source(); | ||
int readerIndex = ReaderUtil.subIndex(hit.docId(), context.searcher().getIndexReader().leaves()); | ||
LeafReaderContext readerContext = context.searcher().getIndexReader().leaves().get(readerIndex); | ||
sourceLookup.setSegmentAndDocument(readerContext, hit.docId()); | ||
return sourceLookup; | ||
} | ||
|
||
/** | ||
* For each of the provided paths, return its value in the source. Note that in contrast with | ||
* {@link SourceLookup#extractRawValues}, array and object values can be returned. | ||
*/ | ||
private Map<String, Object> extractValues(SourceLookup sourceLookup, Collection<String> paths) { | ||
Map<String, Object> result = new HashMap<>(paths.size()); | ||
for (String path : paths) { | ||
Object value = XContentMapValues.extractValue(path, sourceLookup); | ||
if (value != null) { | ||
result.put(path, value); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.