Search in sources :

Example 1 with MatchNoneQueryBuilder

use of org.opensearch.index.query.MatchNoneQueryBuilder in project OpenSearch by opensearch-project.

the class FunctionScoreQueryBuilderTests method testCacheability.

/**
 * Check that this query is generally cacheable except for builders using {@link ScriptScoreFunctionBuilder} or
 * {@link RandomScoreFunctionBuilder} without a seed
 */
@Override
public void testCacheability() throws IOException {
    FunctionScoreQueryBuilder queryBuilder = createTestQueryBuilder();
    boolean isCacheable = isCacheable(queryBuilder);
    QueryShardContext context = createShardContext();
    QueryBuilder rewriteQuery = rewriteQuery(queryBuilder, new QueryShardContext(context));
    assertNotNull(rewriteQuery.toQuery(context));
    // we occasionally need to update the expected "isCacheable" flag after rewrite for MatchNoneQueryBuilder
    if (rewriteQuery instanceof MatchNoneQueryBuilder) {
        isCacheable = true;
    }
    assertEquals("query should " + (isCacheable ? "" : "not") + " be cacheable: " + queryBuilder.toString(), isCacheable, context.isCacheable());
    ScoreFunctionBuilder<?> scriptScoreFunction = new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, MockScriptEngine.NAME, "1", Collections.emptyMap()));
    queryBuilder = new FunctionScoreQueryBuilder(new FilterFunctionBuilder[] { new FilterFunctionBuilder(RandomQueryBuilder.createQuery(random()), scriptScoreFunction) });
    context = createShardContext();
    rewriteQuery = rewriteQuery(queryBuilder, new QueryShardContext(context));
    assertNotNull(rewriteQuery.toQuery(context));
    assertTrue("function script query should be cacheable" + queryBuilder.toString(), context.isCacheable());
    RandomScoreFunctionBuilder randomScoreFunctionBuilder = new RandomScoreFunctionBuilderWithFixedSeed();
    queryBuilder = new FunctionScoreQueryBuilder(new FilterFunctionBuilder[] { new FilterFunctionBuilder(RandomQueryBuilder.createQuery(random()), randomScoreFunctionBuilder) });
    context = createShardContext();
    rewriteQuery = rewriteQuery(queryBuilder, new QueryShardContext(context));
    assertNotNull(rewriteQuery.toQuery(context));
    assertFalse("function random query should not be cacheable: " + queryBuilder.toString(), context.isCacheable());
}
Also used : MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) Script(org.opensearch.script.Script) FilterFunctionBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) QueryShardContext(org.opensearch.index.query.QueryShardContext) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) RandomQueryBuilder(org.opensearch.index.query.RandomQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with MatchNoneQueryBuilder

use of org.opensearch.index.query.MatchNoneQueryBuilder in project OpenSearch by opensearch-project.

the class FunctionScoreQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
    QueryBuilder queryBuilder = this.query.rewrite(queryRewriteContext);
    if (queryBuilder instanceof MatchNoneQueryBuilder) {
        return queryBuilder;
    }
    FilterFunctionBuilder[] rewrittenBuilders = new FilterFunctionBuilder[this.filterFunctionBuilders.length];
    boolean rewritten = false;
    for (int i = 0; i < rewrittenBuilders.length; i++) {
        FilterFunctionBuilder rewrite = filterFunctionBuilders[i].rewrite(queryRewriteContext);
        rewritten |= rewrite != filterFunctionBuilders[i];
        rewrittenBuilders[i] = rewrite;
    }
    if (queryBuilder != query || rewritten) {
        FunctionScoreQueryBuilder newQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, rewrittenBuilders);
        newQueryBuilder.scoreMode = scoreMode;
        newQueryBuilder.minScore = minScore;
        newQueryBuilder.maxBoost = maxBoost;
        newQueryBuilder.boostMode = boostMode;
        return newQueryBuilder;
    }
    return this;
}
Also used : MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 3 with MatchNoneQueryBuilder

use of org.opensearch.index.query.MatchNoneQueryBuilder in project OpenSearch by opensearch-project.

the class SearchServiceTests method testMatchNoDocsEmptyResponse.

public void testMatchNoDocsEmptyResponse() throws InterruptedException {
    createIndex("index");
    Thread currentThread = Thread.currentThread();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(false).source(new SearchSourceBuilder().aggregation(AggregationBuilders.count("count").field("value")));
    ShardSearchRequest shardRequest = new ShardSearchRequest(OriginalIndices.NONE, searchRequest, indexShard.shardId(), 5, AliasFilter.EMPTY, 1.0f, 0, null, null);
    SearchShardTask task = new SearchShardTask(123L, "", "", "", null, Collections.emptyMap());
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.source().query(new MatchAllQueryBuilder());
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    assertNotSame(Thread.currentThread(), currentThread);
                    assertThat(Thread.currentThread().getName(), startsWith("opensearch[node_s_0][search]"));
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertFalse(result.queryResult().isNull());
                    assertNotNull(result.queryResult().topDocs());
                    assertNotNull(result.queryResult().aggregations());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception exc) {
                try {
                    throw new AssertionError(exc);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.source().query(new MatchNoneQueryBuilder());
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    assertNotSame(Thread.currentThread(), currentThread);
                    assertThat(Thread.currentThread().getName(), startsWith("opensearch[node_s_0][search]"));
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertFalse(result.queryResult().isNull());
                    assertNotNull(result.queryResult().topDocs());
                    assertNotNull(result.queryResult().aggregations());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception exc) {
                try {
                    throw new AssertionError(exc);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.canReturnNullResponseIfMatchNoDocs(true);
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    // make sure we don't use the search threadpool
                    assertSame(Thread.currentThread(), currentThread);
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertTrue(result.queryResult().isNull());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception e) {
                try {
                    throw new AssertionError(e);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) ActionListener(org.opensearch.action.ActionListener) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchShardTask(org.opensearch.action.search.SearchShardTask) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 4 with MatchNoneQueryBuilder

use of org.opensearch.index.query.MatchNoneQueryBuilder in project OpenSearch by opensearch-project.

the class SearchServiceTests method testCanRewriteToMatchNone.

public void testCanRewriteToMatchNone() {
    assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder()).aggregation(new GlobalAggregationBuilder("test"))));
    assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder()));
    assertFalse(SearchService.canRewriteToMatchNone(null));
    assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder()).aggregation(new TermsAggregationBuilder("test").userValueTypeHint(ValueType.STRING).minDocCount(0))));
    assertTrue(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new TermQueryBuilder("foo", "bar"))));
    assertTrue(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder()).aggregation(new TermsAggregationBuilder("test").userValueTypeHint(ValueType.STRING).minDocCount(1))));
    assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder()).aggregation(new TermsAggregationBuilder("test").userValueTypeHint(ValueType.STRING).minDocCount(1)).suggest(new SuggestBuilder())));
    assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new TermQueryBuilder("foo", "bar")).suggest(new SuggestBuilder())));
}
Also used : MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder)

Example 5 with MatchNoneQueryBuilder

use of org.opensearch.index.query.MatchNoneQueryBuilder in project OpenSearch by opensearch-project.

the class SamplerAggregatorTests method testEmptyParentBucket.

/**
 * Tests that the sampler aggregation works correctly if the parent bucket does not contain any hit.
 */
public void testEmptyParentBucket() throws Exception {
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    try (Directory dir = newDirectory();
        IndexWriter writer = new IndexWriter(dir, indexWriterConfig)) {
        writer.addDocument(new Document());
        try (IndexReader reader = DirectoryReader.open(writer)) {
            IndexSearcher searcher = new IndexSearcher(reader);
            QueryBuilder[] filters = new QueryBuilder[] { new MatchAllQueryBuilder(), new MatchNoneQueryBuilder() };
            FiltersAggregationBuilder samplerParent = new FiltersAggregationBuilder("filters", filters);
            TermsAggregationBuilder samplerChild = new TermsAggregationBuilder("child").field("field");
            SamplerAggregationBuilder sampler = new SamplerAggregationBuilder("sampler").subAggregation(samplerChild);
            samplerParent.subAggregation(sampler);
            InternalFilters response = searchAndReduce(searcher, new MatchAllDocsQuery(), samplerParent);
            assertEquals(response.getBuckets().size(), 2);
            assertEquals(response.getBuckets().get(0).getDocCount(), 1);
            assertEquals(response.getBuckets().get(1).getDocCount(), 0);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Aggregations

MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)21 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)9 QueryBuilder (org.opensearch.index.query.QueryBuilder)9 RangeQueryBuilder (org.opensearch.index.query.RangeQueryBuilder)8 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)7 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)5 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)4 SearchResponse (org.opensearch.action.search.SearchResponse)3 IndexService (org.opensearch.index.IndexService)3 IndexShard (org.opensearch.index.shard.IndexShard)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 BwViewFilter (org.bedework.calfacade.filter.BwViewFilter)2 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)2 MultiMatchQueryBuilder (org.opensearch.index.query.MultiMatchQueryBuilder)2 NestedQueryBuilder (org.opensearch.index.query.NestedQueryBuilder)2