Search in sources :

Example 71 with SortedNumericDocValuesField

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

the class TestPointFields method callAndCheckCreateFields.

/** 
   * Calls {@link SchemaField#createFields} on the specified value for the specified field name, and asserts 
   * that the results match the SchemaField propeties, with an additional check that the <code>pointType</code> 
   * is included if and only if the SchemaField is "indexed" 
   */
private List<IndexableField> callAndCheckCreateFields(final String fieldName, final Class<?> pointType, final Object value) throws Exception {
    final SchemaField sf = h.getCore().getLatestSchema().getField(fieldName);
    final List<IndexableField> results = sf.createFields(value);
    final Set<IndexableField> resultSet = new LinkedHashSet<>(results);
    assertEquals("duplicates found in results? " + results.toString(), results.size(), resultSet.size());
    final Set<Class<?>> resultClasses = new HashSet<>();
    for (IndexableField f : results) {
        resultClasses.add(f.getClass());
        if (!sf.hasDocValues()) {
            assertFalse(f.toString(), (f instanceof NumericDocValuesField) || (f instanceof SortedNumericDocValuesField));
        }
    }
    assertEquals(fieldName + " stored? Result Fields: " + Arrays.toString(results.toArray()), sf.stored(), resultClasses.contains(StoredField.class));
    assertEquals(fieldName + " indexed? Result Fields: " + Arrays.toString(results.toArray()), sf.indexed(), resultClasses.contains(pointType));
    if (sf.multiValued()) {
        assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()), sf.hasDocValues(), resultClasses.contains(SortedNumericDocValuesField.class));
    } else {
        assertEquals(fieldName + " docvalues? Result Fields: " + Arrays.toString(results.toArray()), sf.hasDocValues(), resultClasses.contains(NumericDocValuesField.class));
    }
    return results;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) LinkedHashSet(java.util.LinkedHashSet) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BeforeClass(org.junit.BeforeClass) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 72 with SortedNumericDocValuesField

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

the class TestDuelingCodecs method createRandomIndex.

/**
   * populates a writer with random stuff. this must be fully reproducable with the seed!
   */
public static void createRandomIndex(int numdocs, RandomIndexWriter writer, long seed) throws IOException {
    Random random = new Random(seed);
    // primary source for our data is from linefiledocs, it's realistic.
    LineFileDocs lineFileDocs = new LineFileDocs(random);
    // because linefiledocs doesn't cover all the possibilities.
    for (int i = 0; i < numdocs; i++) {
        Document document = lineFileDocs.nextDoc();
        // grab the title and add some SortedSet instances for fun
        String title = document.get("titleTokenized");
        String[] split = title.split("\\s+");
        document.removeFields("sortedset");
        for (String trash : split) {
            document.add(new SortedSetDocValuesField("sortedset", new BytesRef(trash)));
        }
        // add a numeric dv field sometimes
        document.removeFields("sparsenumeric");
        if (random.nextInt(4) == 2) {
            document.add(new NumericDocValuesField("sparsenumeric", random.nextInt()));
        }
        // add sortednumeric sometimes
        document.removeFields("sparsesortednum");
        if (random.nextInt(5) == 1) {
            document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
            if (random.nextBoolean()) {
                document.add(new SortedNumericDocValuesField("sparsesortednum", random.nextLong()));
            }
        }
        writer.addDocument(document);
    }
    lineFileDocs.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Random(java.util.Random) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) LineFileDocs(org.apache.lucene.util.LineFileDocs)

Example 73 with SortedNumericDocValuesField

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

the class TestSortedNumericSortField method testForward.

public void testForward() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", 5));
    doc.add(newStringField("id", "2", Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", 3));
    doc.add(new SortedNumericDocValuesField("value", 7));
    doc.add(newStringField("id", "1", Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = writer.getReader();
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    Sort sort = new Sort(new SortedNumericSortField("value", SortField.Type.INT));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(2, td.totalHits);
    // 3 comes before 5
    assertEquals("1", searcher.doc(td.scoreDocs[0].doc).get("id"));
    assertEquals("2", searcher.doc(td.scoreDocs[1].doc).get("id"));
    ir.close();
    dir.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 74 with SortedNumericDocValuesField

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

the class TestSortedNumericSortField method testMissingFirst.

public void testMissingFirst() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", 5));
    doc.add(newStringField("id", "2", Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", 3));
    doc.add(new SortedNumericDocValuesField("value", 7));
    doc.add(newStringField("id", "1", Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(newStringField("id", "3", Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = writer.getReader();
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    SortField sortField = new SortedNumericSortField("value", SortField.Type.INT);
    sortField.setMissingValue(Integer.MIN_VALUE);
    Sort sort = new Sort(sortField);
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(3, td.totalHits);
    // 3 comes before 5
    // null comes first
    assertEquals("3", searcher.doc(td.scoreDocs[0].doc).get("id"));
    assertEquals("1", searcher.doc(td.scoreDocs[1].doc).get("id"));
    assertEquals("2", searcher.doc(td.scoreDocs[2].doc).get("id"));
    ir.close();
    dir.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 75 with SortedNumericDocValuesField

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

the class TestSortedNumericSortField method testDouble.

public void testDouble() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(-3d)));
    doc.add(newStringField("id", "2", Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(-5d)));
    doc.add(new SortedNumericDocValuesField("value", NumericUtils.doubleToSortableLong(7d)));
    doc.add(newStringField("id", "1", Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = writer.getReader();
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    Sort sort = new Sort(new SortedNumericSortField("value", SortField.Type.DOUBLE));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
    assertEquals(2, td.totalHits);
    // -5 comes before -3
    assertEquals("1", searcher.doc(td.scoreDocs[0].doc).get("id"));
    assertEquals("2", searcher.doc(td.scoreDocs[1].doc).get("id"));
    ir.close();
    dir.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)104 Document (org.apache.lucene.document.Document)86 Directory (org.apache.lucene.store.Directory)72 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)44 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)40 IndexReader (org.apache.lucene.index.IndexReader)35 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)27 BytesRef (org.apache.lucene.util.BytesRef)21 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)19 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)18 StringField (org.apache.lucene.document.StringField)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)18 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)16 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)16 Sort (org.apache.lucene.search.Sort)15 SortField (org.apache.lucene.search.SortField)14 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12 StoredField (org.apache.lucene.document.StoredField)12