use of org.apache.lucene.search.SortedSetSortField in project neo4j by neo4j.
the class QueryContext method sort.
/**
* Returns a QueryContext with sorting added to it.
*
* @param key The key to sort on.
* @param additionalKeys Any additional keys to sort on.
* @return A QueryContext with sorting added to it.
*/
public QueryContext sort(String key, String... additionalKeys) {
SortField firstSortField = new SortedSetSortField(key, false);
if (additionalKeys.length == 0) {
return sort(new Sort(firstSortField));
}
SortField[] sortFields = new SortField[1 + additionalKeys.length];
sortFields[0] = firstSortField;
for (int i = 0; i < additionalKeys.length; i++) {
sortFields[1 + i] = new SortedSetSortField(additionalKeys[i], false);
}
return sort(new Sort(sortFields));
}
use of org.apache.lucene.search.SortedSetSortField in project neo4j by neo4j.
the class TestLuceneIndex method queryAndSortNodesByStringProperty.
private void queryAndSortNodesByStringProperty(Index<Node> index, String stringProperty, Function<Integer, String> expectedValueProvider) {
try (Transaction transaction = graphDb.beginTx()) {
QueryContext queryContext = new QueryContext(stringProperty + ":**");
queryContext.sort(new Sort(new SortedSetSortField(stringProperty, true)));
IndexHits<Node> nodes = index.query(queryContext);
int nodeIndex = 0;
for (Node node : nodes) {
assertEquals("Nodes should be sorted by string property", expectedValueProvider.apply(nodeIndex++), node.getProperty(stringProperty));
}
transaction.success();
}
}
use of org.apache.lucene.search.SortedSetSortField in project lucene-solr by apache.
the class TestIndexSorting method testMissingMultiValuedStringFirst.
public void testMissingMultiValuedStringFirst() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
SortField sortField = new SortedSetSortField("foo", false);
sortField.setMissingValue(SortField.STRING_FIRST);
Sort indexSort = new Sort(sortField);
iwc.setIndexSort(indexSort);
IndexWriter w = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new NumericDocValuesField("id", 3));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzz")));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzza")));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzzd")));
w.addDocument(doc);
// so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
w.commit();
// missing
doc = new Document();
doc.add(new NumericDocValuesField("id", 1));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(new NumericDocValuesField("id", 2));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("mmm")));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("nnnn")));
w.addDocument(doc);
w.forceMerge(1);
DirectoryReader r = DirectoryReader.open(w);
LeafReader leaf = getOnlyLeafReader(r);
assertEquals(3, leaf.maxDoc());
NumericDocValues values = leaf.getNumericDocValues("id");
assertEquals(0, values.nextDoc());
assertEquals(1l, values.longValue());
assertEquals(1, values.nextDoc());
assertEquals(2l, values.longValue());
assertEquals(2, values.nextDoc());
assertEquals(3l, values.longValue());
r.close();
w.close();
dir.close();
}
use of org.apache.lucene.search.SortedSetSortField in project lucene-solr by apache.
the class TestIndexSorting method testMultiValuedStringAlreadySorted.
public void testMultiValuedStringAlreadySorted() throws Exception {
assertNeedsIndexSortMerge(new SortedSetSortField("foo", false), (doc) -> {
doc.add(new SortedSetDocValuesField("foo", new BytesRef("")));
int num = random().nextInt(5);
for (int j = 0; j < num; j++) {
doc.add(new SortedSetDocValuesField("foo", TestUtil.randomBinaryTerm(random())));
}
}, (doc) -> {
int num = random().nextInt(5);
for (int j = 0; j < num; j++) {
doc.add(new SortedSetDocValuesField("foo", TestUtil.randomBinaryTerm(random())));
}
});
}
use of org.apache.lucene.search.SortedSetSortField in project lucene-solr by apache.
the class TestIndexSorting method testMissingMultiValuedStringLast.
public void testMissingMultiValuedStringLast() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
SortField sortField = new SortedSetSortField("foo", false);
sortField.setMissingValue(SortField.STRING_LAST);
Sort indexSort = new Sort(sortField);
iwc.setIndexSort(indexSort);
IndexWriter w = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new NumericDocValuesField("id", 2));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzz")));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzzd")));
w.addDocument(doc);
// so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
w.commit();
// missing
doc = new Document();
doc.add(new NumericDocValuesField("id", 3));
w.addDocument(doc);
w.commit();
doc = new Document();
doc.add(new NumericDocValuesField("id", 1));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("mmm")));
doc.add(new SortedSetDocValuesField("foo", new BytesRef("ppp")));
w.addDocument(doc);
w.forceMerge(1);
DirectoryReader r = DirectoryReader.open(w);
LeafReader leaf = getOnlyLeafReader(r);
assertEquals(3, leaf.maxDoc());
NumericDocValues values = leaf.getNumericDocValues("id");
assertEquals(0, values.nextDoc());
assertEquals(1l, values.longValue());
assertEquals(1, values.nextDoc());
assertEquals(2l, values.longValue());
assertEquals(2, values.nextDoc());
assertEquals(3l, values.longValue());
r.close();
w.close();
dir.close();
}
Aggregations