nested path to be represented as bytes as well as string

This commit is contained in:
Shay Banon 2012-12-18 15:39:13 -08:00
parent 2950799243
commit 14678a91ab
5 changed files with 20 additions and 14 deletions

View File

@ -118,8 +118,6 @@ public interface FieldMapper<T> {
Names names(); Names names();
// LUCENE 4 UPGRADE Consider replacing these all with fieldType() and letting consumer pick and choose
FieldType fieldType(); FieldType fieldType();
float boost(); float boost();

View File

@ -25,6 +25,7 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.FormatDateTimeFormatter;
@ -280,7 +281,8 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
private final Nested nested; private final Nested nested;
private final String nestedTypePath; private final String nestedTypePathAsString;
private final BytesRef nestedTypePathAsBytes;
private final Filter nestedTypeFilter; private final Filter nestedTypeFilter;
@ -304,8 +306,9 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
if (mappers != null) { if (mappers != null) {
this.mappers = copyOf(mappers); this.mappers = copyOf(mappers);
} }
this.nestedTypePath = "__" + fullPath; this.nestedTypePathAsString = "__" + fullPath;
this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePath)); this.nestedTypePathAsBytes = new BytesRef(nestedTypePathAsString);
this.nestedTypeFilter = new TermFilter(new Term(TypeFieldMapper.NAME, nestedTypePathAsBytes));
} }
@Override @Override
@ -377,8 +380,12 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
return this.fullPath; return this.fullPath;
} }
public String nestedTypePath() { public BytesRef nestedTypePathAsBytes() {
return nestedTypePath; return nestedTypePathAsBytes;
}
public String nestedTypePathAsString() {
return nestedTypePathAsString;
} }
public final Dynamic dynamic() { 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 // 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 // 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) // 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); restoreDoc = context.switchDoc(nestedDoc);
context.addDoc(nestedDoc); context.addDoc(nestedDoc);
} }

View File

@ -20,12 +20,12 @@
package org.elasticsearch.index.search.nested; package org.elasticsearch.index.search.nested;
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.PrefixFilter;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
import java.io.IOException; import java.io.IOException;
@ -34,7 +34,7 @@ public class NestedDocsFilter extends Filter {
public static final NestedDocsFilter INSTANCE = new NestedDocsFilter(); 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(); private final int hashCode = filter.hashCode();

View File

@ -25,6 +25,7 @@ import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixFilter; import org.apache.lucene.search.PrefixFilter;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.common.lucene.docset.DocIdSets; import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
@ -35,7 +36,7 @@ public class NonNestedDocsFilter extends Filter {
public static final NonNestedDocsFilter INSTANCE = new NonNestedDocsFilter(); 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(); private final int hashCode = filter.hashCode();

View File

@ -81,7 +81,7 @@ public class NestedMappingTests {
.bytes()); .bytes());
assertThat(doc.docs().size(), equalTo(2)); 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.field1"), equalTo("1"));
assertThat(doc.docs().get(0).get("nested1.field2"), equalTo("2")); assertThat(doc.docs().get(0).get("nested1.field2"), equalTo("2"));
@ -99,10 +99,10 @@ public class NestedMappingTests {
.bytes()); .bytes());
assertThat(doc.docs().size(), equalTo(3)); 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.field1"), equalTo("3"));
assertThat(doc.docs().get(0).get("nested1.field2"), equalTo("4")); 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.field1"), equalTo("1"));
assertThat(doc.docs().get(1).get("nested1.field2"), equalTo("2")); assertThat(doc.docs().get(1).get("nested1.field2"), equalTo("2"));