From 0ee889fd8b263cd4117708491c2687dff8d8b743 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 25 Mar 2014 15:15:51 +1100 Subject: [PATCH] Fix include_in_all for multi field, to not include multi fields into _all field. Closes #5522 --- .../index/mapper/ParseContext.java | 12 ++++++++++ .../mapper/core/AbstractFieldMapper.java | 4 ++++ .../mapper/all/SimpleAllMapperTests.java | 22 ++++++++++++++++++ .../index/mapper/all/multifield-mapping.json | 23 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 src/test/java/org/elasticsearch/index/mapper/all/multifield-mapping.json diff --git a/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index 69d82a88704..25234e5afd3 100644 --- a/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -156,6 +156,7 @@ public class ParseContext { private boolean mappingsModified = false; private boolean withinNewMapper = false; private boolean withinCopyTo = false; + private boolean withinMultiFields = false; private boolean externalValueSet; @@ -237,6 +238,14 @@ public class ParseContext { return withinCopyTo; } + public void setWithinMultiFields() { + this.withinMultiFields = true; + } + + public void clearWithinMultiFields() { + this.withinMultiFields = false; + } + public String index() { return this.index; } @@ -360,6 +369,9 @@ public class ParseContext { if (withinCopyTo) { return false; } + if (withinMultiFields) { + return false; + } if (!docMapper.allFieldMapper().enabled()) { return false; } diff --git a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java index 8dd163bf0f3..ba3ea228e97 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java @@ -911,6 +911,8 @@ public abstract class AbstractFieldMapper implements FieldMapper { return; } + context.setWithinMultiFields(); + ContentPath.Type origPathType = context.path().pathType(); context.path().pathType(pathType); @@ -920,6 +922,8 @@ public abstract class AbstractFieldMapper implements FieldMapper { } context.path().remove(); context.path().pathType(origPathType); + + context.clearWithinMultiFields(); } // No need for locking, because locking is taken care of in ObjectMapper#merge and DocumentMapper#merge diff --git a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java index 96c94c0dd91..9b20ef73350 100644 --- a/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/all/SimpleAllMapperTests.java @@ -31,6 +31,7 @@ import org.elasticsearch.common.lucene.all.AllTermQuery; import org.elasticsearch.common.lucene.all.AllTokenStream; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; @@ -40,6 +41,7 @@ import org.elasticsearch.test.ElasticsearchTestCase; import org.hamcrest.Matchers; import org.junit.Test; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -280,4 +282,24 @@ public class SimpleAllMapperTests extends ElasticsearchTestCase { } } + + @Test + public void testMultiField() throws IOException { + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/multifield-mapping.json"); + DocumentMapper docMapper = MapperTestUtils.newParser().parse(mapping); + + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject() + .field("_id", "1") + .field("foo") + .startObject() + .field("bar", "Elasticsearch rules!") + .endObject() + .endObject(); + + Document doc = docMapper.parse(builder.bytes()).rootDoc(); + AllField field = (AllField) doc.getField("_all"); + AllEntries allEntries = ((AllTokenStream) field.tokenStream(docMapper.mappers().indexAnalyzer())).allEntries(); + assertThat(allEntries.fields(), empty()); + } } diff --git a/src/test/java/org/elasticsearch/index/mapper/all/multifield-mapping.json b/src/test/java/org/elasticsearch/index/mapper/all/multifield-mapping.json new file mode 100644 index 00000000000..5a0ad92afad --- /dev/null +++ b/src/test/java/org/elasticsearch/index/mapper/all/multifield-mapping.json @@ -0,0 +1,23 @@ +{ + "test": { + "properties": { + "foo": { + "type": "nested", + "include_in_all": false, + "properties": { + "bar": { + "type": "string", + "index": "not_analyzed", + "include_in_all": false, + "fields": { + "lower": { + "analyzer": "standard", + "type": "string" + } + } + } + } + } + } + } +} \ No newline at end of file