Search in sources :

Example 1 with BitsetFilterCache

use of org.opensearch.index.cache.bitset.BitsetFilterCache in project OpenSearch by opensearch-project.

the class ContextIndexSearcherTests method doTestContextIndexSearcher.

public void doTestContextIndexSearcher(boolean sparse, boolean deletions) throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(null));
    Document doc = new Document();
    StringField allowedField = new StringField("allowed", "yes", Field.Store.NO);
    doc.add(allowedField);
    StringField fooField = new StringField("foo", "bar", Field.Store.NO);
    doc.add(fooField);
    StringField deleteField = new StringField("delete", "no", Field.Store.NO);
    doc.add(deleteField);
    IntPoint pointField = new IntPoint("point", 1, 2);
    doc.add(pointField);
    w.addDocument(doc);
    if (deletions) {
        // add a document that matches foo:bar but will be deleted
        deleteField.setStringValue("yes");
        w.addDocument(doc);
        deleteField.setStringValue("no");
    }
    allowedField.setStringValue("no");
    w.addDocument(doc);
    if (sparse) {
        for (int i = 0; i < 1000; ++i) {
            w.addDocument(doc);
        }
        w.forceMerge(1);
    }
    w.deleteDocuments(new Term("delete", "yes"));
    IndexSettings settings = IndexSettingsModule.newIndexSettings("_index", Settings.EMPTY);
    BitsetFilterCache.Listener listener = new BitsetFilterCache.Listener() {

        @Override
        public void onCache(ShardId shardId, Accountable accountable) {
        }

        @Override
        public void onRemoval(ShardId shardId, Accountable accountable) {
        }
    };
    DirectoryReader reader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(w), new ShardId(settings.getIndex(), 0));
    BitsetFilterCache cache = new BitsetFilterCache(settings, listener);
    Query roleQuery = new TermQuery(new Term("allowed", "yes"));
    BitSet bitSet = cache.getBitSetProducer(roleQuery).getBitSet(reader.leaves().get(0));
    if (sparse) {
        assertThat(bitSet, instanceOf(SparseFixedBitSet.class));
    } else {
        assertThat(bitSet, instanceOf(FixedBitSet.class));
    }
    DocumentSubsetDirectoryReader filteredReader = new DocumentSubsetDirectoryReader(reader, cache, roleQuery);
    ContextIndexSearcher searcher = new ContextIndexSearcher(filteredReader, IndexSearcher.getDefaultSimilarity(), IndexSearcher.getDefaultQueryCache(), IndexSearcher.getDefaultQueryCachingPolicy(), true);
    for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
        assertThat(context.reader(), instanceOf(SequentialStoredFieldsLeafReader.class));
        SequentialStoredFieldsLeafReader lf = (SequentialStoredFieldsLeafReader) context.reader();
        assertNotNull(lf.getSequentialStoredFieldsReader());
    }
    // Assert wrapping
    assertEquals(ExitableDirectoryReader.class, searcher.getIndexReader().getClass());
    for (LeafReaderContext lrc : searcher.getIndexReader().leaves()) {
        assertEquals(ExitableLeafReader.class, lrc.reader().getClass());
        assertNotEquals(ExitableTerms.class, lrc.reader().terms("foo").getClass());
        assertNotEquals(ExitablePointValues.class, lrc.reader().getPointValues("point").getClass());
    }
    searcher.addQueryCancellation(() -> {
    });
    for (LeafReaderContext lrc : searcher.getIndexReader().leaves()) {
        assertEquals(ExitableTerms.class, lrc.reader().terms("foo").getClass());
        assertEquals(ExitablePointValues.class, lrc.reader().getPointValues("point").getClass());
    }
    // Searching a non-existing term will trigger a null scorer
    assertEquals(0, searcher.count(new TermQuery(new Term("non_existing_field", "non_existing_value"))));
    assertEquals(1, searcher.count(new TermQuery(new Term("foo", "bar"))));
    // make sure scorers are created only once, see #1725
    assertEquals(1, searcher.count(new CreateScorerOnceQuery(new MatchAllDocsQuery())));
    TopDocs topDocs = searcher.search(new BoostQuery(new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), 3f), 1);
    assertEquals(1, topDocs.totalHits.value);
    assertEquals(1, topDocs.scoreDocs.length);
    assertEquals(3f, topDocs.scoreDocs[0].score, 0);
    IOUtils.close(reader, w, dir);
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IndexSettings(org.opensearch.index.IndexSettings) Document(org.apache.lucene.document.Document) BoostQuery(org.apache.lucene.search.BoostQuery) ShardId(org.opensearch.index.shard.ShardId) TopDocs(org.apache.lucene.search.TopDocs) SparseFixedBitSet(org.apache.lucene.util.SparseFixedBitSet) SparseFixedBitSet(org.apache.lucene.util.SparseFixedBitSet) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Directory(org.apache.lucene.store.Directory) TermQuery(org.apache.lucene.search.TermQuery) SequentialStoredFieldsLeafReader(org.opensearch.common.lucene.index.SequentialStoredFieldsLeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) OpenSearchDirectoryReader(org.opensearch.common.lucene.index.OpenSearchDirectoryReader) Accountable(org.apache.lucene.util.Accountable) BitSet(org.apache.lucene.util.BitSet) ContextIndexSearcher.intersectScorerAndBitSet(org.opensearch.search.internal.ContextIndexSearcher.intersectScorerAndBitSet) SparseFixedBitSet(org.apache.lucene.util.SparseFixedBitSet) FixedBitSet(org.apache.lucene.util.FixedBitSet) CombinedBitSet(org.apache.lucene.util.CombinedBitSet) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IntPoint(org.apache.lucene.document.IntPoint) IntPoint(org.apache.lucene.document.IntPoint) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache)

Example 2 with BitsetFilterCache

use of org.opensearch.index.cache.bitset.BitsetFilterCache 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)

Example 3 with BitsetFilterCache

use of org.opensearch.index.cache.bitset.BitsetFilterCache in project OpenSearch by opensearch-project.

the class AggregatorTestCase method createSearchContext.

protected SearchContext createSearchContext(IndexSearcher indexSearcher, IndexSettings indexSettings, Query query, MultiBucketConsumer bucketConsumer, CircuitBreakerService circuitBreakerService, MappedFieldType... fieldTypes) throws IOException {
    QueryCache queryCache = new DisabledQueryCache(indexSettings);
    QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {

        @Override
        public void onUse(Query query) {
        }

        @Override
        public boolean shouldCache(Query query) {
            // never cache a query
            return false;
        }
    };
    ContextIndexSearcher contextIndexSearcher = new ContextIndexSearcher(indexSearcher.getIndexReader(), indexSearcher.getSimilarity(), queryCache, queryCachingPolicy, false);
    SearchContext searchContext = mock(SearchContext.class);
    when(searchContext.numberOfShards()).thenReturn(1);
    when(searchContext.searcher()).thenReturn(contextIndexSearcher);
    when(searchContext.fetchPhase()).thenReturn(new FetchPhase(Arrays.asList(new FetchSourcePhase(), new FetchDocValuesPhase())));
    when(searchContext.bitsetFilterCache()).thenReturn(new BitsetFilterCache(indexSettings, mock(Listener.class)));
    IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(new ShardId("test", "test", 0));
    when(searchContext.indexShard()).thenReturn(indexShard);
    when(searchContext.aggregations()).thenReturn(new SearchContextAggregations(AggregatorFactories.EMPTY, bucketConsumer));
    when(searchContext.query()).thenReturn(query);
    /*
         * Always use the circuit breaking big arrays instance so that the CircuitBreakerService
         * we're passed gets a chance to break.
         */
    BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), circuitBreakerService).withCircuitBreaking();
    when(searchContext.bigArrays()).thenReturn(bigArrays);
    // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests:
    MapperService mapperService = mapperServiceMock();
    when(mapperService.getIndexSettings()).thenReturn(indexSettings);
    when(mapperService.hasNested()).thenReturn(false);
    when(searchContext.mapperService()).thenReturn(mapperService);
    IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
    }), circuitBreakerService, mapperService);
    QueryShardContext queryShardContext = queryShardContextMock(contextIndexSearcher, mapperService, indexSettings, circuitBreakerService, bigArrays);
    when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
    when(queryShardContext.getObjectMapper(anyString())).thenAnswer(invocation -> {
        String fieldName = (String) invocation.getArguments()[0];
        if (fieldName.startsWith(NESTEDFIELD_PREFIX)) {
            BuilderContext context = new BuilderContext(indexSettings.getSettings(), new ContentPath());
            return new ObjectMapper.Builder<>(fieldName).nested(Nested.newNested()).build(context);
        }
        return null;
    });
    Map<String, MappedFieldType> fieldNameToType = new HashMap<>();
    fieldNameToType.putAll(Arrays.stream(fieldTypes).filter(Objects::nonNull).collect(Collectors.toMap(MappedFieldType::name, Function.identity())));
    fieldNameToType.putAll(getFieldAliases(fieldTypes));
    registerFieldTypes(searchContext, mapperService, fieldNameToType);
    doAnswer(invocation -> {
        /* Store the release-ables so we can release them at the end of the test case. This is important because aggregations don't
             * close their sub-aggregations. This is fairly similar to what the production code does. */
        releasables.add((Releasable) invocation.getArguments()[0]);
        return null;
    }).when(searchContext).addReleasable(any());
    return searchContext;
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) QueryCache(org.apache.lucene.search.QueryCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) Listener(org.opensearch.index.cache.bitset.BitsetFilterCache.Listener) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Builder(org.opensearch.search.aggregations.AggregatorFactories.Builder) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) ContextIndexSearcher(org.opensearch.search.internal.ContextIndexSearcher) SearchContext(org.opensearch.search.internal.SearchContext) MockBigArrays(org.opensearch.common.util.MockBigArrays) Mockito.anyString(org.mockito.Mockito.anyString) ShardId(org.opensearch.index.shard.ShardId) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexShard(org.opensearch.index.shard.IndexShard) FetchSourcePhase(org.opensearch.search.fetch.subphase.FetchSourcePhase) FetchDocValuesPhase(org.opensearch.search.fetch.subphase.FetchDocValuesPhase) ContentPath(org.opensearch.index.mapper.ContentPath) IndicesFieldDataCache(org.opensearch.indices.fielddata.cache.IndicesFieldDataCache) MockBigArrays(org.opensearch.common.util.MockBigArrays) BigArrays(org.opensearch.common.util.BigArrays) IndexFieldDataService(org.opensearch.index.fielddata.IndexFieldDataService) FetchPhase(org.opensearch.search.fetch.FetchPhase) Objects(java.util.Objects) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) BitsetFilterCache(org.opensearch.index.cache.bitset.BitsetFilterCache) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) MapperService(org.opensearch.index.mapper.MapperService)

Aggregations

BitsetFilterCache (org.opensearch.index.cache.bitset.BitsetFilterCache)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Query (org.apache.lucene.search.Query)2 BigArrays (org.opensearch.common.util.BigArrays)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 IndexSettings (org.opensearch.index.IndexSettings)2 ContentPath (org.opensearch.index.mapper.ContentPath)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 BuilderContext (org.opensearch.index.mapper.Mapper.BuilderContext)2 QueryShardContext (org.opensearch.index.query.QueryShardContext)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Document (org.apache.lucene.document.Document)1 IntPoint (org.apache.lucene.document.IntPoint)1