Mapping: string mapping to automatically set omit_norms to true and index_options to docs when setting index to not_analyzed

closes #2349
This commit is contained in:
Shay Banon 2012-10-22 19:01:01 +02:00
parent e40f4d2b1a
commit 04eabbd38a
3 changed files with 70 additions and 12 deletions

View File

@ -107,24 +107,17 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
public abstract static class Builder<T extends Builder, Y extends AbstractFieldMapper> extends Mapper.Builder<T, Y> {
protected Field.Index index = Defaults.INDEX;
protected Field.Store store = Defaults.STORE;
protected Field.TermVector termVector = Defaults.TERM_VECTOR;
protected float boost = Defaults.BOOST;
protected boolean omitNorms = Defaults.OMIT_NORMS;
protected boolean omitNormsSet = false;
protected String indexName;
protected NamedAnalyzer indexAnalyzer;
protected NamedAnalyzer searchAnalyzer;
protected Boolean includeInAll;
protected IndexOptions indexOptions = Defaults.INDEX_OPTIONS;
protected boolean indexOptionsSet = false;
protected Builder(String name) {
super(name);
@ -152,11 +145,13 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
protected T omitNorms(boolean omitNorms) {
this.omitNorms = omitNorms;
this.omitNormsSet = true;
return builder;
}
protected T indexOptions(IndexOptions indexOptions) {
this.indexOptions = indexOptions;
this.indexOptionsSet = true;
return builder;
}

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.analysis.NamedCustomAnalyzer;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.core.BooleanFieldMapper.Defaults;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import java.io.IOException;
@ -111,6 +110,17 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
searchAnalyzer = new NamedCustomAnalyzer(searchAnalyzer, positionOffsetGap);
searchQuotedAnalyzer = new NamedCustomAnalyzer(searchQuotedAnalyzer, positionOffsetGap);
}
// if the field is not analyzed, then by default, we should omit norms and have docs only
// index options, as probably what the user really wants
// if they are set explicitly, we will use those values
if (index == Field.Index.NOT_ANALYZED) {
if (!omitNormsSet) {
omitNorms = true;
}
if (!indexOptionsSet) {
indexOptions = IndexOptions.DOCS_ONLY;
}
}
StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
index, store, termVector, boost, omitNorms, indexOptions, nullValue,
indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, ignoreAbove);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.test.unit.index.mapper.string;
import org.apache.lucene.index.FieldInfo;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
@ -26,8 +27,7 @@ import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
/**
*/
@ -66,4 +66,57 @@ public class SimpleStringMappingTests {
assertThat(doc.rootDoc().getFieldable("field"), nullValue());
}
@Test
public void testDefaultsForAnalyzed() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").endObject().endObject()
.endObject().endObject().string();
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "1234")
.endObject()
.bytes());
assertThat(doc.rootDoc().getFieldable("field").getOmitNorms(), equalTo(false));
assertThat(doc.rootDoc().getFieldable("field").getIndexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS));
}
@Test
public void testDefaultsForNotAnalyzed() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("index", "not_analyzed").endObject().endObject()
.endObject().endObject().string();
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "1234")
.endObject()
.bytes());
assertThat(doc.rootDoc().getFieldable("field").getOmitNorms(), equalTo(true));
assertThat(doc.rootDoc().getFieldable("field").getIndexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_ONLY));
// now test it explicitly set
mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field").field("type", "string").field("index", "not_analyzed").field("omit_norms", false).field("index_options", "freqs").endObject().endObject()
.endObject().endObject().string();
defaultMapper = MapperTests.newParser().parse(mapping);
doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", "1234")
.endObject()
.bytes());
assertThat(doc.rootDoc().getFieldable("field").getOmitNorms(), equalTo(false));
assertThat(doc.rootDoc().getFieldable("field").getIndexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_AND_FREQS));
}
}