Search in sources :

Example 56 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class IntPointField method getSortField.

@Override
public SortField getSortField(SchemaField field, boolean top) {
    field.checkSortability();
    Object missingValue = null;
    boolean sortMissingLast = field.sortMissingLast();
    boolean sortMissingFirst = field.sortMissingFirst();
    if (sortMissingLast) {
        missingValue = top ? Integer.MIN_VALUE : Integer.MAX_VALUE;
    } else if (sortMissingFirst) {
        missingValue = top ? Integer.MAX_VALUE : Integer.MIN_VALUE;
    }
    SortField sf = new SortField(field.getName(), SortField.Type.INT, top);
    sf.setMissingValue(missingValue);
    return sf;
}
Also used : SortField(org.apache.lucene.search.SortField)

Example 57 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class DoublePointField method getSortField.

@Override
public SortField getSortField(SchemaField field, boolean top) {
    field.checkSortability();
    Object missingValue = null;
    boolean sortMissingLast = field.sortMissingLast();
    boolean sortMissingFirst = field.sortMissingFirst();
    if (sortMissingLast) {
        missingValue = top ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    } else if (sortMissingFirst) {
        missingValue = top ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
    }
    SortField sf = new SortField(field.getName(), SortField.Type.DOUBLE, top);
    sf.setMissingValue(missingValue);
    return sf;
}
Also used : SortField(org.apache.lucene.search.SortField)

Example 58 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class DistinctValuesCollectorTest method testRandom.

public void testRandom() throws Exception {
    Random random = random();
    int numberOfRuns = TestUtil.nextInt(random, 3, 6);
    for (int indexIter = 0; indexIter < numberOfRuns; indexIter++) {
        IndexContext context = createIndexContext();
        for (int searchIter = 0; searchIter < 100; searchIter++) {
            final IndexSearcher searcher = newSearcher(context.indexReader);
            String term = context.contentStrings[random.nextInt(context.contentStrings.length)];
            Sort groupSort = new Sort(new SortField("id", SortField.Type.STRING));
            int topN = 1 + random.nextInt(10);
            List<DistinctValuesCollector.GroupCount<Comparable<Object>, Comparable<Object>>> expectedResult = createExpectedResult(context, term, groupSort, topN);
            FirstPassGroupingCollector<Comparable<Object>> firstCollector = createRandomFirstPassCollector(groupSort, GROUP_FIELD, topN);
            searcher.search(new TermQuery(new Term("content", term)), firstCollector);
            DistinctValuesCollector<Comparable<Object>, Comparable<Object>> distinctValuesCollector = createDistinctCountCollector(firstCollector, COUNT_FIELD);
            searcher.search(new TermQuery(new Term("content", term)), distinctValuesCollector);
            @SuppressWarnings("unchecked") List<DistinctValuesCollector.GroupCount<Comparable<Object>, Comparable<Object>>> actualResult = distinctValuesCollector.getGroups();
            if (VERBOSE) {
                System.out.println("Index iter=" + indexIter);
                System.out.println("Search iter=" + searchIter);
                System.out.println("1st pass collector class name=" + firstCollector.getClass().getName());
                System.out.println("2nd pass collector class name=" + distinctValuesCollector.getClass().getName());
                System.out.println("Search term=" + term);
                System.out.println("1st pass groups=" + firstCollector.getTopGroups(0, false));
                System.out.println("Expected:");
                printGroups(expectedResult);
                System.out.println("Actual:");
                printGroups(actualResult);
            }
            assertEquals(expectedResult.size(), actualResult.size());
            for (int i = 0; i < expectedResult.size(); i++) {
                DistinctValuesCollector.GroupCount<Comparable<Object>, Comparable<Object>> expected = expectedResult.get(i);
                DistinctValuesCollector.GroupCount<Comparable<Object>, Comparable<Object>> actual = actualResult.get(i);
                assertValues(expected.groupValue, actual.groupValue);
                assertEquals(expected.uniqueValues.size(), actual.uniqueValues.size());
                List<Comparable<?>> expectedUniqueValues = new ArrayList<>(expected.uniqueValues);
                Collections.sort(expectedUniqueValues, nullComparator);
                List<Comparable<?>> actualUniqueValues = new ArrayList<>(actual.uniqueValues);
                Collections.sort(actualUniqueValues, nullComparator);
                for (int j = 0; j < expectedUniqueValues.size(); j++) {
                    assertValues(expectedUniqueValues.get(j), actualUniqueValues.get(j));
                }
            }
        }
        context.indexReader.close();
        context.directory.close();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) Random(java.util.Random) Sort(org.apache.lucene.search.Sort)

Example 59 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class AllGroupHeadsCollectorTest method testBasic.

public void testBasic() throws Exception {
    final String groupField = "author";
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
    DocValuesType valueType = DocValuesType.SORTED;
    // 0
    Document doc = new Document();
    addGroupField(doc, groupField, "author1", valueType);
    doc.add(newTextField("content", "random text", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 1));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("1")));
    w.addDocument(doc);
    // 1
    doc = new Document();
    addGroupField(doc, groupField, "author1", valueType);
    doc.add(newTextField("content", "some more random text blob", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 2));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("2")));
    w.addDocument(doc);
    // 2
    doc = new Document();
    addGroupField(doc, groupField, "author1", valueType);
    doc.add(newTextField("content", "some more random textual data", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 3));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("3")));
    w.addDocument(doc);
    // To ensure a second segment
    w.commit();
    // 3
    doc = new Document();
    addGroupField(doc, groupField, "author2", valueType);
    doc.add(newTextField("content", "some random text", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 4));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("4")));
    w.addDocument(doc);
    // 4
    doc = new Document();
    addGroupField(doc, groupField, "author3", valueType);
    doc.add(newTextField("content", "some more random text", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 5));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("5")));
    w.addDocument(doc);
    // 5
    doc = new Document();
    addGroupField(doc, groupField, "author3", valueType);
    doc.add(newTextField("content", "random blob", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 6));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("6")));
    w.addDocument(doc);
    // 6 -- no author field
    doc = new Document();
    doc.add(newTextField("content", "random word stuck in alot of other text", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 6));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("6")));
    w.addDocument(doc);
    // 7 -- no author field
    doc = new Document();
    doc.add(newTextField("content", "random word stuck in alot of other text", Field.Store.NO));
    doc.add(new NumericDocValuesField("id_1", 7));
    doc.add(new SortedDocValuesField("id_2", new BytesRef("7")));
    w.addDocument(doc);
    IndexReader reader = w.getReader();
    IndexSearcher indexSearcher = newSearcher(reader);
    w.close();
    int maxDoc = reader.maxDoc();
    Sort sortWithinGroup = new Sort(new SortField("id_1", SortField.Type.INT, true));
    AllGroupHeadsCollector<?> allGroupHeadsCollector = createRandomCollector(groupField, sortWithinGroup);
    indexSearcher.search(new TermQuery(new Term("content", "random")), allGroupHeadsCollector);
    assertTrue(arrayContains(new int[] { 2, 3, 5, 7 }, allGroupHeadsCollector.retrieveGroupHeads()));
    assertTrue(openBitSetContains(new int[] { 2, 3, 5, 7 }, allGroupHeadsCollector.retrieveGroupHeads(maxDoc), maxDoc));
    allGroupHeadsCollector = createRandomCollector(groupField, sortWithinGroup);
    indexSearcher.search(new TermQuery(new Term("content", "some")), allGroupHeadsCollector);
    assertTrue(arrayContains(new int[] { 2, 3, 4 }, allGroupHeadsCollector.retrieveGroupHeads()));
    assertTrue(openBitSetContains(new int[] { 2, 3, 4 }, allGroupHeadsCollector.retrieveGroupHeads(maxDoc), maxDoc));
    allGroupHeadsCollector = createRandomCollector(groupField, sortWithinGroup);
    indexSearcher.search(new TermQuery(new Term("content", "blob")), allGroupHeadsCollector);
    assertTrue(arrayContains(new int[] { 1, 5 }, allGroupHeadsCollector.retrieveGroupHeads()));
    assertTrue(openBitSetContains(new int[] { 1, 5 }, allGroupHeadsCollector.retrieveGroupHeads(maxDoc), maxDoc));
    // STRING sort type triggers different implementation
    Sort sortWithinGroup2 = new Sort(new SortField("id_2", SortField.Type.STRING, true));
    allGroupHeadsCollector = createRandomCollector(groupField, sortWithinGroup2);
    indexSearcher.search(new TermQuery(new Term("content", "random")), allGroupHeadsCollector);
    assertTrue(arrayContains(new int[] { 2, 3, 5, 7 }, allGroupHeadsCollector.retrieveGroupHeads()));
    assertTrue(openBitSetContains(new int[] { 2, 3, 5, 7 }, allGroupHeadsCollector.retrieveGroupHeads(maxDoc), maxDoc));
    Sort sortWithinGroup3 = new Sort(new SortField("id_2", SortField.Type.STRING, false));
    allGroupHeadsCollector = createRandomCollector(groupField, sortWithinGroup3);
    indexSearcher.search(new TermQuery(new Term("content", "random")), allGroupHeadsCollector);
    // 7 b/c higher doc id wins, even if order of field is in not in reverse.
    assertTrue(arrayContains(new int[] { 0, 3, 4, 6 }, allGroupHeadsCollector.retrieveGroupHeads()));
    assertTrue(openBitSetContains(new int[] { 0, 3, 4, 6 }, allGroupHeadsCollector.retrieveGroupHeads(maxDoc), maxDoc));
    indexSearcher.getIndexReader().close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) DocValuesType(org.apache.lucene.index.DocValuesType) Sort(org.apache.lucene.search.Sort) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 60 with SortField

use of org.apache.lucene.search.SortField in project lucene-solr by apache.

the class TestNumericTerms32 method testSorting.

private void testSorting(int precisionStep) throws Exception {
    String field = "field" + precisionStep;
    // 10 random tests, the index order is ascending,
    // so using a reverse sort field should retun descending documents
    int num = TestUtil.nextInt(random(), 10, 20);
    for (int i = 0; i < num; i++) {
        int lower = (int) (random().nextDouble() * noDocs * distance) + startOffset;
        int upper = (int) (random().nextDouble() * noDocs * distance) + startOffset;
        if (lower > upper) {
            int a = lower;
            lower = upper;
            upper = a;
        }
        Query tq = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
        TopDocs topDocs = searcher.search(tq, noDocs, new Sort(new SortField(field, SortField.Type.INT, true)));
        if (topDocs.totalHits == 0)
            continue;
        ScoreDoc[] sd = topDocs.scoreDocs;
        assertNotNull(sd);
        int last = searcher.doc(sd[0].doc).getField(field).numericValue().intValue();
        for (int j = 1; j < sd.length; j++) {
            int act = searcher.doc(sd[j].doc).getField(field).numericValue().intValue();
            assertTrue("Docs should be sorted backwards", last > act);
            last = act;
        }
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

SortField (org.apache.lucene.search.SortField)231 Sort (org.apache.lucene.search.Sort)175 Document (org.apache.lucene.document.Document)116 Directory (org.apache.lucene.store.Directory)110 IndexSearcher (org.apache.lucene.search.IndexSearcher)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)75 TopDocs (org.apache.lucene.search.TopDocs)74 IndexReader (org.apache.lucene.index.IndexReader)65 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)62 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)49 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)37 TermQuery (org.apache.lucene.search.TermQuery)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)32 Query (org.apache.lucene.search.Query)29 Term (org.apache.lucene.index.Term)25 BytesRef (org.apache.lucene.util.BytesRef)25 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)24 StoredField (org.apache.lucene.document.StoredField)23