List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.elasticsearch.index.fielddata.AbstractNumericFieldDataTests.java
License:Apache License
@Test public void testSortMultiValuesFields() throws Exception { fillExtendedMvSet();/*from w w w . j a va 2s . c o m*/ IndexFieldData indexFieldData = getForField("value"); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN)))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-10)); assertThat(topDocs.scoreDocs[1].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2)); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[3].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(4)); assertThat(topDocs.scoreDocs[4].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(6)); assertThat(topDocs.scoreDocs[5].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(8)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(10)); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(8)); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(6)); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(4)); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[5].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-8)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); searcher = new IndexSearcher(DirectoryReader.open(writer, true)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.SUM)))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-27)); assertThat(topDocs.scoreDocs[1].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[2].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(6)); assertThat(topDocs.scoreDocs[3].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(15)); assertThat(topDocs.scoreDocs[4].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(21)); assertThat(topDocs.scoreDocs[5].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(27)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); searcher = new IndexSearcher(DirectoryReader.open(writer, true)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.SUM), true))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(27)); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(21)); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(15)); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(6)); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[5].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-27)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); searcher = new IndexSearcher(DirectoryReader.open(writer, true)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.AVG)))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-9)); assertThat(topDocs.scoreDocs[1].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[3].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(5)); assertThat(topDocs.scoreDocs[4].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(7)); assertThat(topDocs.scoreDocs[5].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(9)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); searcher = new IndexSearcher(DirectoryReader.open(writer, true)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.AVG), true))); // defaults to _last assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(6)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(9)); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(7)); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(5)); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3)); assertThat(topDocs.scoreDocs[5].doc, equalTo(7)); assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).intValue(), equalTo(-9)); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); // assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); // assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource("_first", SortMode.MIN)))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(1)); assertThat(topDocs.scoreDocs[1].doc, equalTo(5)); assertThat(topDocs.scoreDocs[2].doc, equalTo(7)); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(topDocs.scoreDocs[5].doc, equalTo(3)); assertThat(topDocs.scoreDocs[6].doc, equalTo(4)); assertThat(topDocs.scoreDocs[7].doc, equalTo(6)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource("_first", SortMode.MAX), true))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(1)); assertThat(topDocs.scoreDocs[1].doc, equalTo(5)); assertThat(topDocs.scoreDocs[2].doc, equalTo(6)); assertThat(topDocs.scoreDocs[3].doc, equalTo(4)); assertThat(topDocs.scoreDocs[4].doc, equalTo(3)); assertThat(topDocs.scoreDocs[5].doc, equalTo(0)); assertThat(topDocs.scoreDocs[6].doc, equalTo(2)); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource("-9", SortMode.MIN)))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(7)); assertThat(topDocs.scoreDocs[1].doc, equalTo(1)); assertThat(topDocs.scoreDocs[2].doc, equalTo(5)); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(topDocs.scoreDocs[5].doc, equalTo(3)); assertThat(topDocs.scoreDocs[6].doc, equalTo(4)); assertThat(topDocs.scoreDocs[7].doc, equalTo(6)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource("9", SortMode.MAX), true))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(6)); assertThat(topDocs.scoreDocs[1].doc, equalTo(1)); assertThat(topDocs.scoreDocs[2].doc, equalTo(5)); assertThat(topDocs.scoreDocs[3].doc, equalTo(4)); assertThat(topDocs.scoreDocs[4].doc, equalTo(3)); assertThat(topDocs.scoreDocs[5].doc, equalTo(0)); assertThat(topDocs.scoreDocs[6].doc, equalTo(2)); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTestCase.java
License:Apache License
public void testActualMissingValue(boolean reverse) throws IOException { // missing value is set to an actual value final String[] values = new String[randomIntBetween(2, 30)]; for (int i = 1; i < values.length; ++i) { values[i] = TestUtil.randomUnicodeString(getRandom()); }/*from ww w .ja v a 2 s . c om*/ final int numDocs = scaledRandomIntBetween(10, 3072); for (int i = 0; i < numDocs; ++i) { final String value = RandomPicks.randomFrom(getRandom(), values); if (value == null) { writer.addDocument(new Document()); } else { Document d = new Document(); addField(d, "value", value); writer.addDocument(d); } if (randomInt(10) == 0) { writer.commit(); } } final IndexFieldData indexFieldData = getForField("value"); final String missingValue = values[1]; IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); XFieldComparatorSource comparator = indexFieldData.comparatorSource(missingValue, MultiValueMode.MIN, null); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), randomBoolean() ? numDocs : randomIntBetween(10, numDocs), new Sort(new SortField("value", comparator, reverse))); assertEquals(numDocs, topDocs.totalHits); BytesRef previousValue = reverse ? UnicodeUtil.BIG_TERM : new BytesRef(); for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final String docValue = searcher.doc(topDocs.scoreDocs[i].doc).get("value"); final BytesRef value = new BytesRef(docValue == null ? missingValue : docValue); if (reverse) { assertTrue(previousValue.compareTo(value) >= 0); } else { assertTrue(previousValue.compareTo(value) <= 0); } previousValue = value; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTestCase.java
License:Apache License
public void testSortMissing(boolean first, boolean reverse) throws IOException { final String[] values = new String[randomIntBetween(2, 10)]; for (int i = 1; i < values.length; ++i) { values[i] = TestUtil.randomUnicodeString(getRandom()); }//from w w w.j ava 2s . c o m final int numDocs = scaledRandomIntBetween(10, 3072); for (int i = 0; i < numDocs; ++i) { final String value = RandomPicks.randomFrom(getRandom(), values); if (value == null) { writer.addDocument(new Document()); } else { Document d = new Document(); addField(d, "value", value); writer.addDocument(d); } if (randomInt(10) == 0) { writer.commit(); } } final IndexFieldData indexFieldData = getForField("value"); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); XFieldComparatorSource comparator = indexFieldData.comparatorSource(first ? "_first" : "_last", MultiValueMode.MIN, null); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), randomBoolean() ? numDocs : randomIntBetween(10, numDocs), new Sort(new SortField("value", comparator, reverse))); assertEquals(numDocs, topDocs.totalHits); BytesRef previousValue = first ? null : reverse ? UnicodeUtil.BIG_TERM : new BytesRef(); for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final String docValue = searcher.doc(topDocs.scoreDocs[i].doc).get("value"); if (first && docValue == null) { assertNull(previousValue); } else if (!first && docValue != null) { assertNotNull(previousValue); } final BytesRef value = docValue == null ? null : new BytesRef(docValue); if (previousValue != null && value != null) { if (reverse) { assertTrue(previousValue.compareTo(value) >= 0); } else { assertTrue(previousValue.compareTo(value) <= 0); } } previousValue = value; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTestCase.java
License:Apache License
public void testNestedSorting(MultiValueMode sortMode) throws IOException { final String[] values = new String[randomIntBetween(2, 20)]; for (int i = 0; i < values.length; ++i) { values[i] = TestUtil.randomSimpleString(getRandom()); }//from ww w . j a v a 2 s . c om final int numParents = scaledRandomIntBetween(10, 3072); List<Document> docs = new ArrayList<>(); FixedBitSet parents = new FixedBitSet(64); for (int i = 0; i < numParents; ++i) { docs.clear(); final int numChildren = randomInt(4); for (int j = 0; j < numChildren; ++j) { final Document child = new Document(); final int numValues = randomInt(3); for (int k = 0; k < numValues; ++k) { final String value = RandomPicks.randomFrom(getRandom(), values); addField(child, "text", value); } docs.add(child); } final Document parent = new Document(); parent.add(new StringField("type", "parent", Store.YES)); final String value = RandomPicks.randomFrom(getRandom(), values); if (value != null) { addField(parent, "text", value); } docs.add(parent); int bit = parents.prevSetBit(parents.length() - 1) + docs.size(); parents = FixedBitSet.ensureCapacity(parents, bit); parents.set(bit); writer.addDocuments(docs); if (randomInt(10) == 0) { writer.commit(); } } DirectoryReader directoryReader = DirectoryReader.open(writer, true); directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("test"), 0)); IndexSearcher searcher = new IndexSearcher(directoryReader); IndexFieldData<?> fieldData = getForField("text"); final Object missingValue; switch (randomInt(4)) { case 0: missingValue = "_first"; break; case 1: missingValue = "_last"; break; case 2: missingValue = new BytesRef(RandomPicks.randomFrom(getRandom(), values)); break; default: missingValue = new BytesRef(TestUtil.randomSimpleString(getRandom())); break; } Query parentFilter = new TermQuery(new Term("type", "parent")); Query childFilter = Queries.not(parentFilter); Nested nested = createNested(searcher, parentFilter, childFilter); BytesRefFieldComparatorSource nestedComparatorSource = new BytesRefFieldComparatorSource(fieldData, missingValue, sortMode, nested); ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("text", nestedComparatorSource)); TopFieldDocs topDocs = searcher.search(query, randomIntBetween(1, numParents), sort); assertTrue(topDocs.scoreDocs.length > 0); BytesRef previous = null; for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final int docID = topDocs.scoreDocs[i].doc; assertTrue("expected " + docID + " to be a parent", parents.get(docID)); BytesRef cmpValue = null; for (int child = parents.prevSetBit(docID - 1) + 1; child < docID; ++child) { String[] sVals = searcher.doc(child).getValues("text"); final BytesRef[] vals; if (sVals.length == 0) { vals = new BytesRef[0]; } else { vals = new BytesRef[sVals.length]; for (int j = 0; j < vals.length; ++j) { vals[j] = new BytesRef(sVals[j]); } } for (BytesRef value : vals) { if (cmpValue == null) { cmpValue = value; } else if (sortMode == MultiValueMode.MIN && value.compareTo(cmpValue) < 0) { cmpValue = value; } else if (sortMode == MultiValueMode.MAX && value.compareTo(cmpValue) > 0) { cmpValue = value; } } } if (cmpValue == null) { if ("_first".equals(missingValue)) { cmpValue = new BytesRef(); } else if ("_last".equals(missingValue) == false) { cmpValue = (BytesRef) missingValue; } } if (previous != null && cmpValue != null) { assertTrue(previous.utf8ToString() + " / " + cmpValue.utf8ToString(), previous.compareTo(cmpValue) <= 0); } previous = cmpValue; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTests.java
License:Apache License
public void testActualMissingValue(boolean reverse) throws IOException { // missing value is set to an actual value Document d = new Document(); final StringField s = new StringField("value", "", Field.Store.YES); d.add(s);// ww w. ja v a 2 s .c o m final String[] values = new String[randomIntBetween(2, 30)]; for (int i = 1; i < values.length; ++i) { values[i] = _TestUtil.randomUnicodeString(getRandom()); } final int numDocs = atLeast(100); for (int i = 0; i < numDocs; ++i) { final String value = RandomPicks.randomFrom(getRandom(), values); if (value == null) { writer.addDocument(new Document()); } else { s.setStringValue(value); writer.addDocument(d); } if (randomInt(10) == 0) { writer.commit(); } } final IndexFieldData indexFieldData = getForField("value"); final String missingValue = values[1]; IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); XFieldComparatorSource comparator = indexFieldData.comparatorSource(missingValue, SortMode.MIN); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), randomBoolean() ? numDocs : randomIntBetween(10, numDocs), new Sort(new SortField("value", comparator, reverse))); assertEquals(numDocs, topDocs.totalHits); BytesRef previousValue = reverse ? UnicodeUtil.BIG_TERM : new BytesRef(); for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final String docValue = searcher.doc(topDocs.scoreDocs[i].doc).get("value"); final BytesRef value = new BytesRef(docValue == null ? missingValue : docValue); if (reverse) { assertTrue(previousValue.compareTo(value) >= 0); } else { assertTrue(previousValue.compareTo(value) <= 0); } previousValue = value; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTests.java
License:Apache License
public void testSortMissing(boolean first, boolean reverse) throws IOException { Document d = new Document(); final StringField s = new StringField("value", "", Field.Store.YES); d.add(s);//from w ww . java 2s. co m final String[] values = new String[randomIntBetween(2, 10)]; for (int i = 1; i < values.length; ++i) { values[i] = _TestUtil.randomUnicodeString(getRandom()); } final int numDocs = atLeast(100); for (int i = 0; i < numDocs; ++i) { final String value = RandomPicks.randomFrom(getRandom(), values); if (value == null) { writer.addDocument(new Document()); } else { s.setStringValue(value); writer.addDocument(d); } if (randomInt(10) == 0) { writer.commit(); } } final IndexFieldData indexFieldData = getForField("value"); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); XFieldComparatorSource comparator = indexFieldData.comparatorSource(first ? "_first" : "_last", SortMode.MIN); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), randomBoolean() ? numDocs : randomIntBetween(10, numDocs), new Sort(new SortField("value", comparator, reverse))); assertEquals(numDocs, topDocs.totalHits); BytesRef previousValue = first ? null : reverse ? UnicodeUtil.BIG_TERM : new BytesRef(); for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final String docValue = searcher.doc(topDocs.scoreDocs[i].doc).get("value"); if (first && docValue == null) { assertNull(previousValue); } else if (!first && docValue != null) { assertNotNull(previousValue); } final BytesRef value = docValue == null ? null : new BytesRef(docValue); if (previousValue != null && value != null) { if (reverse) { assertTrue(previousValue.compareTo(value) >= 0); } else { assertTrue(previousValue.compareTo(value) <= 0); } } previousValue = value; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTests.java
License:Apache License
public void testNestedSorting(SortMode sortMode) throws IOException { final String[] values = new String[randomIntBetween(2, 20)]; for (int i = 0; i < values.length; ++i) { values[i] = _TestUtil.randomSimpleString(getRandom()); }/*from w w w. j ava 2 s. c om*/ final int numParents = atLeast(100); List<Document> docs = new ArrayList<Document>(); final OpenBitSet parents = new OpenBitSet(); for (int i = 0; i < numParents; ++i) { docs.clear(); final int numChildren = randomInt(4); for (int j = 0; j < numChildren; ++j) { final Document child = new Document(); final int numValues = randomInt(3); for (int k = 0; k < numValues; ++k) { final String value = RandomPicks.randomFrom(getRandom(), values); child.add(new StringField("text", value, Store.YES)); } docs.add(child); } final Document parent = new Document(); parent.add(new StringField("type", "parent", Store.YES)); final String value = RandomPicks.randomFrom(getRandom(), values); if (value != null) { parent.add(new StringField("text", value, Store.YES)); } docs.add(parent); parents.set(parents.prevSetBit(parents.length() - 1) + docs.size()); writer.addDocuments(docs); if (randomInt(10) == 0) { writer.commit(); } } IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); IndexFieldData<?> fieldData = getForField("text"); final BytesRef missingValue; switch (randomInt(4)) { case 0: missingValue = new BytesRef(); break; case 1: missingValue = BytesRefFieldComparatorSource.MAX_TERM; break; case 2: missingValue = new BytesRef(RandomPicks.randomFrom(getRandom(), values)); break; default: missingValue = new BytesRef(_TestUtil.randomSimpleString(getRandom())); break; } BytesRefFieldComparatorSource innerSource = new BytesRefFieldComparatorSource(fieldData, missingValue, sortMode); Filter parentFilter = new TermFilter(new Term("type", "parent")); Filter childFilter = new NotFilter(parentFilter); NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerSource, parentFilter, childFilter); ToParentBlockJoinQuery query = new ToParentBlockJoinQuery( new XFilteredQuery(new MatchAllDocsQuery(), childFilter), new FixedBitSetCachingWrapperFilter(parentFilter), ScoreMode.None); Sort sort = new Sort(new SortField("text", nestedComparatorSource)); TopFieldDocs topDocs = searcher.search(query, randomIntBetween(1, numParents), sort); assertTrue(topDocs.scoreDocs.length > 0); BytesRef previous = null; for (int i = 0; i < topDocs.scoreDocs.length; ++i) { final int docID = topDocs.scoreDocs[i].doc; assertTrue("expected " + docID + " to be a parent", parents.get(docID)); BytesRef cmpValue = null; for (int child = parents.prevSetBit(docID - 1) + 1; child < docID; ++child) { String[] vals = searcher.doc(child).getValues("text"); if (vals.length == 0) { vals = new String[] { missingValue.utf8ToString() }; } for (String value : vals) { final BytesRef bytesValue = new BytesRef(value); if (cmpValue == null) { cmpValue = bytesValue; } else if (sortMode == SortMode.MIN && bytesValue.compareTo(cmpValue) < 0) { cmpValue = bytesValue; } else if (sortMode == SortMode.MAX && bytesValue.compareTo(cmpValue) > 0) { cmpValue = bytesValue; } } } if (cmpValue == null) { cmpValue = missingValue; } if (previous != null) { assertNotNull(cmpValue); assertTrue(previous.utf8ToString() + " / " + cmpValue.utf8ToString(), previous.compareTo(cmpValue) <= 0); } previous = cmpValue; } searcher.getIndexReader().close(); }
From source file:org.elasticsearch.index.fielddata.ParentChildFieldDataTests.java
License:Apache License
@Test public void testSorting() throws Exception { IndexFieldData indexFieldData = getForField(childType); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true)); IndexFieldData.XFieldComparatorSource comparator = indexFieldData.comparatorSource("_last", SortMode.MIN); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.NAME, comparator, false))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[1].doc, equalTo(1)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[3].doc, equalTo(5)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[4].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[5].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[6].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0]), equalTo(IndexFieldData.XFieldComparatorSource.MAX_TERM)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.NAME, comparator, true))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[2].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[4].doc, equalTo(1)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[5].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[6].doc, equalTo(5)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], nullValue()); }
From source file:org.elasticsearch.index.IndexSortConfig.java
License:Apache License
/** * Builds the {@link Sort} order from the settings for this index * or returns null if this index has no sort. *///from w ww .j a v a 2s.c o m public Sort buildIndexSort(Function<String, MappedFieldType> fieldTypeLookup, Function<MappedFieldType, IndexFieldData<?>> fieldDataLookup) { if (hasIndexSort() == false) { return null; } final SortField[] sortFields = new SortField[sortSpecs.length]; for (int i = 0; i < sortSpecs.length; i++) { FieldSortSpec sortSpec = sortSpecs[i]; final MappedFieldType ft = fieldTypeLookup.apply(sortSpec.field); if (ft == null) { throw new IllegalArgumentException("unknown index sort field:[" + sortSpec.field + "]"); } boolean reverse = sortSpec.order == null ? false : (sortSpec.order == SortOrder.DESC); MultiValueMode mode = sortSpec.mode; if (mode == null) { mode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN; } IndexFieldData<?> fieldData; try { fieldData = fieldDataLookup.apply(ft); } catch (Exception e) { throw new IllegalArgumentException( "docvalues not found for index sort field:[" + sortSpec.field + "]"); } if (fieldData == null) { throw new IllegalArgumentException( "docvalues not found for index sort field:[" + sortSpec.field + "]"); } sortFields[i] = fieldData.sortField(sortSpec.missingValue, mode, null, reverse); validateIndexSortField(sortFields[i]); } return new Sort(sortFields); }
From source file:org.elasticsearch.index.query.TermsSetQueryBuilderTests.java
License:Apache License
public void testDoToQuery() throws Exception { try (Directory directory = newDirectory()) { IndexWriterConfig config = new IndexWriterConfig(new WhitespaceAnalyzer()); config.setMergePolicy(NoMergePolicy.INSTANCE); try (IndexWriter iw = new IndexWriter(directory, config)) { Document document = new Document(); document.add(new TextField("message", "a b", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 1)); iw.addDocument(document);//from w ww. jav a2s. c o m document = new Document(); document.add(new TextField("message", "a b c", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 1)); iw.addDocument(document); document = new Document(); document.add(new TextField("message", "a b c", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 2)); iw.addDocument(document); document = new Document(); document.add(new TextField("message", "a b c d", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 1)); iw.addDocument(document); document = new Document(); document.add(new TextField("message", "a b c d", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 2)); iw.addDocument(document); document = new Document(); document.add(new TextField("message", "a b c d", Field.Store.NO)); document.add(new SortedNumericDocValuesField("m_s_m", 3)); iw.addDocument(document); } try (IndexReader ir = DirectoryReader.open(directory)) { QueryShardContext context = createShardContext(); Query query = new TermsSetQueryBuilder("message", Arrays.asList("c", "d")) .setMinimumShouldMatchField("m_s_m").doToQuery(context); IndexSearcher searcher = new IndexSearcher(ir); TopDocs topDocs = searcher.search(query, 10, new Sort(SortField.FIELD_DOC)); assertThat(topDocs.totalHits, equalTo(3L)); assertThat(topDocs.scoreDocs[0].doc, equalTo(1)); assertThat(topDocs.scoreDocs[1].doc, equalTo(3)); assertThat(topDocs.scoreDocs[2].doc, equalTo(4)); } } }