Search in sources :

Example 6 with ObjectMapper

use of org.opensearch.index.mapper.ObjectMapper in project OpenSearch by opensearch-project.

the class NestedQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (context.allowExpensiveQueries() == false) {
        throw new OpenSearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false.");
    }
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new IllegalStateException("[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException("[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter(context.indexVersionCreated()));
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    // in its child space
    if (new NestedHelper(context.getMapperService()).mightMatchNonNestedDocs(innerQuery, path)) {
        innerQuery = Queries.filtered(innerQuery, nestedObjectMapper.nestedTypeFilter());
    }
    return new OpenSearchToParentBlockJoinQuery(innerQuery, parentFilter, scoreMode, objectMapper == null ? null : objectMapper.fullPath());
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ParentChildrenBlockJoinQuery(org.apache.lucene.search.join.ParentChildrenBlockJoinQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) BitSetProducer(org.apache.lucene.search.join.BitSetProducer) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) OpenSearchToParentBlockJoinQuery(org.opensearch.index.search.OpenSearchToParentBlockJoinQuery) OpenSearchException(org.opensearch.OpenSearchException) NestedHelper(org.opensearch.index.search.NestedHelper) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 7 with ObjectMapper

use of org.opensearch.index.mapper.ObjectMapper in project OpenSearch by opensearch-project.

the class NestedScope method nextLevel.

/**
 * Sets the new current nested level and pushes old current nested level down the stack returns that level.
 */
public ObjectMapper nextLevel(ObjectMapper level) {
    ObjectMapper previous = levelStack.peek();
    levelStack.push(level);
    return previous;
}
Also used : ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 8 with ObjectMapper

use of org.opensearch.index.mapper.ObjectMapper in project OpenSearch by opensearch-project.

the class ReverseNestedAggregationBuilder method doBuild.

@Override
protected AggregatorFactory doBuild(QueryShardContext queryShardContext, AggregatorFactory parent, Builder subFactoriesBuilder) throws IOException {
    if (findNestedAggregatorFactory(parent) == null) {
        throw new IllegalArgumentException("Reverse nested aggregation [" + name + "] can only be used inside a [nested] aggregation");
    }
    ObjectMapper parentObjectMapper = null;
    if (path != null) {
        parentObjectMapper = queryShardContext.getObjectMapper(path);
        if (parentObjectMapper == null) {
            return new ReverseNestedAggregatorFactory(name, true, null, queryShardContext, parent, subFactoriesBuilder, metadata);
        }
        if (parentObjectMapper.nested().isNested() == false) {
            throw new AggregationExecutionException("[reverse_nested] nested path [" + path + "] is not nested");
        }
    }
    NestedScope nestedScope = queryShardContext.nestedScope();
    try {
        nestedScope.nextLevel(parentObjectMapper);
        return new ReverseNestedAggregatorFactory(name, false, parentObjectMapper, queryShardContext, parent, subFactoriesBuilder, metadata);
    } finally {
        nestedScope.previousLevel();
    }
}
Also used : NestedScope(org.opensearch.index.query.support.NestedScope) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) ObjectMapper(org.opensearch.index.mapper.ObjectMapper)

Example 9 with ObjectMapper

use of org.opensearch.index.mapper.ObjectMapper in project OpenSearch by opensearch-project.

the class AbstractSortTestCase method createMockShardContext.

protected final QueryShardContext createMockShardContext(IndexSearcher searcher) {
    Index index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_");
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build());
    BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, Mockito.mock(BitsetFilterCache.Listener.class));
    TriFunction<MappedFieldType, String, Supplier<SearchLookup>, IndexFieldData<?>> indexFieldDataLookup = (fieldType, fieldIndexName, searchLookup) -> {
        IndexFieldData.Builder builder = fieldType.fielddataBuilder(fieldIndexName, searchLookup);
        return builder.build(new IndexFieldDataCache.None(), null);
    };
    return new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, bitsetFilterCache, indexFieldDataLookup, null, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, searcher, () -> randomNonNegativeLong(), null, null, () -> true, null) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            return provideMappedFieldType(name);
        }

        @Override
        public ObjectMapper getObjectMapper(String name) {
            BuilderContext context = new BuilderContext(this.getIndexSettings().getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(name).nested(Nested.newNested()).build(context);
        }
    };
}
Also used : ScriptModule(org.opensearch.script.ScriptModule) ToXContent(org.opensearch.common.xcontent.ToXContent) ScriptEngine(org.opensearch.script.ScriptEngine) ContentPath(org.opensearch.index.mapper.ContentPath) Version(org.opensearch.Version) XContentParser(org.opensearch.common.xcontent.XContentParser) IndexFieldDataCache(org.opensearch.index.fielddata.IndexFieldDataCache) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) SortField(org.apache.lucene.search.SortField) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) AfterClass(org.junit.AfterClass) Index(org.opensearch.index.Index) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) SearchLookup(org.opensearch.search.lookup.SearchLookup) QueryBuilder(org.opensearch.index.query.QueryBuilder) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) XContentType(org.opensearch.common.xcontent.XContentType) BigArrays(org.opensearch.common.util.BigArrays) IndexSearcher(org.apache.lucene.search.IndexSearcher) TriFunction(org.opensearch.common.TriFunction) IndexSettingsModule(org.opensearch.test.IndexSettingsModule) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DocValueFormat(org.opensearch.search.DocValueFormat) ObjectMapper(org.opensearch.index.mapper.ObjectMapper) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Nested(org.opensearch.index.mapper.ObjectMapper.Nested) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) Environment(org.opensearch.env.Environment) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) EqualsHashCodeTestUtils.checkEqualsAndHashCode(org.opensearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode) Rewriteable(org.opensearch.index.query.Rewriteable) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) IOException(java.io.IOException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Mockito(org.mockito.Mockito) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Collections(java.util.Collections) SearchModule(org.opensearch.search.SearchModule) IndexSettings(org.opensearch.index.IndexSettings) QueryBuilder(org.opensearch.index.query.QueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Index(org.opensearch.index.Index) ContentPath(org.opensearch.index.mapper.ContentPath) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexFieldData(org.opensearch.index.fielddata.IndexFieldData) QueryShardContext(org.opensearch.index.query.QueryShardContext) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) Supplier(java.util.function.Supplier) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache)

Aggregations

ObjectMapper (org.opensearch.index.mapper.ObjectMapper)9 Query (org.apache.lucene.search.Query)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ToChildBlockJoinQuery (org.apache.lucene.search.join.ToChildBlockJoinQuery)2 XContentType (org.opensearch.common.xcontent.XContentType)2 IndexSettings (org.opensearch.index.IndexSettings)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 SearchHit (org.opensearch.search.SearchHit)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1