From 14678a91ab0c6bf8eb25ba49fead1d0af736561c Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 18 Dec 2012 15:39:13 -0800 Subject: [PATCH] nested path to be represented as bytes as well as string --- .../index/mapper/FieldMapper.java | 2 -- .../index/mapper/object/ObjectMapper.java | 19 +++++++++++++------ .../index/search/nested/NestedDocsFilter.java | 4 ++-- .../search/nested/NonNestedDocsFilter.java | 3 ++- .../mapper/nested/NestedMappingTests.java | 6 +++--- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index c62a6f10360..1ce0c737748 100644 --- a/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -118,8 +118,6 @@ public interface FieldMapper { Names names(); - // LUCENE 4 UPGRADE Consider replacing these all with fieldType() and letting consumer pick and choose - FieldType fieldType(); float boost(); diff --git a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index 9f4f8b5620c..9412561f09d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -25,6 +25,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; import org.apache.lucene.search.Filter; +import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.joda.FormatDateTimeFormatter; @@ -280,7 +281,8 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { private final Nested nested; - private final String nestedTypePath; + private final String nestedTypePathAsString; + private final BytesRef nestedTypePathAsBytes; private final Filter nestedTypeFilter; @@ -304,8 +306,9 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { if (mappers != null) { this.mappers = copyOf(mappers); } - this.nestedTypePath = "__" + fullPath; - this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePath)); + this.nestedTypePathAsString = "__" + fullPath; + this.nestedTypePathAsBytes = new BytesRef(nestedTypePathAsString); + this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePathAsBytes)); } @Override @@ -377,8 +380,12 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { return this.fullPath; } - public String nestedTypePath() { - return nestedTypePath; + public BytesRef nestedTypePathAsBytes() { + return nestedTypePathAsBytes; + } + + public String nestedTypePathAsString() { + return nestedTypePathAsString; } public final Dynamic dynamic() { @@ -428,7 +435,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll { // the type of the nested doc starts with __, so we can identify that its a nested one in filters // note, we don't prefix it with the type of the doc since it allows us to execute a nested query // across types (for example, with similar nested objects) - nestedDoc.add(new Field(TypeFieldMapper.NAME, nestedTypePath, Field.Store.NO, Field.Index.NOT_ANALYZED)); + nestedDoc.add(new Field(TypeFieldMapper.NAME, nestedTypePathAsString, Field.Store.NO, Field.Index.NOT_ANALYZED)); restoreDoc = context.switchDoc(nestedDoc); context.addDoc(nestedDoc); } diff --git a/src/main/java/org/elasticsearch/index/search/nested/NestedDocsFilter.java b/src/main/java/org/elasticsearch/index/search/nested/NestedDocsFilter.java index 5940b40974d..f7a88f65e67 100644 --- a/src/main/java/org/elasticsearch/index/search/nested/NestedDocsFilter.java +++ b/src/main/java/org/elasticsearch/index/search/nested/NestedDocsFilter.java @@ -20,12 +20,12 @@ package org.elasticsearch.index.search.nested; import org.apache.lucene.index.AtomicReaderContext; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.util.Bits; +import org.apache.lucene.util.BytesRef; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import java.io.IOException; @@ -34,7 +34,7 @@ public class NestedDocsFilter extends Filter { public static final NestedDocsFilter INSTANCE = new NestedDocsFilter(); - private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, "__")); + private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__"))); private final int hashCode = filter.hashCode(); diff --git a/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java b/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java index 3a2918dbfb8..604c2ed21b1 100644 --- a/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java +++ b/src/main/java/org/elasticsearch/index/search/nested/NonNestedDocsFilter.java @@ -25,6 +25,7 @@ import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.util.Bits; +import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FixedBitSet; import org.elasticsearch.common.lucene.docset.DocIdSets; import org.elasticsearch.index.mapper.internal.TypeFieldMapper; @@ -35,7 +36,7 @@ public class NonNestedDocsFilter extends Filter { public static final NonNestedDocsFilter INSTANCE = new NonNestedDocsFilter(); - private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, "__")); + private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__"))); private final int hashCode = filter.hashCode(); diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/nested/NestedMappingTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/nested/NestedMappingTests.java index 9abe6921d39..10631fa39b7 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/nested/NestedMappingTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/nested/NestedMappingTests.java @@ -81,7 +81,7 @@ public class NestedMappingTests { .bytes()); assertThat(doc.docs().size(), equalTo(2)); - assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); + assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePathAsString())); assertThat(doc.docs().get(0).get("nested1.field1"), equalTo("1")); assertThat(doc.docs().get(0).get("nested1.field2"), equalTo("2")); @@ -99,10 +99,10 @@ public class NestedMappingTests { .bytes()); assertThat(doc.docs().size(), equalTo(3)); - assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); + assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePathAsString())); assertThat(doc.docs().get(0).get("nested1.field1"), equalTo("3")); assertThat(doc.docs().get(0).get("nested1.field2"), equalTo("4")); - assertThat(doc.docs().get(1).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); + assertThat(doc.docs().get(1).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePathAsString())); assertThat(doc.docs().get(1).get("nested1.field1"), equalTo("1")); assertThat(doc.docs().get(1).get("nested1.field2"), equalTo("2"));