List of usage examples for org.apache.lucene.util BytesRef compareTo
@Override public int compareTo(BytesRef other)
From source file:org.elasticsearch.index.mapper.BinaryRangeUtilTests.java
License:Apache License
public void testEncode_double() { int iters = randomIntBetween(32, 1024); for (int i = 0; i < iters; i++) { double number1 = randomDouble(); BytesRef encodedNumber1 = new BytesRef(BinaryRangeUtil.encodeDouble(number1)); double number2 = randomBoolean() ? Math.nextUp(number1) : randomDouble(); BytesRef encodedNumber2 = new BytesRef(BinaryRangeUtil.encodeDouble(number2)); assertEquals(8, encodedNumber1.length); assertEquals(8, encodedNumber2.length); int cmp = normalize(Double.compare(number1, number2)); assertEquals(cmp, normalize(encodedNumber1.compareTo(encodedNumber2))); cmp = normalize(Double.compare(number2, number1)); assertEquals(cmp, normalize(encodedNumber2.compareTo(encodedNumber1))); }/*w w w . jav a2 s . c o m*/ }
From source file:org.elasticsearch.index.mapper.BinaryRangeUtilTests.java
License:Apache License
public void testEncode_Float() { int iters = randomIntBetween(32, 1024); for (int i = 0; i < iters; i++) { float number1 = randomFloat(); BytesRef encodedNumber1 = new BytesRef(BinaryRangeUtil.encodeFloat(number1)); float number2 = randomBoolean() ? Math.nextUp(number1) : randomFloat(); BytesRef encodedNumber2 = new BytesRef(BinaryRangeUtil.encodeFloat(number2)); assertEquals(4, encodedNumber1.length); assertEquals(4, encodedNumber2.length); int cmp = normalize(Double.compare(number1, number2)); assertEquals(cmp, normalize(encodedNumber1.compareTo(encodedNumber2))); cmp = normalize(Double.compare(number2, number1)); assertEquals(cmp, normalize(encodedNumber2.compareTo(encodedNumber1))); }//from ww w . j av a 2 s.co m }
From source file:org.elasticsearch.search.aggregations.bucket.composite.BinaryValuesSource.java
License:Apache License
int compareValues(BytesRef v1, BytesRef v2) { return v1.compareTo(v2) * reverseMul; }
From source file:org.elasticsearch.search.aggregations.bucket.composite.TermsSortedDocsProducer.java
License:Apache License
@Override DocIdSet processLeaf(Query query, CompositeValuesCollectorQueue queue, LeafReaderContext context, boolean fillDocIdSet) throws IOException { final Terms terms = context.reader().terms(field); if (terms == null) { // no value for the field return DocIdSet.EMPTY; }//w w w .j a va 2s.c o m BytesRef lowerValue = (BytesRef) queue.getLowerValueLeadSource(); BytesRef upperValue = (BytesRef) queue.getUpperValueLeadSource(); final TermsEnum te = terms.iterator(); if (lowerValue != null) { if (te.seekCeil(lowerValue) == TermsEnum.SeekStatus.END) { return DocIdSet.EMPTY; } } else { if (te.next() == null) { return DocIdSet.EMPTY; } } DocIdSetBuilder builder = fillDocIdSet ? new DocIdSetBuilder(context.reader().maxDoc(), terms) : null; PostingsEnum reuse = null; boolean first = true; do { if (upperValue != null && upperValue.compareTo(te.term()) < 0) { break; } reuse = te.postings(reuse, PostingsEnum.NONE); if (processBucket(queue, context, reuse, te.term(), builder) && !first) { // this bucket does not have any competitive composite buckets, // we can early terminate the collection because the remaining buckets are guaranteed // to be greater than this bucket. break; } first = false; } while (te.next() != null); return fillDocIdSet ? builder.build() : DocIdSet.EMPTY; }
From source file:org.elasticsearch.search.aggregations.bucket.range.IpRangeAggregatorTests.java
License:Apache License
private static boolean isInRange(BytesRef value, BytesRef from, BytesRef to) { if ((to == null || to.compareTo(value) > 0) && (from == null || from.compareTo(value) <= 0)) { return true; }/*from ww w . j av a 2s . c o m*/ return false; }
From source file:org.elasticsearch.search.controller.ShardFieldDocSortedHitQueue.java
License:Apache License
/** * Returns whether <code>a</code> is less relevant than <code>b</code>. * * @param docA ScoreDoc//from ww w . j a v a2s . c om * @param docB ScoreDoc * @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>. */ @SuppressWarnings("unchecked") @Override protected final boolean lessThan(final FieldDoc docA, final FieldDoc docB) { final int n = fields.length; int c = 0; for (int i = 0; i < n && c == 0; ++i) { final SortField.Type type = fields[i].getType(); if (type == SortField.Type.STRING) { final BytesRef s1 = (BytesRef) docA.fields[i]; final BytesRef s2 = (BytesRef) docB.fields[i]; // null values need to be sorted first, because of how FieldCache.getStringIndex() // works - in that routine, any documents without a value in the given field are // put first. If both are null, the next SortField is used if (s1 == null) { c = (s2 == null) ? 0 : -1; } else if (s2 == null) { c = 1; } else { //if (fields[i].getLocale() == null) { c = s1.compareTo(s2); } // } else { // c = collators[i].compare(s1, s2); // } } else { c = comparators[i].compareValues(docA.fields[i], docB.fields[i]); } // reverse sort if (fields[i].getReverse()) { c = -c; } } // avoid random sort order that could lead to duplicates (bug #31241): if (c == 0) { // CHANGE: Add shard base tie breaking c = docA.shardIndex - docB.shardIndex; if (c == 0) { return docA.doc > docB.doc; } } return c > 0; }
From source file:org.elasticsearch.search.MultiValueModeTests.java
License:Apache License
private void verify(SortedBinaryDocValues values, int maxDoc) { for (BytesRef missingValue : new BytesRef[] { new BytesRef(), new BytesRef(RandomStrings.randomAsciiOfLength(getRandom(), 8)) }) { for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX }) { final BinaryDocValues selected = mode.select(values, missingValue); for (int i = 0; i < maxDoc; ++i) { final BytesRef actual = selected.get(i); BytesRef expected = null; values.setDocument(i);//w w w. ja v a2 s . co m int numValues = values.count(); if (numValues == 0) { expected = missingValue; } else { for (int j = 0; j < numValues; ++j) { if (expected == null) { expected = BytesRef.deepCopyOf(values.valueAt(j)); } else { if (mode == MultiValueMode.MIN) { expected = expected.compareTo(values.valueAt(j)) <= 0 ? expected : BytesRef.deepCopyOf(values.valueAt(j)); } else if (mode == MultiValueMode.MAX) { expected = expected.compareTo(values.valueAt(j)) > 0 ? expected : BytesRef.deepCopyOf(values.valueAt(j)); } } } if (expected == null) { expected = missingValue; } } assertEquals(mode.toString() + " docId=" + i, expected, actual); } } } }
From source file:org.elasticsearch.search.MultiValueModeTests.java
License:Apache License
private void verify(SortedBinaryDocValues values, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs) throws IOException { for (BytesRef missingValue : new BytesRef[] { new BytesRef(), new BytesRef(RandomStrings.randomAsciiOfLength(getRandom(), 8)) }) { for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX }) { final BinaryDocValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc); int prevRoot = -1; for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {// w w w .j av a 2s .co m final BytesRef actual = selected.get(root); BytesRef expected = null; for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) { values.setDocument(child); for (int j = 0; j < values.count(); ++j) { if (expected == null) { expected = BytesRef.deepCopyOf(values.valueAt(j)); } else { if (mode == MultiValueMode.MIN) { expected = expected.compareTo(values.valueAt(j)) <= 0 ? expected : BytesRef.deepCopyOf(values.valueAt(j)); } else if (mode == MultiValueMode.MAX) { expected = expected.compareTo(values.valueAt(j)) > 0 ? expected : BytesRef.deepCopyOf(values.valueAt(j)); } } } } if (expected == null) { expected = missingValue; } assertEquals(mode.toString() + " docId=" + root, expected, actual); prevRoot = root; } } } }
From source file:org.fastcatsearch.ir.sort.DataAscSortFunction.java
License:Apache License
@Override public int compare(BytesRef one, BytesRef two) { // 0? ? ? . return one.compareTo(two); }
From source file:org.fastcatsearch.ir.sort.DataDescSortFunction.java
License:Apache License
@Override public int compare(BytesRef one, BytesRef two) { return two.compareTo(one); }