Search in sources :

Example 1 with StoredField

use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.

the class IpFieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    Object addressAsObject;
    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))));
    }
}
Also used : StoredField(org.apache.lucene.document.StoredField) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) InetAddress(java.net.InetAddress) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with StoredField

use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.

the class SourceFieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (!enabled) {
        return;
    }
    if (!fieldType().stored()) {
        return;
    }
    BytesReference source = context.sourceToParse().source();
    // Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
    if (source == null) {
        return;
    }
    if (filter != null) {
        // we don't update the context source if we filter, we want to keep it as is...
        Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(source, true, context.sourceToParse().getXContentType());
        Map<String, Object> filteredSource = filter.apply(mapTuple.v2());
        BytesStreamOutput bStream = new BytesStreamOutput();
        XContentType contentType = mapTuple.v1();
        XContentBuilder builder = XContentFactory.contentBuilder(contentType, bStream).map(filteredSource);
        builder.close();
        source = bStream.bytes();
    }
    BytesRef ref = source.toBytesRef();
    fields.add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StoredField(org.apache.lucene.document.StoredField) XContentType(org.elasticsearch.common.xcontent.XContentType) Map(java.util.Map) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with StoredField

use of org.apache.lucene.document.StoredField 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 StoredField

use of org.apache.lucene.document.StoredField in project elasticsearch by elastic.

the class ScaledFloatFieldTypeTests method testStats.

public void testStats() throws IOException {
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setName("scaled_float");
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        assertNull(ft.stats(reader));
    }
    Document doc = new Document();
    doc.add(new StoredField("scaled_float", -1));
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        // field exists, but has no point values
        FieldStats<?> stats = ft.stats(reader);
        assertFalse(stats.hasMinMax());
        assertNull(stats.getMinValue());
        assertNull(stats.getMaxValue());
    }
    LongPoint point = new LongPoint("scaled_float", -1);
    doc.add(point);
    w.addDocument(doc);
    point.setLongValue(10);
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        FieldStats<?> stats = ft.stats(reader);
        assertEquals(-1 / ft.getScalingFactor(), stats.getMinValue());
        assertEquals(10 / ft.getScalingFactor(), stats.getMaxValue());
        assertEquals(3, stats.getMaxDoc());
    }
    w.deleteAll();
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        assertNull(ft.stats(reader));
    }
    IOUtils.close(w, dir);
}
Also used : StoredField(org.apache.lucene.document.StoredField) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 5 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class RandomSpatialOpFuzzyPrefixTreeTest method newDoc.

//Override so we can index parts of a pair separately, resulting in the detailLevel
// being independent for each shape vs the whole thing
@Override
protected Document newDoc(String id, Shape shape) {
    Document doc = new Document();
    doc.add(new StringField("id", id, Field.Store.YES));
    if (shape != null) {
        Collection<Shape> shapes;
        if (shape instanceof ShapePair) {
            shapes = new ArrayList<>(2);
            shapes.add(((ShapePair) shape).shape1);
            shapes.add(((ShapePair) shape).shape2);
        } else {
            shapes = Collections.singleton(shape);
        }
        for (Shape shapei : shapes) {
            for (Field f : strategy.createIndexableFields(shapei)) {
                doc.add(f);
            }
        }
        if (//just for diagnostics
        storeShape)
            doc.add(new StoredField(strategy.getFieldName(), shape.toString()));
    }
    return doc;
}
Also used : StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) StoredField(org.apache.lucene.document.StoredField) Shape(org.locationtech.spatial4j.shape.Shape) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document)

Aggregations

StoredField (org.apache.lucene.document.StoredField)140 Document (org.apache.lucene.document.Document)118 Directory (org.apache.lucene.store.Directory)77 StringField (org.apache.lucene.document.StringField)61 Field (org.apache.lucene.document.Field)52 BytesRef (org.apache.lucene.util.BytesRef)50 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)41 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)39 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)36 TextField (org.apache.lucene.document.TextField)34 IntPoint (org.apache.lucene.document.IntPoint)31 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)31 IndexReader (org.apache.lucene.index.IndexReader)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)26 TopDocs (org.apache.lucene.search.TopDocs)24 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)22 SortField (org.apache.lucene.search.SortField)22 FieldType (org.apache.lucene.document.FieldType)21 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)21 Sort (org.apache.lucene.search.Sort)21