Search in sources :

Example 1 with ParentFieldMapper

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

the class ParentFieldSubFetchPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) {
        return;
    }
    ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().getType()).parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        return;
    }
    String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId());
    if (parentId == null) {
        // hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document.
        return;
    }
    Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull();
    if (fields == null) {
        fields = new HashMap<>();
        hitContext.hit().fields(fields);
    }
    fields.put(ParentFieldMapper.NAME, new SearchHitField(ParentFieldMapper.NAME, Collections.singletonList(parentId)));
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) SearchHitField(org.elasticsearch.search.SearchHitField)

Example 2 with ParentFieldMapper

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

the class ParentIdQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    DocumentMapper childDocMapper = context.getMapperService().documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type());
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST);
    // Need to take child type into account, otherwise a child doc of different type with the same id could match
    query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER);
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) DocValuesTermsQuery(org.apache.lucene.search.DocValuesTermsQuery) Term(org.apache.lucene.index.Term)

Example 3 with ParentFieldMapper

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

the class ParentFieldSubFetchPhaseTests method testGetParentId.

public void testGetParentId() throws Exception {
    ParentFieldMapper fieldMapper = createParentFieldMapper();
    Directory directory = newDirectory();
    IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig());
    Document document = new Document();
    document.add(new SortedDocValuesField(fieldMapper.fieldType().name(), new BytesRef("1")));
    indexWriter.addDocument(document);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0);
    assertEquals("1", id);
    indexReader.close();
    directory.close();
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) IndexWriter(org.apache.lucene.index.IndexWriter) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 4 with ParentFieldMapper

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

the class ChildrenAggregationBuilder method resolveConfig.

@Override
protected ValuesSourceConfig<ParentChild> resolveConfig(SearchContext context) {
    ValuesSourceConfig<ParentChild> config = new ValuesSourceConfig<>(ValuesSourceType.BYTES);
    DocumentMapper childDocMapper = context.mapperService().documentMapper(childType);
    if (childDocMapper != null) {
        ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
        if (!parentFieldMapper.active()) {
            throw new IllegalArgumentException("[children] no [_parent] field not configured that points to a parent type");
        }
        parentType = parentFieldMapper.type();
        DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType);
        if (parentDocMapper != null) {
            parentFilter = parentDocMapper.typeFilter();
            childFilter = childDocMapper.typeFilter();
            ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType());
            config.fieldContext(new FieldContext(parentFieldMapper.fieldType().name(), parentChildIndexFieldData, parentFieldMapper.fieldType()));
        } else {
            config.unmapped(true);
        }
    } else {
        config.unmapped(true);
    }
    return config;
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ValuesSourceConfig(org.elasticsearch.search.aggregations.support.ValuesSourceConfig) FieldContext(org.elasticsearch.search.aggregations.support.FieldContext) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData) ParentChild(org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild)

Example 5 with ParentFieldMapper

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

the class HasChildQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query innerQuery;
    final String[] previousTypes = context.getTypes();
    context.setTypes(type);
    try {
        innerQuery = query.toQuery(context);
    } finally {
        context.setTypes(previousTypes);
    }
    DocumentMapper childDocMapper = context.documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String parentType = parentFieldMapper.type();
    DocumentMapper parentDocMapper = context.getMapperService().documentMapper(parentType);
    if (parentDocMapper == null) {
        throw new QueryShardException(context, "[" + NAME + "] Type [" + type + "] points to a non existent parent type [" + parentType + "]");
    }
    // wrap the query with type query
    innerQuery = Queries.filtered(innerQuery, childDocMapper.typeFilter());
    final ParentChildIndexFieldData parentChildIndexFieldData = context.getForField(parentFieldMapper.fieldType());
    return new LateParsingQuery(parentDocMapper.typeFilter(), innerQuery, minChildren(), maxChildren(), parentType, scoreMode, parentChildIndexFieldData, context.getSearchSimilarity());
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData)

Aggregations

ParentFieldMapper (org.elasticsearch.index.mapper.ParentFieldMapper)8 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)5 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 BytesRef (org.apache.lucene.util.BytesRef)3 ParentChildIndexFieldData (org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData)3 Document (org.apache.lucene.document.Document)2 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 Directory (org.apache.lucene.store.Directory)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 HashSet (java.util.HashSet)1 Term (org.apache.lucene.index.Term)1 DocValuesTermsQuery (org.apache.lucene.search.DocValuesTermsQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 MapperService (org.elasticsearch.index.mapper.MapperService)1 TypeFieldMapper (org.elasticsearch.index.mapper.TypeFieldMapper)1 SearchHitField (org.elasticsearch.search.SearchHitField)1