Search in sources :

Example 1 with ParentChildIndexFieldData

use of org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData in project elasticsearch by elastic.

the class ParentChildFieldDataTests method testThreads.

public void testThreads() throws Exception {
    final ParentChildIndexFieldData indexFieldData = getForField(childType);
    final DirectoryReader reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId(new Index("test", ""), 0));
    final IndexParentChildFieldData global = indexFieldData.loadGlobal(reader);
    final AtomicReference<Exception> error = new AtomicReference<>();
    final int numThreads = scaledRandomIntBetween(3, 8);
    final Thread[] threads = new Thread[numThreads];
    final CountDownLatch latch = new CountDownLatch(1);
    final Map<Object, BytesRef[]> expected = new HashMap<>();
    for (LeafReaderContext context : reader.leaves()) {
        AtomicParentChildFieldData leafData = global.load(context);
        SortedDocValues parentIds = leafData.getOrdinalsValues(parentType);
        final BytesRef[] ids = new BytesRef[parentIds.getValueCount()];
        for (int j = 0; j < parentIds.getValueCount(); ++j) {
            final BytesRef id = parentIds.lookupOrd(j);
            if (id != null) {
                ids[j] = BytesRef.deepCopyOf(id);
            }
        }
        expected.put(context.reader().getCoreCacheKey(), ids);
    }
    for (int i = 0; i < numThreads; ++i) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                try {
                    latch.await();
                    for (int i = 0; i < 100000; ++i) {
                        for (LeafReaderContext context : reader.leaves()) {
                            AtomicParentChildFieldData leafData = global.load(context);
                            SortedDocValues parentIds = leafData.getOrdinalsValues(parentType);
                            final BytesRef[] expectedIds = expected.get(context.reader().getCoreCacheKey());
                            for (int j = 0; j < parentIds.getValueCount(); ++j) {
                                final BytesRef id = parentIds.lookupOrd(j);
                                assertEquals(expectedIds[j], id);
                            }
                        }
                    }
                } catch (Exception e) {
                    error.compareAndSet(null, e);
                }
            }
        };
        threads[i].start();
    }
    latch.countDown();
    for (Thread thread : threads) {
        thread.join();
    }
    if (error.get() != null) {
        throw error.get();
    }
}
Also used : ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData) CountDownLatch(java.util.concurrent.CountDownLatch) SortedDocValues(org.apache.lucene.index.SortedDocValues) ShardId(org.elasticsearch.index.shard.ShardId) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with ParentChildIndexFieldData

use of org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData 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 3 with ParentChildIndexFieldData

use of org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData 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)

Example 4 with ParentChildIndexFieldData

use of org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData in project elasticsearch by elastic.

the class HasParentQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query innerQuery;
    String[] previousTypes = context.getTypes();
    context.setTypes(type);
    try {
        innerQuery = query.toQuery(context);
    } finally {
        context.setTypes(previousTypes);
    }
    DocumentMapper parentDocMapper = context.documentMapper(type);
    if (parentDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] query configured 'parent_type' [" + type + "] is not a valid type");
        }
    }
    Set<String> childTypes = new HashSet<>();
    ParentChildIndexFieldData parentChildIndexFieldData = null;
    for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) {
        ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper();
        if (parentFieldMapper.active() && type.equals(parentFieldMapper.type())) {
            childTypes.add(documentMapper.type());
            parentChildIndexFieldData = context.getForField(parentFieldMapper.fieldType());
        }
    }
    if (childTypes.isEmpty()) {
        throw new QueryShardException(context, "[" + NAME + "] no child types found for type [" + type + "]");
    }
    Query childrenQuery;
    if (childTypes.size() == 1) {
        DocumentMapper documentMapper = context.getMapperService().documentMapper(childTypes.iterator().next());
        childrenQuery = documentMapper.typeFilter();
    } else {
        BooleanQuery.Builder childrenFilter = new BooleanQuery.Builder();
        for (String childrenTypeStr : childTypes) {
            DocumentMapper documentMapper = context.getMapperService().documentMapper(childrenTypeStr);
            childrenFilter.add(documentMapper.typeFilter(), BooleanClause.Occur.SHOULD);
        }
        childrenQuery = childrenFilter.build();
    }
    // wrap the query with type query
    innerQuery = Queries.filtered(innerQuery, parentDocMapper.typeFilter());
    return new HasChildQueryBuilder.LateParsingQuery(childrenQuery, innerQuery, HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, type, score ? ScoreMode.Max : ScoreMode.None, parentChildIndexFieldData, context.getSearchSimilarity());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParentChildIndexFieldData(org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) HashSet(java.util.HashSet)

Aggregations

ParentChildIndexFieldData (org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData)4 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)3 ParentFieldMapper (org.elasticsearch.index.mapper.ParentFieldMapper)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BytesRef (org.apache.lucene.util.BytesRef)1 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Index (org.elasticsearch.index.Index)1 ShardId (org.elasticsearch.index.shard.ShardId)1 FieldContext (org.elasticsearch.search.aggregations.support.FieldContext)1 ParentChild (org.elasticsearch.search.aggregations.support.ValuesSource.Bytes.ParentChild)1