Search in sources :

Example 66 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class DocValueFieldsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.collapse() != null) {
        // retrieve the `doc_value` associated with the collapse field
        String name = context.collapse().getFieldType().name();
        if (context.docValueFieldsContext() == null) {
            context.docValueFieldsContext(new DocValueFieldsContext(Collections.singletonList(name)));
        } else if (context.docValueFieldsContext().fields().contains(name) == false) {
            context.docValueFieldsContext().fields().add(name);
        }
    }
    if (context.docValueFieldsContext() == null) {
        return;
    }
    for (String field : context.docValueFieldsContext().fields()) {
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<>(2));
        }
        SearchHitField hitField = hitContext.hit().getFields().get(field);
        if (hitField == null) {
            hitField = new SearchHitField(field, new ArrayList<>(2));
            hitContext.hit().getFields().put(field, hitField);
        }
        MappedFieldType fieldType = context.mapperService().fullName(field);
        if (fieldType != null) {
            /* Because this is called once per document we end up creating a new ScriptDocValues for every document which is important
                 * because the values inside ScriptDocValues might be reused for different documents (Dates do this). */
            AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext());
            ScriptDocValues<?> values = data.getScriptValues();
            values.setNextDocId(hitContext.docId());
            hitField.getValues().addAll(values);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) AtomicFieldData(org.elasticsearch.index.fielddata.AtomicFieldData) SearchHitField(org.elasticsearch.search.SearchHitField)

Example 67 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class IndexShard method prepareDeleteOnPrimary.

public Engine.Delete prepareDeleteOnPrimary(String type, String id, long version, VersionType versionType) {
    verifyPrimary();
    final DocumentMapper documentMapper = docMapper(type).getDocumentMapper();
    final MappedFieldType uidFieldType = documentMapper.uidMapper().fieldType();
    final Query uidQuery = uidFieldType.termQuery(Uid.createUid(type, id), null);
    final Term uid = MappedFieldType.extractTerm(uidQuery);
    return prepareDelete(type, id, uid, SequenceNumbersService.UNASSIGNED_SEQ_NO, primaryTerm, version, versionType, Engine.Operation.Origin.PRIMARY);
}
Also used : Query(org.apache.lucene.search.Query) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 68 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class IndexShard method prepareDeleteOnReplica.

public Engine.Delete prepareDeleteOnReplica(String type, String id, long seqNo, long primaryTerm, long version, VersionType versionType) {
    verifyReplicationTarget();
    final DocumentMapper documentMapper = docMapper(type).getDocumentMapper();
    final MappedFieldType uidFieldType = documentMapper.uidMapper().fieldType();
    final Query uidQuery = uidFieldType.termQuery(Uid.createUid(type, id), null);
    final Term uid = MappedFieldType.extractTerm(uidQuery);
    return prepareDelete(type, id, uid, seqNo, primaryTerm, version, versionType, Engine.Operation.Origin.REPLICA);
}
Also used : Query(org.apache.lucene.search.Query) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 69 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class QueryStringQueryBuilder method allQueryableDefaultFields.

/**
     * Given a shard context, return a map of all fields in the mappings that
     * can be queried. The map will be field name to a float of 1.0f.
     */
public static Map<String, Float> allQueryableDefaultFields(QueryShardContext context) {
    Collection<String> allFields = context.simpleMatchToIndexNames("*");
    Map<String, Float> fields = new HashMap<>();
    for (String fieldName : allFields) {
        if (MapperService.isMetadataField(fieldName)) {
            // Ignore our metadata fields
            continue;
        }
        MappedFieldType mft = context.fieldMapper(fieldName);
        assert mft != null : "should never have a null mapper for an existing field";
        // Ignore fields that are not in the allowed mapper types. Some
        // types do not support term queries, and thus we cannot generate
        // a special query for them.
        String mappingType = mft.typeName();
        if (ALLOWED_QUERY_MAPPER_TYPES.contains(mappingType)) {
            fields.put(fieldName, 1.0f);
        }
    }
    return fields;
}
Also used : HashMap(java.util.HashMap) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

Example 70 with MappedFieldType

use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.

the class RangeQueryBuilder method getRelation.

// Overridable for testing only
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
    IndexReader reader = queryRewriteContext.getIndexReader();
    // rewrite so just pretend there is an intersection so that the rewrite is a noop
    if (reader == null) {
        return MappedFieldType.Relation.INTERSECTS;
    }
    final MapperService mapperService = queryRewriteContext.getMapperService();
    final MappedFieldType fieldType = mapperService.fullName(fieldName);
    if (fieldType == null) {
        // no field means we have no values
        return MappedFieldType.Relation.DISJOINT;
    } else {
        DateMathParser dateMathParser = format == null ? null : new DateMathParser(format);
        return fieldType.isFieldWithinQuery(queryRewriteContext.getIndexReader(), from, to, includeLower, includeUpper, timeZone, dateMathParser, queryRewriteContext);
    }
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) DateMathParser(org.elasticsearch.common.joda.DateMathParser) MapperService(org.elasticsearch.index.mapper.MapperService)

Aggregations

MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)122 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 IndexReader (org.apache.lucene.index.IndexReader)33 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)29 Directory (org.apache.lucene.store.Directory)29 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 Document (org.apache.lucene.document.Document)23 Query (org.apache.lucene.search.Query)19 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)17 Term (org.apache.lucene.index.Term)17 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 IndexableField (org.apache.lucene.index.IndexableField)9 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)9 ArrayList (java.util.ArrayList)8 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)8 TermQuery (org.apache.lucene.search.TermQuery)8 IndexNumericFieldData (org.elasticsearch.index.fielddata.IndexNumericFieldData)8 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)8 Analyzer (org.apache.lucene.analysis.Analyzer)7