-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to retrieve document metadata via fields option (#78939)
Currently we exclude metadata fields from being looked up using the fields option in search. However, as issue like #75836 show, they can still be retrieved e.g. via aliases and then fetching their values causes errors. With this change, we enable retrieval of metadata fields (like `_id`, `_ignored` etc.) using the fields option when the field is explicitely requested. We still continue to exclude any metadata field from matching wildcard patterns, but they should be retrievable via an exact name or if there is an alias definition with a path to a metadata field. This change adds support for the `_id`, `_routing`, `_ignored`, `_index` and `_version` field in particular. Backport of #78828, #78981 and #79042
- Loading branch information
Christoph Büscher
committed
Oct 14, 2021
1 parent
5d391f0
commit e7c6ed8
Showing
15 changed files
with
454 additions
and
36 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
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
44 changes: 44 additions & 0 deletions
44
server/src/main/java/org/elasticsearch/index/mapper/StoredValueFetcher.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,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.search.lookup.LeafSearchLookup; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
import org.elasticsearch.search.lookup.SourceLookup; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Value fetcher that loads from stored values. | ||
*/ | ||
public final class StoredValueFetcher implements ValueFetcher { | ||
|
||
private final SearchLookup lookup; | ||
private LeafSearchLookup leafSearchLookup; | ||
private final String fieldname; | ||
|
||
public StoredValueFetcher(SearchLookup lookup, String fieldname) { | ||
this.lookup = lookup; | ||
this.fieldname = fieldname; | ||
} | ||
|
||
@Override | ||
public void setNextReader(LeafReaderContext context) { | ||
this.leafSearchLookup = lookup.getLeafSearchLookup(context); | ||
} | ||
|
||
@Override | ||
public List<Object> fetchValues(SourceLookup lookup, List<Object> ignoredValues) throws IOException { | ||
leafSearchLookup.setDocument(lookup.docId()); | ||
return leafSearchLookup.fields().get(fieldname).getValues(); | ||
} | ||
|
||
} |
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
68 changes: 68 additions & 0 deletions
68
server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldMapperTests.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,68 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.index.IndexOptions; | ||
import org.apache.lucene.index.IndexableField; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.elasticsearch.core.List; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class IgnoredFieldMapperTests extends MapperServiceTestCase { | ||
|
||
public void testIncludeInObjectNotAllowed() throws Exception { | ||
DocumentMapper docMapper = createDocumentMapper(mapping(b -> {})); | ||
|
||
Exception e = expectThrows(MapperParsingException.class, | ||
() -> docMapper.parse(source(b -> b.field("_ignored", 1)))); | ||
|
||
assertThat(e.getCause().getMessage(), | ||
containsString("Field [_ignored] is a metadata field and cannot be added inside a document")); | ||
} | ||
|
||
public void testDefaults() throws IOException { | ||
DocumentMapper mapper = createDocumentMapper( | ||
mapping(b -> b.startObject("field").field("type", "keyword").field("ignore_above", 3).endObject()) | ||
); | ||
ParsedDocument document = mapper.parse(source(b -> b.field("field", "value"))); | ||
IndexableField[] fields = document.rootDoc().getFields(IgnoredFieldMapper.NAME); | ||
assertEquals(1, fields.length); | ||
assertEquals(IndexOptions.DOCS, fields[0].fieldType().indexOptions()); | ||
assertTrue(fields[0].fieldType().stored()); | ||
} | ||
|
||
public void testFetchIgnoredFieldValue() throws IOException { | ||
MapperService mapperService = createMapperService( | ||
fieldMapping(b -> b.field("type", "keyword").field("ignore_above", 3)) | ||
); | ||
withLuceneIndex(mapperService, iw -> { | ||
iw.addDocument(mapperService.documentMapper().parse(source(b -> b.field("field", "value"))).rootDoc()); | ||
}, iw -> { | ||
SearchLookup lookup = new SearchLookup(mapperService::fieldType, fieldDataLookup()); | ||
SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class); | ||
when(searchExecutionContext.lookup()).thenReturn(lookup); | ||
IgnoredFieldMapper.IgnoredFieldType ft = (IgnoredFieldMapper.IgnoredFieldType) mapperService.fieldType("_ignored"); | ||
ValueFetcher valueFetcher = ft.valueFetcher(searchExecutionContext, null); | ||
IndexSearcher searcher = newSearcher(iw); | ||
LeafReaderContext context = searcher.getIndexReader().leaves().get(0); | ||
lookup.source().setSegmentAndDocument(context, 0); | ||
valueFetcher.setNextReader(context); | ||
assertEquals(List.of("field"), valueFetcher.fetchValues(lookup.source(), List.of())); | ||
}); | ||
} | ||
|
||
} |
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
Oops, something went wrong.