Example usage for org.apache.lucene.document StoredField StoredField

List of usage examples for org.apache.lucene.document StoredField StoredField

Introduction

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

Prototype

public StoredField(String name, double value) 

Source Link

Document

Create a stored-only field with the given double value.

Usage

From source file:org.elasticsearch.index.mapper.DateFieldMapper.java

License:Apache License

@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    String dateAsString;/* w  w w.  j a va2s. co  m*/
    if (context.externalValueSet()) {
        Object dateAsObject = context.externalValue();
        if (dateAsObject == null) {
            dateAsString = null;
        } else {
            dateAsString = dateAsObject.toString();
        }
    } else {
        dateAsString = context.parser().textOrNull();
    }

    if (dateAsString == null) {
        dateAsString = fieldType().nullValueAsString();
    }

    if (dateAsString == null) {
        return;
    }

    long timestamp;
    try {
        timestamp = fieldType().parse(dateAsString);
    } catch (IllegalArgumentException e) {
        if (ignoreMalformed.value()) {
            return;
        } else {
            throw e;
        }
    }

    if (context.includeInAll(includeInAll, this)) {
        context.allEntries().addText(fieldType().name(), dateAsString, fieldType().boost());
    }

    if (fieldType().indexOptions() != IndexOptions.NONE) {
        fields.add(new LongPoint(fieldType().name(), timestamp));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().name(), timestamp));
    }
    if (fieldType().stored()) {
        fields.add(new StoredField(fieldType().name(), timestamp));
    }
}

From source file:org.elasticsearch.index.mapper.internal.SourceFieldVisitor.java

License:Apache License

@Override
public Document createDocument() {
    Document document = new Document();
    if (source != null) {
        document.add(new StoredField(SourceFieldMapper.NAME, source));
    }/*from  w w  w  .j a  va 2 s.  c om*/
    return document;
}

From source file:org.elasticsearch.index.mapper.IpFieldMapper.java

License:Apache License

@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    Object addressAsObject;//  w  ww . j  a v  a  2 s. c  o m
    if (context.externalValueSet()) {
        addressAsObject = context.externalValue();
    } else {
        addressAsObject = context.parser().textOrNull();
    }

    if (addressAsObject == null) {
        addressAsObject = fieldType().nullValue();
    }

    if (addressAsObject == null) {
        return;
    }

    String addressAsString = addressAsObject.toString();
    InetAddress address;
    if (addressAsObject instanceof InetAddress) {
        address = (InetAddress) addressAsObject;
    } else {
        try {
            address = InetAddresses.forString(addressAsString);
        } catch (IllegalArgumentException e) {
            if (ignoreMalformed.value()) {
                return;
            } else {
                throw e;
            }
        }
    }

    if (context.includeInAll(includeInAll, this)) {
        context.allEntries().addText(fieldType().name(), addressAsString, fieldType().boost());
    }

    if (fieldType().indexOptions() != IndexOptions.NONE) {
        fields.add(new InetAddressPoint(fieldType().name(), address));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new SortedSetDocValuesField(fieldType().name(),
                new BytesRef(InetAddressPoint.encode(address))));
    }
    if (fieldType().stored()) {
        fields.add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
    }
}

From source file:org.elasticsearch.index.mapper.LatLonPointFieldMapper.java

License:Apache License

@Override
protected void parse(ParseContext originalContext, GeoPoint point, String geoHash) throws IOException {
    // Geopoint fields, by default, will not be included in _all
    final ParseContext context = originalContext.setIncludeInAllDefault(false);

    if (ignoreMalformed.value() == false) {
        if (point.lat() > 90.0 || point.lat() < -90.0) {
            throw new IllegalArgumentException("illegal latitude value [" + point.lat() + "] for " + name());
        }/*from  w ww.  ja va  2s  .  c  om*/
        if (point.lon() > 180.0 || point.lon() < -180) {
            throw new IllegalArgumentException("illegal longitude value [" + point.lon() + "] for " + name());
        }
    } else {
        GeoUtils.normalizePoint(point);
    }
    if (fieldType().indexOptions() != IndexOptions.NONE) {
        context.doc().add(new LatLonPoint(fieldType().name(), point.lat(), point.lon()));
    }
    if (fieldType().stored()) {
        context.doc().add(new StoredField(fieldType().name(), point.toString()));
    }
    if (fieldType.hasDocValues()) {
        context.doc().add(new LatLonDocValuesField(fieldType().name(), point.lat(), point.lon()));
    }
    // if the mapping contains multifields then use the geohash string
    if (multiFields.iterator().hasNext()) {
        multiFields.parse(this, context.createExternalValueContext(point.geohash()));
    }
}

From source file:org.elasticsearch.index.mapper.lucene.StoredNumericValuesTest.java

License:Apache License

@Test
public void testBytesAndNumericRepresentation() throws Exception {
    IndexWriter writer = new IndexWriter(new RAMDirectory(),
            new IndexWriterConfig(Lucene.VERSION, Lucene.STANDARD_ANALYZER));

    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field1").field("type", "integer").field("store", "yes").endObject()
            .startObject("field2").field("type", "float").field("store", "yes").endObject()
            .startObject("field3").field("type", "long").field("store", "yes").endObject().endObject()
            .endObject().endObject().string();
    DocumentMapper mapper = MapperTestUtils.newParser().parse(mapping);

    ParsedDocument doc = mapper.parse("type", "1",
            XContentFactory.jsonBuilder().startObject().field("field1", 1).field("field2", 1.1)
                    .startArray("field3").value(1).value(2).value(3).endArray().endObject().bytes());

    writer.addDocument(doc.rootDoc(), doc.analyzer());

    // Indexing a doc in the old way
    FieldType fieldType = new FieldType();
    fieldType.setStored(true);/*  ww w  .  jav a  2s.co  m*/
    fieldType.setNumericType(FieldType.NumericType.INT);
    Document doc2 = new Document();
    doc2.add(new StoredField("field1", new BytesRef(Numbers.intToBytes(1))));
    doc2.add(new StoredField("field2", new BytesRef(Numbers.floatToBytes(1.1f))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(1l))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(2l))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(3l))));
    writer.addDocument(doc2);

    DirectoryReader reader = DirectoryReader.open(writer, true);
    IndexSearcher searcher = new IndexSearcher(reader);

    Set<String> fields = new HashSet<String>(Arrays.asList("field1", "field2", "field3"));
    CustomFieldsVisitor fieldsVisitor = new CustomFieldsVisitor(fields, false);
    searcher.doc(0, fieldsVisitor);
    fieldsVisitor.postProcess(mapper);
    assertThat(fieldsVisitor.fields().size(), equalTo(3));
    assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
    assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
    assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
    assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3l));

    // Make sure the doc gets loaded as if it was stored in the new way
    fieldsVisitor.reset();
    searcher.doc(1, fieldsVisitor);
    fieldsVisitor.postProcess(mapper);
    assertThat(fieldsVisitor.fields().size(), equalTo(3));
    assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
    assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
    assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
    assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3l));

    reader.close();
    writer.close();
}

From source file:org.elasticsearch.index.mapper.lucene.StoredNumericValuesTests.java

License:Apache License

@Test
public void testBytesAndNumericRepresentation() throws Exception {
    IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Lucene.STANDARD_ANALYZER));

    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
            .startObject("field1").field("type", "integer").field("store", "yes").endObject()
            .startObject("field2").field("type", "float").field("store", "yes").endObject()
            .startObject("field3").field("type", "long").field("store", "yes").endObject().endObject()
            .endObject().endObject().string();
    DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser().parse("type",
            new CompressedXContent(mapping));

    ParsedDocument doc = mapper.parse("test", "type", "1",
            XContentFactory.jsonBuilder().startObject().field("field1", 1).field("field2", 1.1)
                    .startArray("field3").value(1).value(2).value(3).endArray().endObject().bytes());

    writer.addDocument(doc.rootDoc());/*from   ww w.j a  v a  2 s. com*/

    // Indexing a doc in the old way
    FieldType fieldType = new FieldType();
    fieldType.setStored(true);
    fieldType.setNumericType(FieldType.NumericType.INT);
    Document doc2 = new Document();
    doc2.add(new StoredField("field1", new BytesRef(Numbers.intToBytes(1))));
    doc2.add(new StoredField("field2", new BytesRef(Numbers.floatToBytes(1.1f))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(1l))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(2l))));
    doc2.add(new StoredField("field3", new BytesRef(Numbers.longToBytes(3l))));
    writer.addDocument(doc2);

    DirectoryReader reader = DirectoryReader.open(writer, true);
    IndexSearcher searcher = new IndexSearcher(reader);

    Set<String> fields = new HashSet<>(Arrays.asList("field1", "field2", "field3"));
    CustomFieldsVisitor fieldsVisitor = new CustomFieldsVisitor(fields, false);
    searcher.doc(0, fieldsVisitor);
    fieldsVisitor.postProcess(mapper);
    assertThat(fieldsVisitor.fields().size(), equalTo(3));
    assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
    assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
    assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
    assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3l));

    // Make sure the doc gets loaded as if it was stored in the new way
    fieldsVisitor.reset();
    searcher.doc(1, fieldsVisitor);
    fieldsVisitor.postProcess(mapper);
    assertThat(fieldsVisitor.fields().size(), equalTo(3));
    assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
    assertThat((Integer) fieldsVisitor.fields().get("field1").get(0), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
    assertThat((Float) fieldsVisitor.fields().get("field2").get(0), equalTo(1.1f));
    assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(3));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(0), equalTo(1l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(1), equalTo(2l));
    assertThat((Long) fieldsVisitor.fields().get("field3").get(2), equalTo(3l));

    reader.close();
    writer.close();
}

From source file:org.elasticsearch.index.mapper.selector.UidAndRoutingFieldVisitor.java

License:Apache License

@Override
public Document createDocument() {
    Document document = new Document();
    if (uid != null) {
        document.add(new StoredField(UidFieldMapper.NAME, uid));
    }/*  ww  w.java2s.c o m*/
    if (routing != null) {
        document.add(new StoredField(SourceFieldMapper.NAME, routing));
    }
    return document;
}

From source file:org.elasticsearch.index.mapper.selector.UidAndSourceFieldVisitor.java

License:Apache License

@Override
public Document createDocument() {
    Document document = new Document();
    if (uid != null) {
        document.add(new StoredField(UidFieldMapper.NAME, uid));
    }// w ww  . ja  v  a  2  s.  com
    if (source != null) {
        document.add(new StoredField(SourceFieldMapper.NAME, source));
    }
    return document;
}

From source file:org.elasticsearch.index.mapper.selector.UidFieldVisitor.java

License:Apache License

@Override
public Document createDocument() {
    Document document = new Document();
    if (uid != null) {
        document.add(new StoredField(UidFieldMapper.NAME, uid));
    }//  w  w w.  jav a 2 s  .  com
    return document;
}

From source file:org.elasticsearch.index.query.PercolateQueryTests.java

License:Apache License

void addPercolatorQuery(String id, Query query, String... extraFields) throws IOException {
    queries.put(id, query);/*  w w  w  .  jav a2s.  c  om*/
    ParseContext.Document document = new ParseContext.Document();
    ExtractQueryTermsService.extractQueryTerms(query, document, EXTRACTED_TERMS_FIELD_NAME,
            UNKNOWN_QUERY_FIELD_NAME, EXTRACTED_TERMS_FIELD_TYPE);
    document.add(
            new StoredField(UidFieldMapper.NAME, Uid.createUid(PercolatorFieldMapper.LEGACY_TYPE_NAME, id)));
    assert extraFields.length % 2 == 0;
    for (int i = 0; i < extraFields.length; i++) {
        document.add(new StringField(extraFields[i], extraFields[++i], Field.Store.NO));
    }
    indexWriter.addDocument(document);
}