diff --git a/src/main/java/org/elasticsearch/common/bytes/BytesReference.java b/src/main/java/org/elasticsearch/common/bytes/BytesReference.java index 67c4114ad19..b794fc3c143 100644 --- a/src/main/java/org/elasticsearch/common/bytes/BytesReference.java +++ b/src/main/java/org/elasticsearch/common/bytes/BytesReference.java @@ -137,55 +137,4 @@ public interface BytesReference { String toUtf8(); - // LUCENE 4 UPGRADE: Used by facets to order. Perhaps make this call implement Comparable. - public final static Comparator utf8SortedAsUnicodeSortOrder = new UTF8SortedAsUnicodeComparator(); - - public static class UTF8SortedAsUnicodeComparator implements Comparator { - - // Only singleton - private UTF8SortedAsUnicodeComparator() { - } - - public int compare(BytesReference a, BytesReference b) { - if (a.hasArray() && b.hasArray()) { - final byte[] aBytes = a.array(); - int aUpto = a.arrayOffset(); - final byte[] bBytes = b.array(); - int bUpto = b.arrayOffset(); - - final int aStop = aUpto + Math.min(a.length(), b.length()); - while (aUpto < aStop) { - int aByte = aBytes[aUpto++] & 0xff; - int bByte = bBytes[bUpto++] & 0xff; - - int diff = aByte - bByte; - if (diff != 0) { - return diff; - } - } - - // One is a prefix of the other, or, they are equal: - return a.length() - b.length(); - } else { - final byte[] aBytes = a.toBytes(); - int aUpto = 0; - final byte[] bBytes = b.toBytes(); - int bUpto = 0; - - final int aStop = aUpto + Math.min(a.length(), b.length()); - while (aUpto < aStop) { - int aByte = aBytes[aUpto++] & 0xff; - int bByte = bBytes[bUpto++] & 0xff; - - int diff = aByte - bByte; - if (diff != 0) { - return diff; - } - } - - // One is a prefix of the other, or, they are equal: - return a.length() - b.length(); - } - } - } } diff --git a/src/main/java/org/elasticsearch/common/text/BytesText.java b/src/main/java/org/elasticsearch/common/text/BytesText.java index 6059ad8a186..cf216b37550 100644 --- a/src/main/java/org/elasticsearch/common/text/BytesText.java +++ b/src/main/java/org/elasticsearch/common/text/BytesText.java @@ -22,6 +22,8 @@ package org.elasticsearch.common.text; import com.google.common.base.Charsets; import org.elasticsearch.common.bytes.BytesReference; +import java.util.Comparator; + /** * A {@link BytesReference} representation of the text, will always convert on the fly to a {@link String}. */ @@ -71,4 +73,10 @@ public class BytesText implements Text { public boolean equals(Object obj) { return bytes().equals(((Text) obj).bytes()); } -} + + @Override + public int compareTo(Text text) { + return UTF8SortedAsUnicodeComparator.utf8SortedAsUnicodeSortOrder.compare(text.bytes(), text.bytes()); + } + +} \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java b/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java index e825e8ad846..86a8ab8bd24 100644 --- a/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java +++ b/src/main/java/org/elasticsearch/common/text/StringAndBytesText.java @@ -97,4 +97,13 @@ public class StringAndBytesText implements Text { public boolean equals(Object obj) { return bytes().equals(((Text) obj).bytes()); } + + @Override + public int compareTo(Text other) { + if (text == null) { + return UTF8SortedAsUnicodeComparator.utf8SortedAsUnicodeSortOrder.compare(bytes, other.bytes()); + } else { + return text.compareTo(other.string()); + } + } } diff --git a/src/main/java/org/elasticsearch/common/text/StringText.java b/src/main/java/org/elasticsearch/common/text/StringText.java index 20183863b71..01840237911 100644 --- a/src/main/java/org/elasticsearch/common/text/StringText.java +++ b/src/main/java/org/elasticsearch/common/text/StringText.java @@ -83,4 +83,9 @@ public class StringText implements Text { // we use bytes here so we can be consistent with other text implementations return bytes().equals(((Text) obj).bytes()); } + + @Override + public int compareTo(Text text) { + return this.text.compareTo(text.string()); + } } diff --git a/src/main/java/org/elasticsearch/common/text/Text.java b/src/main/java/org/elasticsearch/common/text/Text.java index 7a77e8dd500..03dc18bfb7c 100644 --- a/src/main/java/org/elasticsearch/common/text/Text.java +++ b/src/main/java/org/elasticsearch/common/text/Text.java @@ -26,7 +26,7 @@ import org.elasticsearch.common.bytes.BytesReference; * so we can represent it in a more optimized manner in memory as well as serializing it over the * network as well as converting it to json format. */ -public interface Text { +public interface Text extends Comparable { /** * Are bytes available without the need to be converted into bytes when calling {@link #bytes()}. diff --git a/src/main/java/org/elasticsearch/common/text/UTF8SortedAsUnicodeComparator.java b/src/main/java/org/elasticsearch/common/text/UTF8SortedAsUnicodeComparator.java new file mode 100644 index 00000000000..e5af06060ed --- /dev/null +++ b/src/main/java/org/elasticsearch/common/text/UTF8SortedAsUnicodeComparator.java @@ -0,0 +1,58 @@ +package org.elasticsearch.common.text; + +import org.elasticsearch.common.bytes.BytesReference; + +import java.util.Comparator; + +// LUCENE 4 UPGRADE: Is this the right way of comparing bytesreferences inside Text instances? +// Copied from Lucene's BytesRef comparator +public class UTF8SortedAsUnicodeComparator implements Comparator { + + public final static Comparator utf8SortedAsUnicodeSortOrder = new UTF8SortedAsUnicodeComparator(); + + // Only singleton + private UTF8SortedAsUnicodeComparator() { + } + + public int compare(BytesReference a, BytesReference b) { + if (a.hasArray() && b.hasArray()) { + final byte[] aBytes = a.array(); + int aUpto = a.arrayOffset(); + final byte[] bBytes = b.array(); + int bUpto = b.arrayOffset(); + + final int aStop = aUpto + Math.min(a.length(), b.length()); + while (aUpto < aStop) { + int aByte = aBytes[aUpto++] & 0xff; + int bByte = bBytes[bUpto++] & 0xff; + + int diff = aByte - bByte; + if (diff != 0) { + return diff; + } + } + + // One is a prefix of the other, or, they are equal: + return a.length() - b.length(); + } else { + final byte[] aBytes = a.toBytes(); + int aUpto = 0; + final byte[] bBytes = b.toBytes(); + int bUpto = 0; + + final int aStop = aUpto + Math.min(a.length(), b.length()); + while (aUpto < aStop) { + int aByte = aBytes[aUpto++] & 0xff; + int bByte = bBytes[bUpto++] & 0xff; + + int diff = aByte - bByte; + if (diff != 0) { + return diff; + } + } + + // One is a prefix of the other, or, they are equal: + return a.length() - b.length(); + } + } +} diff --git a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacet.java index b67569be344..07e5d9ced08 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/TermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/TermsFacet.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.facet.terms; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.text.Text; import org.elasticsearch.search.facet.Facet; import java.util.Comparator; @@ -41,9 +42,9 @@ public interface TermsFacet extends Facet, Iterable { public interface Entry extends Comparable { - BytesReference term(); + Text term(); - BytesReference getTerm(); + Text getTerm(); Number termAsNumber(); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/bytes/InternalByteTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/bytes/InternalByteTermsFacet.java index dad3cbe0f28..df970366235 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/bytes/InternalByteTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/bytes/InternalByteTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.bytes; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TByteIntIterator; import gnu.trove.map.hash.TByteIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -74,11 +73,11 @@ public class InternalByteTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Short.toString(term)); + public Text term() { + return new StringText(Short.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/bytes/TermsByteFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/bytes/TermsByteFacetCollector.java index 92bfd60c4a1..59ff3a083d3 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/bytes/TermsByteFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/bytes/TermsByteFacetCollector.java @@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TByteIntIterator; import gnu.trove.map.hash.TByteIntHashMap; import gnu.trove.set.hash.TByteHashSet; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java index 4522b036a27..c7757132d27 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/doubles/InternalDoubleTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.doubles; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TDoubleIntIterator; import gnu.trove.map.hash.TDoubleIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -74,11 +73,11 @@ public class InternalDoubleTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Double.toString(term)); + public Text term() { + return new StringText(Double.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/doubles/TermsDoubleFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/doubles/TermsDoubleFacetCollector.java index 23968376171..bce37aaa9df 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/doubles/TermsDoubleFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/doubles/TermsDoubleFacetCollector.java @@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TDoubleIntIterator; import gnu.trove.map.hash.TDoubleIntHashMap; import gnu.trove.set.hash.TDoubleHashSet; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/floats/InternalFloatTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/floats/InternalFloatTermsFacet.java index 8dac5e38a67..b16424e7f94 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/floats/InternalFloatTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/floats/InternalFloatTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.floats; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TFloatIntIterator; import gnu.trove.map.hash.TFloatIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -74,11 +73,11 @@ public class InternalFloatTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Float.toString(term)); + public Text term() { + return new StringText(Float.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -282,7 +281,7 @@ public class InternalFloatTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -297,7 +296,7 @@ public class InternalFloatTermsFacet extends InternalTermsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/floats/TermsFloatFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/floats/TermsFloatFacetCollector.java index 19333a3426c..7c25fd7f13a 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/floats/TermsFloatFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/floats/TermsFloatFacetCollector.java @@ -24,9 +24,7 @@ import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TFloatIntIterator; import gnu.trove.map.hash.TFloatIntHashMap; import gnu.trove.set.hash.TFloatHashSet; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalArgumentException; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/ints/InternalIntTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/ints/InternalIntTermsFacet.java index b7738b7d193..644dc6acf40 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/ints/InternalIntTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/ints/InternalIntTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.ints; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TIntIntIterator; import gnu.trove.map.hash.TIntIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -74,11 +73,11 @@ public class InternalIntTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Integer.toString(term)); + public Text term() { + return new StringText(Integer.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -279,7 +278,7 @@ public class InternalIntTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -294,7 +293,7 @@ public class InternalIntTermsFacet extends InternalTermsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/ints/TermsIntFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/ints/TermsIntFacetCollector.java index e06ca78c9e2..be37b6da701 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/ints/TermsIntFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/ints/TermsIntFacetCollector.java @@ -24,9 +24,7 @@ import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TIntIntIterator; import gnu.trove.map.hash.TIntIntHashMap; import gnu.trove.set.hash.TIntHashSet; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalArgumentException; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/ip/InternalIpTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/ip/InternalIpTermsFacet.java index 447a4c6c341..7fb5dcb92aa 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/ip/InternalIpTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/ip/InternalIpTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.ip; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TLongIntIterator; import gnu.trove.map.hash.TLongIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.index.mapper.ip.IpFieldMapper; @@ -75,11 +74,11 @@ public class InternalIpTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(IpFieldMapper.longToIp(term)); + public Text term() { + return new StringText(IpFieldMapper.longToIp(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -284,7 +283,7 @@ public class InternalIpTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -299,7 +298,7 @@ public class InternalIpTermsFacet extends InternalTermsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/ip/TermsIpFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/ip/TermsIpFacetCollector.java index 9b2f0370e16..9a7f127e97f 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/ip/TermsIpFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/ip/TermsIpFacetCollector.java @@ -22,9 +22,7 @@ package org.elasticsearch.search.facet.terms.ip; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TLongIntIterator; import gnu.trove.map.hash.TLongIntHashMap; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Scorer; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.CacheRecycler; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java index 4a0ebdcfddd..f3c8d18a564 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/longs/InternalLongTermsFacet.java @@ -23,11 +23,11 @@ import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TLongIntIterator; import gnu.trove.map.hash.TLongIntHashMap; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -73,11 +73,11 @@ public class InternalLongTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Long.toString(term)); + public Text term() { + return new StringText(Long.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -281,7 +281,7 @@ public class InternalLongTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -296,7 +296,7 @@ public class InternalLongTermsFacet extends InternalTermsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/shorts/InternalShortTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/shorts/InternalShortTermsFacet.java index c199d631e28..e5cd4ace763 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/shorts/InternalShortTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/shorts/InternalShortTermsFacet.java @@ -22,13 +22,12 @@ package org.elasticsearch.search.facet.terms.shorts; import com.google.common.collect.ImmutableList; import gnu.trove.iterator.TShortIntIterator; import gnu.trove.map.hash.TShortIntHashMap; -import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -74,11 +73,11 @@ public class InternalShortTermsFacet extends InternalTermsFacet { this.count = count; } - public BytesReference term() { - return new BytesArray(Short.toString(term)); + public Text term() { + return new StringText(Short.toString(term)); } - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -279,7 +278,7 @@ public class InternalShortTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -294,7 +293,7 @@ public class InternalShortTermsFacet extends InternalTermsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/terms/shorts/TermsShortFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/shorts/TermsShortFacetCollector.java index a362b18a2f5..079ec61f15b 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/shorts/TermsShortFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/shorts/TermsShortFacetCollector.java @@ -24,9 +24,7 @@ import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TShortIntIterator; import gnu.trove.map.hash.TShortIntHashMap; import gnu.trove.set.hash.TShortHashSet; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; -import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalArgumentException; diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java index a6d217e0b64..cf5a19d99cd 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/InternalStringTermsFacet.java @@ -25,10 +25,12 @@ import gnu.trove.map.hash.TObjectIntHashMap; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.BytesText; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.search.facet.Facet; @@ -66,36 +68,36 @@ public class InternalStringTermsFacet extends InternalTermsFacet { public static class TermEntry implements Entry { - private BytesReference term; + private Text term; private int count; public TermEntry(String term, int count) { - this.term = new BytesArray(term); + this.term = new StringText(term); this.count = count; } public TermEntry(BytesRef term, int count) { - this.term = new BytesArray(term); + this.term = new BytesText(new BytesArray(term)); this.count = count; } - public TermEntry(BytesReference term, int count) { + public TermEntry(Text term, int count) { this.term = term; this.count = count; } - public BytesReference term() { + public Text term() { return term; } - public BytesReference getTerm() { + public Text getTerm() { return term; } @Override public Number termAsNumber() { // LUCENE 4 UPGRADE: better way? - return Double.parseDouble(term.toUtf8()); + return Double.parseDouble(term.string()); } @Override @@ -113,7 +115,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { @Override public int compareTo(Entry o) { - int i = BytesReference.utf8SortedAsUnicodeSortOrder.compare(this.term, o.term()); + int i = this.term.compareTo(o.term()); if (i == 0) { i = count - o.count(); if (i == 0) { @@ -227,7 +229,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { return facets.get(0); } InternalStringTermsFacet first = (InternalStringTermsFacet) facets.get(0); - TObjectIntHashMap aggregated = CacheRecycler.popObjectIntMap(); + TObjectIntHashMap aggregated = CacheRecycler.popObjectIntMap(); long missing = 0; long total = 0; for (Facet facet : facets) { @@ -240,7 +242,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { } BoundedTreeSet ordered = new BoundedTreeSet(first.comparatorType.comparator(), first.requiredSize); - for (TObjectIntIterator it = aggregated.iterator(); it.hasNext(); ) { + for (TObjectIntIterator it = aggregated.iterator(); it.hasNext(); ) { it.advance(); ordered.add(new TermEntry(it.key(), it.value())); } @@ -290,7 +292,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -299,7 +301,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { int size = in.readVInt(); entries = new ArrayList(size); for (int i = 0; i < size; i++) { - entries.add(new TermEntry(in.readBytesReference(), in.readVInt())); + entries.add(new TermEntry(in.readText(), in.readVInt())); } } @@ -313,7 +315,7 @@ public class InternalStringTermsFacet extends InternalTermsFacet { out.writeVInt(entries.size()); for (Entry entry : entries) { - out.writeBytesReference(entry.term()); + out.writeText(entry.term()); out.writeVInt(entry.count()); } } diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/ScriptTermsStringFieldFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/ScriptTermsStringFieldFacetCollector.java index bf2dc8e0898..48740b442a3 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/ScriptTermsStringFieldFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/ScriptTermsStringFieldFacetCollector.java @@ -135,7 +135,7 @@ public class ScriptTermsStringFieldFacetCollector extends AbstractFacetCollector } private boolean match(String value) { - if (excluded != null && excluded.contains(value)) { + if (excluded != null && excluded.contains(new BytesRef(value))) { return false; } if (matcher != null && !matcher.reset(value).matches()) { diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringFacetCollector.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringFacetCollector.java index 6706b0d6cd9..5c90c1c6d40 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringFacetCollector.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringFacetCollector.java @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import gnu.trove.iterator.TObjectIntIterator; import gnu.trove.map.hash.TObjectIntHashMap; -import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.Scorer; import org.apache.lucene.util.BytesRef; diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacet.java index d2b5d85e25c..7eaad2f47e0 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/TermsStatsFacet.java @@ -22,6 +22,7 @@ package org.elasticsearch.search.facet.termsstats; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.text.Text; import org.elasticsearch.search.facet.Facet; import java.util.Comparator; @@ -394,9 +395,9 @@ public interface TermsStatsFacet extends Facet, Iterable public interface Entry extends Comparable { - BytesReference term(); + Text term(); - BytesReference getTerm(); + Text getTerm(); Number termAsNumber(); diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java index 5c2894a8730..850d588b684 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/doubles/InternalTermsStatsDoubleFacet.java @@ -21,10 +21,10 @@ package org.elasticsearch.search.facet.termsstats.doubles; import com.google.common.collect.ImmutableList; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.trove.ExtTDoubleObjectHashMap; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; @@ -76,12 +76,12 @@ public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { } @Override - public BytesReference term() { - return new BytesArray(Double.toString(term)); + public Text term() { + return new StringText(Double.toString(term)); } @Override - public BytesReference getTerm() { + public Text getTerm() { return term(); } @@ -341,7 +341,7 @@ public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { @Override public void readFrom(StreamInput in) throws IOException { - name = in.readUTF(); + name = in.readString(); comparatorType = ComparatorType.fromId(in.readByte()); requiredSize = in.readVInt(); missing = in.readVLong(); @@ -355,7 +355,7 @@ public class InternalTermsStatsDoubleFacet extends InternalTermsStatsFacet { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeUTF(name); + out.writeString(name); out.writeByte(comparatorType.id()); out.writeVInt(requiredSize); out.writeVLong(missing); diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java index 0e77fd785da..48edaaed09f 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/longs/InternalTermsStatsLongFacet.java @@ -21,10 +21,10 @@ package org.elasticsearch.search.facet.termsstats.longs; import com.google.common.collect.ImmutableList; import org.elasticsearch.common.CacheRecycler; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.trove.ExtTLongObjectHashMap; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; @@ -76,12 +76,12 @@ public class InternalTermsStatsLongFacet extends InternalTermsStatsFacet { } @Override - public BytesReference term() { - return new BytesArray(Long.toString(term)); + public Text term() { + return new StringText(Long.toString(term)); } @Override - public BytesReference getTerm() { + public Text getTerm() { return term(); } diff --git a/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java b/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java index 8c5150a5860..6ac49a81810 100644 --- a/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java +++ b/src/main/java/org/elasticsearch/search/facet/termsstats/strings/InternalTermsStatsStringFacet.java @@ -26,6 +26,8 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.text.BytesText; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.trove.ExtTHashMap; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; @@ -60,7 +62,7 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { public static class StringEntry implements Entry { - BytesReference term; + Text term; long count; long totalCount; double total; @@ -68,10 +70,10 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { double max; public StringEntry(BytesRef term, long count, long totalCount, double total, double min, double max) { - this(new BytesArray(term), count, totalCount, total, min, max); + this(new BytesText(new BytesArray(term)), count, totalCount, total, min, max); } - public StringEntry(BytesReference term, long count, long totalCount, double total, double min, double max) { + public StringEntry(Text term, long count, long totalCount, double total, double min, double max) { this.term = term; this.count = count; this.totalCount = totalCount; @@ -81,18 +83,18 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { } @Override - public BytesReference term() { + public Text term() { return term; } @Override - public BytesReference getTerm() { + public Text getTerm() { return term(); } @Override public Number termAsNumber() { - return Double.parseDouble(term.toUtf8()); + return Double.parseDouble(term.string()); } @Override @@ -164,8 +166,8 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { } @Override - public int compareTo(Entry o) { - return BytesReference.utf8SortedAsUnicodeSortOrder.compare(this.term, o.term()); + public int compareTo(Entry other) { + return term.compareTo(other.term()); } } @@ -257,7 +259,7 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { return facets.get(0); } int missing = 0; - ExtTHashMap map = CacheRecycler.popHashMap(); + ExtTHashMap map = CacheRecycler.popHashMap(); for (Facet facet : facets) { InternalTermsStatsStringFacet tsFacet = (InternalTermsStatsStringFacet) facet; missing += tsFacet.missing; @@ -353,7 +355,7 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { int size = in.readVInt(); entries = new ArrayList(size); for (int i = 0; i < size; i++) { - entries.add(new StringEntry(in.readBytesReference(), in.readVLong(), in.readVLong(), in.readDouble(), in.readDouble(), in.readDouble())); + entries.add(new StringEntry(in.readText(), in.readVLong(), in.readVLong(), in.readDouble(), in.readDouble(), in.readDouble())); } } @@ -366,7 +368,7 @@ public class InternalTermsStatsStringFacet extends InternalTermsStatsFacet { out.writeVInt(entries.size()); for (Entry entry : entries) { - out.writeBytesReference(entry.term()); + out.writeText(entry.term()); out.writeVLong(entry.count()); out.writeVLong(entry.totalCount()); out.writeDouble(entry.total());