|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 12 | +import org.apache.lucene.search.Query; |
| 13 | +import org.apache.lucene.util.BytesRef; |
| 14 | +import org.elasticsearch.common.regex.Regex; |
| 15 | +import org.elasticsearch.features.NodeFeature; |
| 16 | +import org.elasticsearch.index.fielddata.FieldData; |
| 17 | +import org.elasticsearch.index.fielddata.FieldDataContext; |
| 18 | +import org.elasticsearch.index.fielddata.IndexFieldData; |
| 19 | +import org.elasticsearch.index.fielddata.ScriptDocValues; |
| 20 | +import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; |
| 21 | +import org.elasticsearch.index.query.QueryRewriteContext; |
| 22 | +import org.elasticsearch.index.query.SearchExecutionContext; |
| 23 | +import org.elasticsearch.script.field.DelegateDocValuesField; |
| 24 | +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; |
| 25 | +import org.elasticsearch.search.fetch.StoredFieldsSpec; |
| 26 | +import org.elasticsearch.search.lookup.Source; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public class IndexModeFieldMapper extends MetadataFieldMapper { |
| 32 | + |
| 33 | + static final NodeFeature QUERYING_INDEX_MODE = new NodeFeature("mapper.query_index_mode"); |
| 34 | + |
| 35 | + public static final String NAME = "_index_mode"; |
| 36 | + |
| 37 | + public static final String CONTENT_TYPE = "_index_mode"; |
| 38 | + |
| 39 | + private static final IndexModeFieldMapper INSTANCE = new IndexModeFieldMapper(); |
| 40 | + |
| 41 | + public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE); |
| 42 | + |
| 43 | + static final class IndexModeFieldType extends ConstantFieldType { |
| 44 | + |
| 45 | + static final IndexModeFieldType INSTANCE = new IndexModeFieldType(); |
| 46 | + |
| 47 | + private IndexModeFieldType() { |
| 48 | + super(NAME, Collections.emptyMap()); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String typeName() { |
| 53 | + return CONTENT_TYPE; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteContext context) { |
| 58 | + final String indexMode = context.getIndexSettings().getMode().getName(); |
| 59 | + return Regex.simpleMatch(pattern, indexMode, caseInsensitive); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Query existsQuery(SearchExecutionContext context) { |
| 64 | + return new MatchAllDocsQuery(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) { |
| 69 | + final String indexMode = fieldDataContext.indexSettings().getMode().getName(); |
| 70 | + return new ConstantIndexFieldData.Builder( |
| 71 | + indexMode, |
| 72 | + name(), |
| 73 | + CoreValuesSourceType.KEYWORD, |
| 74 | + (dv, n) -> new DelegateDocValuesField( |
| 75 | + new ScriptDocValues.Strings(new ScriptDocValues.StringsSupplier(FieldData.toString(dv))), |
| 76 | + n |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public BlockLoader blockLoader(BlockLoaderContext blContext) { |
| 83 | + final String indexMode = blContext.indexSettings().getMode().getName(); |
| 84 | + return BlockLoader.constantBytes(new BytesRef(indexMode)); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { |
| 89 | + return new ValueFetcher() { |
| 90 | + private final List<Object> indexMode = List.of(context.getIndexSettings().getMode().getName()); |
| 91 | + |
| 92 | + @Override |
| 93 | + public List<Object> fetchValues(Source source, int doc, List<Object> ignoredValues) { |
| 94 | + return indexMode; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public StoredFieldsSpec storedFieldsSpec() { |
| 99 | + return StoredFieldsSpec.NO_REQUIREMENTS; |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public IndexModeFieldMapper() { |
| 107 | + super(IndexModeFieldType.INSTANCE); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected String contentType() { |
| 112 | + return CONTENT_TYPE; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() { |
| 117 | + return SourceLoader.SyntheticFieldLoader.NOTHING; |
| 118 | + } |
| 119 | +} |
0 commit comments