Search in sources :

Example 1 with ObjectArrayList

use of com.carrotsearch.hppc.ObjectArrayList in project elasticsearch by elastic.

the class BinaryDVFieldDataTests method testDocValue.

public void testDocValue() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("test").startObject("properties").startObject("field").field("type", "binary").field("doc_values", true).endObject().endObject().endObject().endObject().string();
    final DocumentMapper mapper = mapperService.documentMapperParser().parse("test", new CompressedXContent(mapping));
    ObjectArrayList<byte[]> bytesList1 = new ObjectArrayList<>(2);
    bytesList1.add(randomBytes());
    bytesList1.add(randomBytes());
    XContentBuilder doc = XContentFactory.jsonBuilder().startObject().startArray("field").value(bytesList1.get(0)).value(bytesList1.get(1)).endArray().endObject();
    ParsedDocument d = mapper.parse("test", "test", "1", doc.bytes());
    writer.addDocument(d.rootDoc());
    byte[] bytes1 = randomBytes();
    doc = XContentFactory.jsonBuilder().startObject().field("field", bytes1).endObject();
    d = mapper.parse("test", "test", "2", doc.bytes());
    writer.addDocument(d.rootDoc());
    doc = XContentFactory.jsonBuilder().startObject().endObject();
    d = mapper.parse("test", "test", "3", doc.bytes());
    writer.addDocument(d.rootDoc());
    // test remove duplicate value
    ObjectArrayList<byte[]> bytesList2 = new ObjectArrayList<>(2);
    bytesList2.add(randomBytes());
    bytesList2.add(randomBytes());
    doc = XContentFactory.jsonBuilder().startObject().startArray("field").value(bytesList2.get(0)).value(bytesList2.get(1)).value(bytesList2.get(0)).endArray().endObject();
    d = mapper.parse("test", "test", "4", doc.bytes());
    writer.addDocument(d.rootDoc());
    List<LeafReaderContext> readers = refreshReader();
    IndexFieldData<?> indexFieldData = getForField("field");
    for (LeafReaderContext reader : readers) {
        AtomicFieldData fieldData = indexFieldData.load(reader);
        SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
        CollectionUtils.sortAndDedup(bytesList1);
        bytesValues.setDocument(0);
        assertThat(bytesValues.count(), equalTo(2));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(bytesList1.get(0))));
        assertThat(bytesValues.valueAt(1), equalTo(new BytesRef(bytesList1.get(1))));
        bytesValues.setDocument(1);
        assertThat(bytesValues.count(), equalTo(1));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(bytes1)));
        bytesValues.setDocument(2);
        assertThat(bytesValues.count(), equalTo(0));
        CollectionUtils.sortAndDedup(bytesList2);
        bytesValues.setDocument(3);
        assertThat(bytesValues.count(), equalTo(2));
        assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(bytesList2.get(0))));
        assertThat(bytesValues.valueAt(1), equalTo(new BytesRef(bytesList2.get(1))));
    }
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ObjectArrayList(com.carrotsearch.hppc.ObjectArrayList) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ObjectArrayList (com.carrotsearch.hppc.ObjectArrayList)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 BytesRef (org.apache.lucene.util.BytesRef)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1