|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.logsdb.patternedtext; |
| 9 | + |
| 10 | +import org.apache.lucene.document.Field; |
| 11 | +import org.apache.lucene.document.FieldType; |
| 12 | +import org.apache.lucene.document.SortedSetDocValuesField; |
| 13 | +import org.apache.lucene.index.IndexOptions; |
| 14 | +import org.apache.lucene.util.BytesRef; |
| 15 | +import org.elasticsearch.common.util.FeatureFlag; |
| 16 | +import org.elasticsearch.index.IndexVersion; |
| 17 | +import org.elasticsearch.index.analysis.IndexAnalyzers; |
| 18 | +import org.elasticsearch.index.analysis.NamedAnalyzer; |
| 19 | +import org.elasticsearch.index.mapper.CompositeSyntheticFieldLoader; |
| 20 | +import org.elasticsearch.index.mapper.DocumentParserContext; |
| 21 | +import org.elasticsearch.index.mapper.FieldMapper; |
| 22 | +import org.elasticsearch.index.mapper.MapperBuilderContext; |
| 23 | +import org.elasticsearch.index.mapper.TextParams; |
| 24 | +import org.elasticsearch.index.mapper.TextSearchInfo; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link FieldMapper} that assigns every document the same value. |
| 31 | + */ |
| 32 | +public class PatternedTextFieldMapper extends FieldMapper { |
| 33 | + |
| 34 | + public static final FeatureFlag PATTERNED_TEXT_MAPPER = new FeatureFlag("patterned_text"); |
| 35 | + |
| 36 | + public static class Defaults { |
| 37 | + public static final FieldType FIELD_TYPE; |
| 38 | + |
| 39 | + static { |
| 40 | + final FieldType ft = new FieldType(); |
| 41 | + ft.setTokenized(true); |
| 42 | + ft.setStored(false); |
| 43 | + ft.setStoreTermVectors(false); |
| 44 | + ft.setOmitNorms(true); |
| 45 | + ft.setIndexOptions(IndexOptions.DOCS); |
| 46 | + FIELD_TYPE = freezeAndDeduplicateFieldType(ft); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class Builder extends FieldMapper.Builder { |
| 51 | + |
| 52 | + private final IndexVersion indexCreatedVersion; |
| 53 | + |
| 54 | + private final Parameter<Map<String, String>> meta = Parameter.metaParam(); |
| 55 | + |
| 56 | + private final TextParams.Analyzers analyzers; |
| 57 | + |
| 58 | + public Builder(String name, IndexVersion indexCreatedVersion, IndexAnalyzers indexAnalyzers) { |
| 59 | + super(name); |
| 60 | + this.indexCreatedVersion = indexCreatedVersion; |
| 61 | + this.analyzers = new TextParams.Analyzers( |
| 62 | + indexAnalyzers, |
| 63 | + m -> ((PatternedTextFieldMapper) m).indexAnalyzer, |
| 64 | + m -> ((PatternedTextFieldMapper) m).positionIncrementGap, |
| 65 | + indexCreatedVersion |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected Parameter<?>[] getParameters() { |
| 71 | + return new Parameter<?>[] { meta }; |
| 72 | + } |
| 73 | + |
| 74 | + private PatternedTextFieldType buildFieldType(MapperBuilderContext context) { |
| 75 | + NamedAnalyzer searchAnalyzer = analyzers.getSearchAnalyzer(); |
| 76 | + NamedAnalyzer searchQuoteAnalyzer = analyzers.getSearchQuoteAnalyzer(); |
| 77 | + NamedAnalyzer indexAnalyzer = analyzers.getIndexAnalyzer(); |
| 78 | + TextSearchInfo tsi = new TextSearchInfo(Defaults.FIELD_TYPE, null, searchAnalyzer, searchQuoteAnalyzer); |
| 79 | + return new PatternedTextFieldType( |
| 80 | + context.buildFullName(leafName()), |
| 81 | + tsi, |
| 82 | + indexAnalyzer, |
| 83 | + context.isSourceSynthetic(), |
| 84 | + meta.getValue() |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public PatternedTextFieldMapper build(MapperBuilderContext context) { |
| 90 | + return new PatternedTextFieldMapper(leafName(), buildFieldType(context), builderParams(this, context), this); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers())); |
| 95 | + |
| 96 | + private final IndexVersion indexCreatedVersion; |
| 97 | + private final IndexAnalyzers indexAnalyzers; |
| 98 | + private final NamedAnalyzer indexAnalyzer; |
| 99 | + private final int positionIncrementGap; |
| 100 | + private final FieldType fieldType; |
| 101 | + |
| 102 | + private PatternedTextFieldMapper( |
| 103 | + String simpleName, |
| 104 | + PatternedTextFieldType mappedFieldPatternedTextFieldType, |
| 105 | + BuilderParams builderParams, |
| 106 | + Builder builder |
| 107 | + ) { |
| 108 | + super(simpleName, mappedFieldPatternedTextFieldType, builderParams); |
| 109 | + assert mappedFieldPatternedTextFieldType.getTextSearchInfo().isTokenized(); |
| 110 | + assert mappedFieldPatternedTextFieldType.hasDocValues() == false; |
| 111 | + this.fieldType = Defaults.FIELD_TYPE; |
| 112 | + this.indexCreatedVersion = builder.indexCreatedVersion; |
| 113 | + this.indexAnalyzers = builder.analyzers.indexAnalyzers; |
| 114 | + this.indexAnalyzer = builder.analyzers.getIndexAnalyzer(); |
| 115 | + this.positionIncrementGap = builder.analyzers.positionIncrementGap.getValue(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Map<String, NamedAnalyzer> indexAnalyzers() { |
| 120 | + return Map.of(mappedFieldType.name(), indexAnalyzer); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public FieldMapper.Builder getMergeBuilder() { |
| 125 | + return new Builder(leafName(), indexCreatedVersion, indexAnalyzers).init(this); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void parseCreateField(DocumentParserContext context) throws IOException { |
| 130 | + final String value = context.parser().textOrNull(); |
| 131 | + if (value == null) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + var existingValue = context.doc().getField(fieldType().name()); |
| 136 | + if (existingValue != null) { |
| 137 | + throw new IllegalArgumentException("Multiple values are not allowed for field [" + fieldType().name() + "]."); |
| 138 | + } |
| 139 | + |
| 140 | + // Parse template and args. |
| 141 | + PatternedTextValueProcessor.Parts parts = PatternedTextValueProcessor.split(value); |
| 142 | + |
| 143 | + // Add index on original value |
| 144 | + context.doc().add(new Field(fieldType().name(), value, fieldType)); |
| 145 | + |
| 146 | + // Add template doc_values |
| 147 | + context.doc().add(new SortedSetDocValuesField(fieldType().templateFieldName(), new BytesRef(parts.template()))); |
| 148 | + |
| 149 | + // Add args doc_values |
| 150 | + if (parts.args().isEmpty() == false) { |
| 151 | + String remainingArgs = PatternedTextValueProcessor.encodeRemainingArgs(parts); |
| 152 | + context.doc().add(new SortedSetDocValuesField(fieldType().argsFieldName(), new BytesRef(remainingArgs))); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected String contentType() { |
| 158 | + return PatternedTextFieldType.CONTENT_TYPE; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public PatternedTextFieldType fieldType() { |
| 163 | + return (PatternedTextFieldType) super.fieldType(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected SyntheticSourceSupport syntheticSourceSupport() { |
| 168 | + return new SyntheticSourceSupport.Native( |
| 169 | + () -> new CompositeSyntheticFieldLoader( |
| 170 | + leafName(), |
| 171 | + fullPath(), |
| 172 | + new PatternedTextSyntheticFieldLoaderLayer(fieldType().name(), fieldType().templateFieldName(), fieldType().argsFieldName()) |
| 173 | + ) |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments