Search in sources :

Example 6 with SortedSetSortField

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));
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Sort(org.apache.lucene.search.Sort) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 7 with SortedSetSortField

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();
    }
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Sort(org.apache.lucene.search.Sort) QueryContext(org.neo4j.index.lucene.QueryContext)

Example 8 with SortedSetSortField

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();
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 9 with SortedSetSortField

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())));
        }
    });
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) BinaryPoint(org.apache.lucene.document.BinaryPoint) IntPoint(org.apache.lucene.document.IntPoint)

Example 10 with SortedSetSortField

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();
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

SortedSetSortField (org.apache.lucene.search.SortedSetSortField)17 SortField (org.apache.lucene.search.SortField)13 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)12 Sort (org.apache.lucene.search.Sort)11 BytesRef (org.apache.lucene.util.BytesRef)7 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)6 Document (org.apache.lucene.document.Document)5 Version (org.apache.lucene.util.Version)5 SortedSetSelector (org.apache.lucene.search.SortedSetSelector)4 Directory (org.apache.lucene.store.Directory)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)3 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 SegmentInfo (org.apache.lucene.index.SegmentInfo)3 SortedNumericSelector (org.apache.lucene.search.SortedNumericSelector)3 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)3 IndexOutput (org.apache.lucene.store.IndexOutput)3 HashMap (java.util.HashMap)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2