Search in sources :

Example 1 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 2 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 3 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 4 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 5 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project crate by crate.

the class LuceneOrderedDocCollectorTest method addDocToLucene.

private void addDocToLucene(IndexWriter w, Long value) throws IOException {
    Document doc = new Document();
    if (value != null) {
        doc.add(new LongFieldMapper.CustomLongNumericField(value, valueFieldType));
        doc.add(new SortedNumericDocValuesField("value", value));
    } else {
        // Create a placeholder field
        doc.add(new SortedDocValuesField("null_value", new BytesRef("null")));
    }
    w.addDocument(doc);
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) LongFieldMapper(org.elasticsearch.index.mapper.core.LongFieldMapper) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)109 Document (org.apache.lucene.document.Document)90 Directory (org.apache.lucene.store.Directory)72 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)45 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 StringField (org.apache.lucene.document.StringField)19 IndexSearcher (org.apache.lucene.search.IndexSearcher)19 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)19 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)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 StoredField (org.apache.lucene.document.StoredField)13 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12