Search in sources :

Example 61 with SortField

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

the class TestNumericTerms64 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++) {
        long lower = (long) (random().nextDouble() * noDocs * distance) + startOffset;
        long upper = (long) (random().nextDouble() * noDocs * distance) + startOffset;
        if (lower > upper) {
            long a = lower;
            lower = upper;
            upper = a;
        }
        Query tq = LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
        TopDocs topDocs = searcher.search(tq, noDocs, new Sort(new SortField(field, SortField.Type.LONG, true)));
        if (topDocs.totalHits == 0)
            continue;
        ScoreDoc[] sd = topDocs.scoreDocs;
        assertNotNull(sd);
        long last = searcher.doc(sd[0].doc).getField(field).numericValue().longValue();
        for (int j = 1; j < sd.length; j++) {
            long act = searcher.doc(sd[j].doc).getField(field).numericValue().longValue();
            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)

Example 62 with SortField

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

the class SolrIndexConfigTest method testSortingMPSolrIndexConfigCreation.

public void testSortingMPSolrIndexConfigCreation() throws Exception {
    final String expectedFieldName = "timestamp_i_dvo";
    final SortField.Type expectedFieldType = SortField.Type.INT;
    final boolean expectedFieldSortDescending = true;
    SolrConfig solrConfig = new SolrConfig(instanceDir, solrConfigFileNameSortingMergePolicyFactory, null);
    SolrIndexConfig solrIndexConfig = new SolrIndexConfig(solrConfig, null, null);
    assertNotNull(solrIndexConfig);
    IndexSchema indexSchema = IndexSchemaFactory.buildIndexSchema(schemaFileName, solrConfig);
    h.getCore().setLatestSchema(indexSchema);
    IndexWriterConfig iwc = solrIndexConfig.toIndexWriterConfig(h.getCore());
    final MergePolicy mergePolicy = iwc.getMergePolicy();
    assertNotNull("null mergePolicy", mergePolicy);
    assertTrue("mergePolicy (" + mergePolicy + ") is not a SortingMergePolicy", mergePolicy instanceof SortingMergePolicy);
    final SortingMergePolicy sortingMergePolicy = (SortingMergePolicy) mergePolicy;
    final Sort expected = new Sort(new SortField(expectedFieldName, expectedFieldType, expectedFieldSortDescending));
    final Sort actual = sortingMergePolicy.getSort();
    assertEquals("SortingMergePolicy.getSort", expected, actual);
}
Also used : SolrConfig(org.apache.solr.core.SolrConfig) SortingMergePolicy(org.apache.solr.index.SortingMergePolicy) MergePolicy(org.apache.lucene.index.MergePolicy) TieredMergePolicy(org.apache.lucene.index.TieredMergePolicy) SortingMergePolicy(org.apache.solr.index.SortingMergePolicy) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) IndexSchema(org.apache.solr.schema.IndexSchema) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 63 with SortField

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

the class TestIndexSorting method testMissingMultiValuedIntFirst.

public void testMissingMultiValuedIntFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.INT);
    sortField.setMissingValue(Integer.valueOf(Integer.MIN_VALUE));
    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 SortedNumericDocValuesField("foo", 18));
    doc.add(new SortedNumericDocValuesField("foo", 187667));
    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 SortedNumericDocValuesField("foo", 7));
    doc.add(new SortedNumericDocValuesField("foo", 34));
    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(1, values.longValue());
    assertEquals(1, values.nextDoc());
    assertEquals(2, values.longValue());
    assertEquals(2, values.nextDoc());
    assertEquals(3, values.longValue());
    r.close();
    w.close();
    dir.close();
}
Also used : SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory)

Example 64 with SortField

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

the class TestIndexSorting method testMissingDoubleFirst.

public void testMissingDoubleFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortField("foo", SortField.Type.DOUBLE);
    sortField.setMissingValue(Double.NEGATIVE_INFINITY);
    Sort indexSort = new Sort(sortField);
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new DoubleDocValuesField("foo", 18.0));
    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
    w.addDocument(new Document());
    w.commit();
    doc = new Document();
    doc.add(new DoubleDocValuesField("foo", 7.0));
    w.addDocument(doc);
    w.forceMerge(1);
    DirectoryReader r = DirectoryReader.open(w);
    LeafReader leaf = getOnlyLeafReader(r);
    assertEquals(3, leaf.maxDoc());
    NumericDocValues values = leaf.getNumericDocValues("foo");
    assertEquals(1, values.nextDoc());
    assertEquals(7.0, Double.longBitsToDouble(values.longValue()), 0.0);
    assertEquals(2, values.nextDoc());
    assertEquals(18.0, Double.longBitsToDouble(values.longValue()), 0.0);
    r.close();
    w.close();
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Example 65 with SortField

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

the class TestIndexSorting method testMissingMultiValuedFloatFirst.

public void testMissingMultiValuedFloatFirst() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    SortField sortField = new SortedNumericSortField("foo", SortField.Type.FLOAT);
    sortField.setMissingValue(Float.NEGATIVE_INFINITY);
    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 SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(18.0f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(726.0f)));
    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 SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(7.0f)));
    doc.add(new SortedNumericDocValuesField("foo", NumericUtils.floatToSortableInt(18.0f)));
    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(1, values.longValue());
    assertEquals(1, values.nextDoc());
    assertEquals(2, values.longValue());
    assertEquals(2, values.nextDoc());
    assertEquals(3, values.longValue());
    r.close();
    w.close();
    dir.close();
}
Also used : SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) Directory(org.apache.lucene.store.Directory)

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