forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…78981) Currently we don't allow retrieving metadata fields through the fields option in search but throw an error on this case. In elastic#78828 we started to enable this for `_id` if the field is explicitely requested. This PR adds `_ignored` and `_routing` metadata fields which are also internally handled as stored fields to the list of fields that can be explicitely retrieved.
- Loading branch information
Christoph Büscher
committed
Oct 14, 2021
1 parent
7b66bfb
commit 58aa5f7
Showing
6 changed files
with
143 additions
and
8 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
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
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