Search in sources :

Example 71 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestFieldCacheSort method testDoubleReverse.

/** Tests sorting on type double in reverse */
public void testDoubleReverse() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new DoublePoint("value", 30.1));
    doc.add(new StoredField("value", 30.1));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", -1.3));
    doc.add(new StoredField("value", -1.3));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", 4.2333333333333));
    doc.add(new StoredField("value", 4.2333333333333));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", 4.2333333333332));
    doc.add(new StoredField("value", 4.2333333333332));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.DOUBLE_POINT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir, false);
    Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE, true));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(4, td.totalHits);
    // numeric order
    assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertEquals("4.2333333333333", searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("4.2333333333332", searcher.doc(td.scoreDocs[2].doc).get("value"));
    assertEquals("-1.3", searcher.doc(td.scoreDocs[3].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) DoublePoint(org.apache.lucene.document.DoublePoint) 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 72 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestFieldCacheSort method testLong.

/** Tests sorting on type long */
public void testLong() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new LongPoint("value", 3000000000L));
    doc.add(new StoredField("value", 3000000000L));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LongPoint("value", -1));
    doc.add(new StoredField("value", -1));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LongPoint("value", 4));
    doc.add(new StoredField("value", 4));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.LONG_POINT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir, false);
    Sort sort = new Sort(new SortField("value", SortField.Type.LONG));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // numeric order
    assertEquals("-1", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertEquals("4", searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("3000000000", 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) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) LongPoint(org.apache.lucene.document.LongPoint) 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 73 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestFieldCacheSort method testFloatReverse.

/** Tests sorting on type float in reverse */
public void testFloatReverse() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new FloatPoint("value", 30.1f));
    doc.add(new StoredField("value", 30.1f));
    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);
    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) 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 74 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestFieldCacheSort method testFloatMissing.

/** Tests sorting on type float with a missing value */
public void testFloatMissing() 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);
    Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // null is treated as 0
    assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertNull(searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("4.2", 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 75 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestFieldCacheSort method testDoubleMissing.

/** Tests sorting on type double with a missing value */
public void testDoubleMissing() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", -1.3));
    doc.add(new StoredField("value", -1.3));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", 4.2333333333333));
    doc.add(new StoredField("value", 4.2333333333333));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("value", 4.2333333333332));
    doc.add(new StoredField("value", 4.2333333333332));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.DOUBLE_POINT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir, false);
    Sort sort = new Sort(new SortField("value", SortField.Type.DOUBLE));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(4, td.totalHits);
    // null treated as a 0
    assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
    assertNull(searcher.doc(td.scoreDocs[1].doc).get("value"));
    assertEquals("4.2333333333332", searcher.doc(td.scoreDocs[2].doc).get("value"));
    assertEquals("4.2333333333333", searcher.doc(td.scoreDocs[3].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) DoublePoint(org.apache.lucene.document.DoublePoint) 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

StoredField (org.apache.lucene.document.StoredField)109 Document (org.apache.lucene.document.Document)97 Directory (org.apache.lucene.store.Directory)72 StringField (org.apache.lucene.document.StringField)43 Field (org.apache.lucene.document.Field)40 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)39 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)36 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)34 BytesRef (org.apache.lucene.util.BytesRef)34 TextField (org.apache.lucene.document.TextField)30 IndexReader (org.apache.lucene.index.IndexReader)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)26 IntPoint (org.apache.lucene.document.IntPoint)24 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)23 TopDocs (org.apache.lucene.search.TopDocs)23 SortField (org.apache.lucene.search.SortField)22 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)21 Sort (org.apache.lucene.search.Sort)21 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)18 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18