Skip to content

add _meta for all mapping fields #5607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions docs/reference/mapping/meta.asciidoc
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
[[mapping-meta]]
== Meta

Each mapping can have custom meta data associated with it. These are
simple storage elements that are simply persisted along with the mapping
Each mapping and each field in mapping can have custom meta data associated with it.
These are simple storage elements that are simply persisted along with the mapping
and can be retrieved when fetching the mapping definition. The meta is
defined under the `_meta` element, for example:

[source,js]
--------------------------------------------------
{
"tweet" : {
"_meta" : {
"attr1" : "value1",
"attr2" : {
"attr3" : "value3"
}
"tweet": {
"_meta": {
"attr1": "value1",
"attr2": {
"attr3": "value3"
}
},
"_timestamp": {
"_meta": {
"attr1": "value1",
"attr2": {
"attr3": "value3"
}
}
},
"properties": {
"message": {
"type": "string",
"_meta": {
"attr1": "value1",
"attr2": {
"attr3": "value3"
}
}
}
}
}
}
--------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ public BoostFieldMapper boostFieldMapper() {
return rootMapper(BoostFieldMapper.class);
}

public AnalyzerMapper analyzerMapper() {
return rootMapper(AnalyzerMapper.class);
}

public Analyzer indexAnalyzer() {
return this.indexAnalyzer;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/elasticsearch/index/mapper/FieldMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.mapper;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.Term;
Expand Down Expand Up @@ -188,6 +189,11 @@ public static Loading parse(String loading, Loading defaultValue) {
*/
public AbstractFieldMapper.CopyTo copyTo();

/**
* Field meta map
*/
ImmutableMap<String, Object> meta();

/**
* Returns the actual value of the field.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
Expand Down Expand Up @@ -107,6 +108,7 @@ public abstract static class Builder<T extends Builder, Y extends AbstractFieldM
protected Settings fieldDataSettings;
protected final MultiFields.Builder multiFieldsBuilder;
protected CopyTo copyTo;
protected ImmutableMap<String, Object> meta;

protected Builder(String name, FieldType fieldType) {
super(name);
Expand Down Expand Up @@ -242,6 +244,11 @@ public T copyTo(CopyTo copyTo) {
return builder;
}

public Builder meta(ImmutableMap<String, Object> meta) {
this.meta = meta;
return this;
}

public Names buildNames(BuilderContext context) {
return new Names(name, buildIndexName(context), indexName == null ? name : indexName, buildFullName(context), context.path().sourcePath());
}
Expand Down Expand Up @@ -276,19 +283,21 @@ protected List<Field> initialValue() {
protected FieldDataType fieldDataType;
protected final MultiFields multiFields;
protected CopyTo copyTo;
protected volatile ImmutableMap<String, Object> meta;

protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer,
NamedAnalyzer searchAnalyzer, PostingsFormatProvider postingsFormat,
DocValuesFormatProvider docValuesFormat, SimilarityProvider similarity,
Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings) {
Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings,
ImmutableMap<String, Object> meta) {
this(names, boost, fieldType, docValues, indexAnalyzer, searchAnalyzer, postingsFormat, docValuesFormat, similarity,
normsLoading, fieldDataSettings, indexSettings, MultiFields.empty(), null);
normsLoading, fieldDataSettings, indexSettings, MultiFields.empty(), null, meta);
}

protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boolean docValues, NamedAnalyzer indexAnalyzer,
NamedAnalyzer searchAnalyzer, PostingsFormatProvider postingsFormat,
DocValuesFormatProvider docValuesFormat, SimilarityProvider similarity,
Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, ImmutableMap<String, Object> meta) {
this.names = names;
this.boost = boost;
this.fieldType = fieldType;
Expand Down Expand Up @@ -334,6 +343,7 @@ protected AbstractFieldMapper(Names names, float boost, FieldType fieldType, Boo
}
this.multiFields = multiFields;
this.copyTo = copyTo;
this.meta = meta;
}

@Nullable
Expand Down Expand Up @@ -400,6 +410,11 @@ public CopyTo copyTo() {
return copyTo;
}

@Override
public ImmutableMap<String, Object> meta() {
return this.meta;
}

@Override
public void parse(ParseContext context) throws IOException {
final List<Field> fields = FIELD_LIST.get();
Expand Down Expand Up @@ -626,6 +641,7 @@ public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappi
this.boost = fieldMergeWith.boost;
this.normsLoading = fieldMergeWith.normsLoading;
this.copyTo = fieldMergeWith.copyTo;
this.meta = fieldMergeWith.meta;
if (fieldMergeWith.postingsFormat != null) {
this.postingsFormat = fieldMergeWith.postingsFormat;
}
Expand Down Expand Up @@ -772,6 +788,9 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
if (copyTo != null) {
copyTo.toXContent(builder, params);
}
if (meta != null && !meta.isEmpty()) {
builder.field("_meta", meta);
}
}

protected static String indexOptionToString(IndexOptions indexOption) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.mapper.core;

import com.google.common.collect.ImmutableMap;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -87,7 +88,7 @@ public Builder compressThreshold(long compressThreshold) {
@Override
public BinaryFieldMapper build(BuilderContext context) {
return new BinaryFieldMapper(buildNames(context), fieldType, compress, compressThreshold, postingsProvider,
docValuesProvider, multiFieldsBuilder.build(this, context), copyTo);
docValuesProvider, multiFieldsBuilder.build(this, context), copyTo, meta);
}
}

Expand Down Expand Up @@ -121,8 +122,8 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext

protected BinaryFieldMapper(Names names, FieldType fieldType, Boolean compress, long compressThreshold,
PostingsFormatProvider postingsProvider, DocValuesFormatProvider docValuesProvider,
MultiFields multiFields, CopyTo copyTo) {
super(names, 1.0f, fieldType, null, null, null, postingsProvider, docValuesProvider, null, null, null, null, multiFields, copyTo);
MultiFields multiFields, CopyTo copyTo, ImmutableMap<String, Object> meta) {
super(names, 1.0f, fieldType, null, null, null, postingsProvider, docValuesProvider, null, null, null, null, multiFields, copyTo, meta);
this.compress = compress;
this.compressThreshold = compressThreshold;
}
Expand Down Expand Up @@ -221,6 +222,9 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
if (includeDefaults || fieldType.stored() != defaultFieldType().stored()) {
builder.field("store", fieldType.stored());
}
if (meta != null && !meta.isEmpty()) {
builder.field("_meta", meta);
}
}

@Override
Expand All @@ -233,6 +237,7 @@ public void merge(Mapper mergeWith, MergeContext mergeContext) throws MergeMappi
if (sourceMergeWith.compressThreshold != -1) {
this.compressThreshold = sourceMergeWith.compressThreshold;
}
this.meta = sourceMergeWith.meta;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.mapper.core;

import com.google.common.collect.ImmutableMap;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
Expand Down Expand Up @@ -98,7 +99,7 @@ public Builder tokenized(boolean tokenized) {
@Override
public BooleanFieldMapper build(BuilderContext context) {
return new BooleanFieldMapper(buildNames(context), boost, fieldType, nullValue, postingsProvider,
docValuesProvider, similarity, normsLoading, fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
docValuesProvider, similarity, normsLoading, fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, meta);
}
}

Expand All @@ -122,8 +123,8 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext

protected BooleanFieldMapper(Names names, float boost, FieldType fieldType, Boolean nullValue, PostingsFormatProvider postingsProvider,
DocValuesFormatProvider docValuesProvider, SimilarityProvider similarity, Loading normsLoading,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(names, boost, fieldType, null, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER, postingsProvider, docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo);
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, ImmutableMap<String, Object> meta) {
super(names, boost, fieldType, null, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER, postingsProvider, docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo, meta);
this.nullValue = nullValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.index.mapper.core;

import com.google.common.collect.ImmutableMap;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field;
Expand Down Expand Up @@ -93,7 +94,7 @@ public ByteFieldMapper build(BuilderContext context) {
ByteFieldMapper fieldMapper = new ByteFieldMapper(buildNames(context),
precisionStep, boost, fieldType, docValues, nullValue, ignoreMalformed(context),
coerce(context), postingsProvider, docValuesProvider, similarity, normsLoading,
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, meta);
fieldMapper.includeInAll(includeInAll);
return fieldMapper;
}
Expand Down Expand Up @@ -123,11 +124,12 @@ protected ByteFieldMapper(Names names, int precisionStep, float boost, FieldType
Byte nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
PostingsFormatProvider postingsProvider,
DocValuesFormatProvider docValuesProvider, SimilarityProvider similarity, Loading normsLoading,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo,
ImmutableMap<String, Object> meta) {
super(names, precisionStep, boost, fieldType, docValues,
ignoreMalformed, coerce, new NamedAnalyzer("_byte/" + precisionStep, new NumericIntegerAnalyzer(precisionStep)),
new NamedAnalyzer("_byte/max", new NumericIntegerAnalyzer(Integer.MAX_VALUE)), postingsProvider,
docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo);
docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo, meta);
this.nullValue = nullValue;
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.index.mapper.core;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -49,6 +50,7 @@
import java.io.Reader;
import java.util.*;

import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeMapValue;
import static org.elasticsearch.index.mapper.MapperBuilders.completionField;
import static org.elasticsearch.index.mapper.core.TypeParsers.parseMultiField;

Expand Down Expand Up @@ -138,7 +140,7 @@ public Builder contextMapping(SortedMap<String, ContextMapping> contextMapping)
@Override
public CompletionFieldMapper build(Mapper.BuilderContext context) {
return new CompletionFieldMapper(buildNames(context), indexAnalyzer, searchAnalyzer, postingsProvider, similarity, payloads,
preserveSeparators, preservePositionIncrements, maxInputLength, multiFieldsBuilder.build(this, context), copyTo, this.contextMapping);
preserveSeparators, preservePositionIncrements, maxInputLength, multiFieldsBuilder.build(this, context), copyTo, this.contextMapping, meta);
}

}
Expand Down Expand Up @@ -174,6 +176,8 @@ public static class TypeParser implements Mapper.TypeParser {
parseMultiField(builder, name, node, parserContext, fieldName, fieldNode);
} else if (fieldName.equals(Fields.CONTEXT)) {
builder.contextMapping(ContextBuilder.loadMappings(fieldNode));
} else if ("_meta".equals(fieldName)) {
builder.meta(ImmutableMap.copyOf(nodeMapValue(fieldNode, "_meta")));
} else {
throw new MapperParsingException("Unknown field [" + fieldName + "]");
}
Expand Down Expand Up @@ -215,8 +219,8 @@ private NamedAnalyzer getNamedAnalyzer(ParserContext parserContext, String name)
* @param contextMappings Configuration of context type. If none should be used set {@link ContextMapping.EMPTY_MAPPING}
*/
public CompletionFieldMapper(Names names, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, PostingsFormatProvider postingsProvider, SimilarityProvider similarity, boolean payloads,
boolean preserveSeparators, boolean preservePositionIncrements, int maxInputLength, MultiFields multiFields, CopyTo copyTo, SortedMap<String, ContextMapping> contextMappings) {
super(names, 1.0f, Defaults.FIELD_TYPE, null, indexAnalyzer, searchAnalyzer, postingsProvider, null, similarity, null, null, null, multiFields, copyTo);
boolean preserveSeparators, boolean preservePositionIncrements, int maxInputLength, MultiFields multiFields, CopyTo copyTo, SortedMap<String, ContextMapping> contextMappings, ImmutableMap<String, Object> meta) {
super(names, 1.0f, Defaults.FIELD_TYPE, null, indexAnalyzer, searchAnalyzer, postingsProvider, null, similarity, null, null, null, multiFields, copyTo, meta);
analyzingSuggestLookupProvider = new AnalyzingCompletionLookupProvider(preserveSeparators, false, preservePositionIncrements, payloads);
this.completionPostingsFormatProvider = new CompletionPostingsFormatProvider("completion", postingsProvider, analyzingSuggestLookupProvider);
this.preserveSeparators = preserveSeparators;
Expand Down Expand Up @@ -429,6 +433,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(Fields.MAX_INPUT_LENGTH.getPreferredName(), this.maxInputLength);
multiFields.toXContent(builder, params);

if (meta != null && !meta.isEmpty()) {
builder.field("_meta", meta);
}

if(!contextMapping.isEmpty()) {
builder.startObject(Fields.CONTEXT);
for (ContextMapping mapping : contextMapping.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.mapper.core;

import com.google.common.collect.ImmutableMap;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.search.Filter;
Expand Down Expand Up @@ -132,7 +133,7 @@ public DateFieldMapper build(BuilderContext context) {
DateFieldMapper fieldMapper = new DateFieldMapper(buildNames(context), dateTimeFormatter,
precisionStep, boost, fieldType, docValues, nullValue, timeUnit, roundCeil, ignoreMalformed(context), coerce(context),
postingsProvider, docValuesProvider, similarity, normsLoading, fieldDataSettings, context.indexSettings(),
multiFieldsBuilder.build(this, context), copyTo);
multiFieldsBuilder.build(this, context), copyTo, meta);
fieldMapper.includeInAll(includeInAll);
return fieldMapper;
}
Expand Down Expand Up @@ -185,12 +186,13 @@ public static class TypeParser implements Mapper.TypeParser {
protected DateFieldMapper(Names names, FormatDateTimeFormatter dateTimeFormatter, int precisionStep, float boost, FieldType fieldType, Boolean docValues,
String nullValue, TimeUnit timeUnit, boolean roundCeil, Explicit<Boolean> ignoreMalformed,Explicit<Boolean> coerce,
PostingsFormatProvider postingsProvider, DocValuesFormatProvider docValuesProvider, SimilarityProvider similarity,

Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
Loading normsLoading, @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo,
ImmutableMap<String, Object> meta) {
super(names, precisionStep, boost, fieldType, docValues, ignoreMalformed, coerce, new NamedAnalyzer("_date/" + precisionStep,
new NumericDateAnalyzer(precisionStep, dateTimeFormatter.parser())),
new NamedAnalyzer("_date/max", new NumericDateAnalyzer(Integer.MAX_VALUE, dateTimeFormatter.parser())),
postingsProvider, docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo);
postingsProvider, docValuesProvider, similarity, normsLoading, fieldDataSettings, indexSettings, multiFields, copyTo,
meta);
this.dateTimeFormatter = dateTimeFormatter;
this.nullValue = nullValue;
this.timeUnit = timeUnit;
Expand Down
Loading