Search in sources :

Example 86 with SortField

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

the class TestNestedDocsSort method parse.

private SortField parse(String a) {
    final SolrQueryRequest req = req("q", "{!parent which=type_s1:parent}whatever_s1:foo", "q2", "{!parent which=type_s1:parent}nomater_s1:what", "notbjq", "foo_s1:bar");
    try {
        final SortSpec spec = SortSpecParsing.parseSortSpec(a, req);
        assertNull(spec.getSchemaFields().get(0));
        final Sort sort = spec.getSort();
        final SortField field = sort.getSort()[0];
        assertNotNull(field);
        return field;
    } finally {
        req.close();
    }
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) SortSpec(org.apache.solr.search.SortSpec)

Example 87 with SortField

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

the class TestSort method testRandomFieldNameSorts.

public void testRandomFieldNameSorts() throws Exception {
    SolrQueryRequest req = lrf.makeRequest("q", "*:*");
    final int iters = atLeast(5000);
    // infinite loop abort when trying to generate a non-blank sort "name"
    final int nonBlankAttempts = 37;
    for (int i = 0; i < iters; i++) {
        final StringBuilder input = new StringBuilder();
        final String[] names = new String[TestUtil.nextInt(r, 1, 10)];
        final boolean[] reverse = new boolean[names.length];
        for (int j = 0; j < names.length; j++) {
            names[j] = null;
            for (int k = 0; k < nonBlankAttempts && null == names[j]; k++) {
                names[j] = TestUtil.randomRealisticUnicodeString(r, 1, 100);
                // munge anything that might make this a function
                names[j] = names[j].replaceFirst("\\{", "\\{\\{");
                names[j] = names[j].replaceFirst("\\(", "\\(\\(");
                names[j] = names[j].replaceFirst("(\\\"|\\')", "$1$1z");
                names[j] = names[j].replaceFirst("(\\d)", "$1x");
                // eliminate pesky problem chars
                names[j] = names[j].replaceAll("\\p{Cntrl}|\\p{javaWhitespace}", "");
                if (0 == names[j].length()) {
                    names[j] = null;
                }
            }
            // with luck this bad, never go to vegas
            // alternatively: if (null == names[j]) names[j] = "never_go_to_vegas";
            assertNotNull("Unable to generate a (non-blank) names[" + j + "] after " + nonBlankAttempts + " attempts", names[j]);
            reverse[j] = r.nextBoolean();
            input.append(r.nextBoolean() ? " " : "");
            input.append(names[j]);
            input.append(" ");
            input.append(reverse[j] ? "desc," : "asc,");
        }
        input.deleteCharAt(input.length() - 1);
        SortField[] sorts = null;
        List<SchemaField> fields = null;
        try {
            SortSpec spec = SortSpecParsing.parseSortSpec(input.toString(), req);
            sorts = spec.getSort().getSort();
            fields = spec.getSchemaFields();
        } catch (RuntimeException e) {
            throw new RuntimeException("Failed to parse sort: " + input, e);
        }
        assertEquals("parsed sorts had unexpected size", names.length, sorts.length);
        assertEquals("parsed sort schema fields had unexpected size", names.length, fields.size());
        for (int j = 0; j < names.length; j++) {
            assertEquals("sorts[" + j + "] had unexpected reverse: " + input, reverse[j], sorts[j].getReverse());
            final Type type = sorts[j].getType();
            if (Type.SCORE.equals(type)) {
                assertEquals("sorts[" + j + "] is (unexpectedly) type score : " + input, "score", names[j]);
            } else if (Type.DOC.equals(type)) {
                assertEquals("sorts[" + j + "] is (unexpectedly) type doc : " + input, "_docid_", names[j]);
            } else if (Type.CUSTOM.equals(type) || Type.REWRITEABLE.equals(type)) {
                fail("sorts[" + j + "] resulted in a '" + type.toString() + "', either sort parsing code is broken, or func/query " + "semantics have gotten broader and munging in this test " + "needs improved: " + input);
            } else {
                assertEquals("sorts[" + j + "] (" + type.toString() + ") had unexpected field in: " + input, names[j], sorts[j].getField());
                assertEquals("fields[" + j + "] (" + type.toString() + ") had unexpected name in: " + input, names[j], fields.get(j).getName());
            }
        }
    }
}
Also used : SortField(org.apache.lucene.search.SortField) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Type(org.apache.lucene.search.SortField.Type)

Example 88 with SortField

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

the class TestFieldCacheSort method testFloatMissingLast.

/** Tests sorting on type float, specifying the missing value should be treated as Float.MAX_VALUE */
public void testFloatMissingLast() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new FloatPoint("value", -1.3f));
    doc.add(new StoredField("value", -1.3f));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new FloatPoint("value", 4.2f));
    doc.add(new StoredField("value", 4.2f));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.FLOAT_POINT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir, false);
    SortField sortField = new SortField("value", SortField.Type.FLOAT);
    sortField.setMissingValue(Float.MAX_VALUE);
    Sort sort = new Sort(sortField);
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // null is treated as Float.MAX_VALUE
    assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertNull(searcher.doc(td.scoreDocs[2].doc).get("value"));
    TestUtil.checkReader(ir);
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 89 with SortField

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

the class TestFieldCacheSort method testLegacyFloatReverse.

/** Tests sorting on type legacy float in reverse */
public void testLegacyFloatReverse() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new LegacyFloatField("value", 30.1f, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", -1.3f, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", 4.2f, Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.LEGACY_FLOAT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT, true));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // reverse numeric order
    assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("-1.3", searcher.doc(td.scoreDocs[2].doc).get("value"));
    TestUtil.checkReader(ir);
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) LegacyFloatField(org.apache.solr.legacy.LegacyFloatField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 90 with SortField

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

the class TestFieldCacheSort method testLegacyLongMissing.

/** Tests sorting on type legacy long with a missing value */
public void testLegacyLongMissing() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyLongField("value", -1, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyLongField("value", 4, Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.LEGACY_LONG));
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    Sort sort = new Sort(new SortField("value", SortField.Type.LONG));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // null is treated as 0
    assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertNull(searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("4", searcher.doc(td.scoreDocs[2].doc).get("value"));
    TestUtil.checkReader(ir);
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) LegacyLongField(org.apache.solr.legacy.LegacyLongField) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) 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