Search in sources :

Example 1 with Field

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

the class FieldMapper method parse.

/**
     * Parse using the provided {@link ParseContext} and return a mapping
     * update if dynamic mappings modified the mappings, or {@code null} if
     * mappings were not modified.
     */
public Mapper parse(ParseContext context) throws IOException {
    final List<IndexableField> fields = new ArrayList<>(2);
    try {
        parseCreateField(context, fields);
        for (IndexableField field : fields) {
            if (!customBoost() && // don't set boosts eg. on dv fields
            field.fieldType().indexOptions() != IndexOptions.NONE && indexCreatedVersion.before(Version.V_5_0_0_alpha1)) {
                ((Field) (field)).setBoost(fieldType().boost());
            }
            context.doc().add(field);
        }
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
    }
    multiFields.parse(this, context);
    return null;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with Field

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

the class GeoShapeFieldMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    try {
        Shape shape = context.parseExternalValue(Shape.class);
        if (shape == null) {
            ShapeBuilder shapeBuilder = ShapeBuilder.parse(context.parser(), this);
            if (shapeBuilder == null) {
                return null;
            }
            shape = shapeBuilder.build();
        }
        if (fieldType().pointsOnly() && !(shape instanceof Point)) {
            throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry) shape).getGeom().getGeometryType() : shape.getClass()) + " was found");
        }
        Field[] fields = fieldType().defaultStrategy().createIndexableFields(shape);
        if (fields == null || fields.length == 0) {
            return null;
        }
        for (Field field : fields) {
            if (!customBoost() && fieldType.boost() != 1f && Version.indexCreated(context.indexSettings()).before(Version.V_5_0_0_alpha1)) {
                field.setBoost(fieldType().boost());
            }
            context.doc().add(field);
        }
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
    }
    return null;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Shape(org.locationtech.spatial4j.shape.Shape) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Point(org.locationtech.spatial4j.shape.Point) QueryShardException(org.elasticsearch.index.query.QueryShardException) IOException(java.io.IOException)

Example 3 with Field

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

the class UidFieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    Field uid = new Field(NAME, Uid.createUid(context.sourceToParse().type(), context.sourceToParse().id()), Defaults.FIELD_TYPE);
    fields.add(uid);
    if (fieldType().hasDocValues()) {
        fields.add(new BinaryDocValuesField(NAME, new BytesRef(uid.stringValue())));
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) Field(org.apache.lucene.document.Field) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with Field

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

the class VersionFieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    // see InternalEngine.updateVersion to see where the real version value is set
    final Field version = new NumericDocValuesField(NAME, -1L);
    context.version(version);
    fields.add(version);
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField)

Example 5 with Field

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

the class SourceScoreOrderFragmentsBuilder method getFields.

@Override
protected Field[] getFields(IndexReader reader, int docId, String fieldName) throws IOException {
    // we know its low level reader, and matching docId, since that's how we call the highlighter with
    SourceLookup sourceLookup = searchContext.lookup().source();
    sourceLookup.setSegmentAndDocument((LeafReaderContext) reader.getContext(), docId);
    List<Object> values = sourceLookup.extractRawValues(mapper.fieldType().name());
    Field[] fields = new Field[values.size()];
    for (int i = 0; i < values.size(); i++) {
        fields[i] = new Field(mapper.fieldType().name(), values.get(i).toString(), TextField.TYPE_NOT_STORED);
    }
    return fields;
}
Also used : Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) SourceLookup(org.elasticsearch.search.lookup.SourceLookup)

Aggregations

Field (org.apache.lucene.document.Field)659 Document (org.apache.lucene.document.Document)524 TextField (org.apache.lucene.document.TextField)307 Directory (org.apache.lucene.store.Directory)265 FieldType (org.apache.lucene.document.FieldType)212 StringField (org.apache.lucene.document.StringField)194 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)177 Term (org.apache.lucene.index.Term)156 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)154 IndexSearcher (org.apache.lucene.search.IndexSearcher)141 StoredField (org.apache.lucene.document.StoredField)139 IndexReader (org.apache.lucene.index.IndexReader)137 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)131 BytesRef (org.apache.lucene.util.BytesRef)123 TermQuery (org.apache.lucene.search.TermQuery)103 IndexWriter (org.apache.lucene.index.IndexWriter)96 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)90 TopDocs (org.apache.lucene.search.TopDocs)88 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)82 Query (org.apache.lucene.search.Query)73