Example usage for org.apache.lucene.document InetAddressPoint encode

List of usage examples for org.apache.lucene.document InetAddressPoint encode

Introduction

In this page you can find the example usage for org.apache.lucene.document InetAddressPoint encode.

Prototype

public static byte[] encode(InetAddress value) 

Source Link

Document

Encode InetAddress value into binary encoding

Usage

From source file:io.crate.expression.reference.doc.IpColumnReferenceTest.java

License:Apache License

private static void addIPv6Values(IndexWriter writer) throws IOException {
    for (int i = 10; i < 20; i++) {
        Document doc = new Document();
        doc.add(new StringField("_id", Integer.toString(i), Field.Store.NO));
        InetAddress address = InetAddresses
                .forString("7bd0:8082:2df8:487e:e0df:e7b5:9362:" + Integer.toHexString(i));
        doc.add(new SortedSetDocValuesField(IP_COLUMN, new BytesRef(InetAddressPoint.encode(address))));
        writer.addDocument(doc);//from  ww w. ja  v  a2s  . com
    }
}

From source file:io.crate.expression.reference.doc.IpColumnReferenceTest.java

License:Apache License

private static void addIPv4Values(IndexWriter writer) throws IOException {
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("_id", Integer.toString(i), Field.Store.NO));
        InetAddress address = InetAddresses.forString("192.168.0." + i);
        doc.add(new SortedSetDocValuesField(IP_COLUMN, new BytesRef(InetAddressPoint.encode(address))));
        if (i == 0) {
            address = InetAddresses.forString("192.168.0.1");
            doc.add(new SortedSetDocValuesField(IP_ARRAY_COLUMN,
                    new BytesRef(InetAddressPoint.encode(address))));
            address = InetAddresses.forString("192.168.0.2");
            doc.add(new SortedSetDocValuesField(IP_ARRAY_COLUMN,
                    new BytesRef(InetAddressPoint.encode(address))));
        }//w w w .jav  a 2 s.  c om
        writer.addDocument(doc);
    }
}

From source file:io.crate.operation.reference.doc.IpColumnReferenceTest.java

License:Apache License

@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        InetAddress address = InetAddresses.forString("192.168.0." + i);
        doc.add(new SortedSetDocValuesField(column, new BytesRef(InetAddressPoint.encode(address))));
        if (i == 0) {
            address = InetAddresses.forString("192.168.0.1");
            doc.add(new SortedSetDocValuesField(column_array, new BytesRef(InetAddressPoint.encode(address))));
            address = InetAddresses.forString("192.168.0.2");
            doc.add(new SortedSetDocValuesField(column_array, new BytesRef(InetAddressPoint.encode(address))));
        }/*  w  ww  .  j  a  v a  2  s .  co m*/
        writer.addDocument(doc);
    }
}

From source file:org.codelibs.elasticsearch.search.aggregations.bucket.range.ip.IpRangeAggregationBuilder.java

License:Apache License

private static BytesRef toBytesRef(String ip) {
    if (ip == null) {
        return null;
    }//from  w  w w  .  ja  v  a2 s.  c  o m
    InetAddress address = InetAddresses.forString(ip);
    byte[] bytes = InetAddressPoint.encode(address);
    return new BytesRef(bytes);
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldMapperTests.java

License:Apache License

public void testDefaults() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field").field("type", "ip").endObject().endObject().endObject().endObject().string();

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));

    assertEquals(mapping, mapper.mappingSource().toString());

    ParsedDocument doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().field("field", "::1").endObject().bytes());

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);/*from  w ww.  j a v  a  2s.  c  om*/
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(16, pointField.fieldType().pointNumBytes());
    assertFalse(pointField.fieldType().stored());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))),
            pointField.binaryValue());
    IndexableField dvField = fields[1];
    assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), dvField.binaryValue());
    assertFalse(dvField.fieldType().stored());
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldMapperTests.java

License:Apache License

public void testNoDocValues() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field").field("type", "ip").field("doc_values", false).endObject().endObject()
            .endObject().endObject().string();

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));

    assertEquals(mapping, mapper.mappingSource().toString());

    ParsedDocument doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().field("field", "::1").endObject().bytes());

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);/*from w  ww  .  ja  va  2 s . c  o  m*/
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))),
            pointField.binaryValue());
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldMapperTests.java

License:Apache License

public void testStore() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field").field("type", "ip").field("store", true).endObject().endObject().endObject()
            .endObject().string();//from w  w w .ja  v  a  2 s .  c  o m

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));

    assertEquals(mapping, mapper.mappingSource().toString());

    ParsedDocument doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().field("field", "::1").endObject().bytes());

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(3, fields.length);
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    IndexableField dvField = fields[1];
    assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType());
    IndexableField storedField = fields[2];
    assertTrue(storedField.fieldType().stored());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddress.getByName("::1"))),
            storedField.binaryValue());
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldMapperTests.java

License:Apache License

public void testNullValue() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field").field("type", "ip").endObject().endObject().endObject().endObject().string();

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());

    ParsedDocument doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().nullField("field").endObject().bytes());
    assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field"));

    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field").field("type", "ip").field("null_value", "::1").endObject().endObject()
            .endObject().endObject().string();

    mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, mapper.mappingSource().toString());

    doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().nullField("field").endObject().bytes());
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(2, fields.length);//from w  ww.  ja  va 2  s.  c  om
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(16, pointField.fieldType().pointNumBytes());
    assertFalse(pointField.fieldType().stored());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))),
            pointField.binaryValue());
    IndexableField dvField = fields[1];
    assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType());
    assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), dvField.binaryValue());
    assertFalse(dvField.fieldType().stored());
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldTypeTests.java

License:Apache License

public void testValueFormat() throws Exception {
    MappedFieldType ft = createDefaultFieldType();
    String ip = "2001:db8::2:1";
    BytesRef asBytes = new BytesRef(InetAddressPoint.encode(InetAddress.getByName(ip)));
    assertEquals(ip, ft.docValueFormat(null, null).format(asBytes));

    ip = "192.168.1.7";
    asBytes = new BytesRef(InetAddressPoint.encode(InetAddress.getByName(ip)));
    assertEquals(ip, ft.docValueFormat(null, null).format(asBytes));
}

From source file:org.elasticsearch.index.mapper.ip.IpFieldTypeTests.java

License:Apache License

public void testValueForSearch() throws Exception {
    MappedFieldType ft = createDefaultFieldType();
    String ip = "2001:db8::2:1";
    BytesRef asBytes = new BytesRef(InetAddressPoint.encode(InetAddresses.forString(ip)));
    assertEquals(ip, ft.valueForSearch(asBytes));

    ip = "192.168.1.7";
    asBytes = new BytesRef(InetAddressPoint.encode(InetAddresses.forString(ip)));
    assertEquals(ip, ft.valueForSearch(asBytes));
}