Search in sources :

Example 16 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class Murmur3FieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    final Object value;
    if (context.externalValueSet()) {
        value = context.externalValue();
    } else {
        value = context.parser().textOrNull();
    }
    if (value != null) {
        final BytesRef bytes = new BytesRef(value.toString());
        final long hash = MurmurHash3.hash128(bytes.bytes, bytes.offset, bytes.length, 0, new MurmurHash3.Hash128()).h1;
        fields.add(new SortedNumericDocValuesField(fieldType().name(), hash));
        if (fieldType().stored()) {
            fields.add(new StoredField(name(), hash));
        }
    }
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) BytesRef(org.apache.lucene.util.BytesRef)

Example 17 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class ScaledFloatFieldTypeTests method testFieldData.

public void testFieldData() throws IOException {
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("scaled_float1", 10));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 5));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 12));
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        IndexMetaData indexMetadata = new IndexMetaData.Builder("index").settings(Settings.builder().put("index.version.created", Version.CURRENT).put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()).build();
        IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
        // single-valued
        ft.setName("scaled_float1");
        IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE);
        AtomicNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0));
        SortedNumericDoubleValues values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(10 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        // multi-valued
        ft.setName("scaled_float2");
        fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        leafFieldData = fielddata.load(reader.leaves().get(0));
        values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(2, values.count());
        assertEquals(5 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        assertEquals(12 / ft.getScalingFactor(), values.valueAt(1), 10e-5);
    }
    IOUtils.close(w, dir);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexSettings(org.elasticsearch.index.IndexSettings) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) Document(org.apache.lucene.document.Document) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) AtomicNumericFieldData(org.elasticsearch.index.fielddata.AtomicNumericFieldData) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 18 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseDouble.

public void testCollapseDouble() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Double>() {

        @Override
        public Double randomGroup(int maxGroup) {
            return new Double(randomIntBetween(0, maxGroup - 1));
        }

        @Override
        public void add(Document doc, Double value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            } else {
                doc.add(new NumericDocValuesField("field", Double.doubleToLongBits(value)));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.DOUBLE);
            } else {
                return new SortField("field", SortField.Type.DOUBLE);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Example 19 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseInt.

public void testCollapseInt() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<Integer>() {

        @Override
        public Integer randomGroup(int maxGroup) {
            return randomIntBetween(0, maxGroup - 1);
        }

        @Override
        public void add(Document doc, Integer value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedNumericDocValuesField("field", value));
            } else {
                doc.add(new NumericDocValuesField("field", value));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedNumericSortField("field", SortField.Type.INT);
            } else {
                return new SortField("field", SortField.Type.INT);
            }
        }
    };
    assertSearchCollapse(producer, true);
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Document(org.apache.lucene.document.Document)

Example 20 with SortedNumericDocValuesField

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

the class TestLucene54DocValuesFormat method testSortedNumericAroundBlockSize.

@Slow
public void testSortedNumericAroundBlockSize() throws IOException {
    final int frontier = 1 << Lucene54DocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT;
    for (int maxDoc = frontier - 1; maxDoc <= frontier + 1; ++maxDoc) {
        final Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setMergePolicy(newLogMergePolicy()));
        RAMFile buffer = new RAMFile();
        RAMOutputStream out = new RAMOutputStream(buffer, false);
        Document doc = new Document();
        SortedNumericDocValuesField field1 = new SortedNumericDocValuesField("snum", 0L);
        doc.add(field1);
        SortedNumericDocValuesField field2 = new SortedNumericDocValuesField("snum", 0L);
        doc.add(field2);
        for (int i = 0; i < maxDoc; ++i) {
            long s1 = random().nextInt(100);
            long s2 = random().nextInt(100);
            field1.setLongValue(s1);
            field2.setLongValue(s2);
            w.addDocument(doc);
            out.writeVLong(Math.min(s1, s2));
            out.writeVLong(Math.max(s1, s2));
        }
        out.close();
        w.forceMerge(1);
        DirectoryReader r = DirectoryReader.open(w);
        w.close();
        LeafReader sr = getOnlyLeafReader(r);
        assertEquals(maxDoc, sr.maxDoc());
        SortedNumericDocValues values = sr.getSortedNumericDocValues("snum");
        assertNotNull(values);
        RAMInputStream in = new RAMInputStream("", buffer);
        for (int i = 0; i < maxDoc; ++i) {
            assertEquals(i, values.nextDoc());
            assertEquals(2, values.docValueCount());
            assertEquals(in.readVLong(), values.nextValue());
            assertEquals(in.readVLong(), values.nextValue());
        }
        r.close();
        dir.close();
    }
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) RAMInputStream(org.apache.lucene.store.RAMInputStream) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) Document(org.apache.lucene.document.Document) 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