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));
}
}
}
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);
}
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);
}
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);
}
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();
}
}
Aggregations