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());
}
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;
}
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();
}
}
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())));
}
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);
}
}
}
Aggregations