Search in sources :

Example 1 with WrapperQueryBuilder

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

the class SearchQueryIT method testPassQueryOrFilterAsJSONString.

public void testPassQueryOrFilterAsJSONString() throws Exception {
    createIndex("test");
    client().prepareIndex("test").setId("1").setSource("field1", "value1_1", "field2", "value2_1").setRefreshPolicy(IMMEDIATE).get();
    WrapperQueryBuilder wrapper = new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
    assertHitCount(client().prepareSearch().setQuery(wrapper).get(), 1L);
    BoolQueryBuilder bool = boolQuery().must(wrapper).must(new TermQueryBuilder("field2", "value2_1"));
    assertHitCount(client().prepareSearch().setQuery(bool).get(), 1L);
    WrapperQueryBuilder wrapperFilter = wrapperQuery("{ \"term\" : { \"field1\" : \"value1_1\" } }");
    assertHitCount(client().prepareSearch().setPostFilter(wrapperFilter).get(), 1L);
}
Also used : BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) QueryBuilders.spanMultiTermQueryBuilder(org.opensearch.index.query.QueryBuilders.spanMultiTermQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder)

Example 2 with WrapperQueryBuilder

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

the class HasChildQueryBuilderTests method testExtractInnerHitBuildersWithDuplicate.

public void testExtractInnerHitBuildersWithDuplicate() {
    final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder(CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), ScoreMode.None);
    queryBuilder.innerHit(new InnerHitBuilder("some_name"));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null)));
    assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage());
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 3 with WrapperQueryBuilder

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

the class HasParentQueryBuilderTests method testIgnoreUnmappedWithRewrite.

public void testIgnoreUnmappedWithRewrite() throws IOException {
    // WrapperQueryBuilder makes sure we always rewrite
    final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("unmapped", new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false);
    queryBuilder.ignoreUnmapped(true);
    QueryShardContext queryShardContext = createShardContext();
    Query query = queryBuilder.rewrite(queryShardContext).toQuery(queryShardContext);
    assertThat(query, notNullValue());
    assertThat(query, instanceOf(MatchNoDocsQuery.class));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) JoinQueryBuilders.hasParentQuery(org.opensearch.join.query.JoinQueryBuilders.hasParentQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) QueryShardContext(org.opensearch.index.query.QueryShardContext) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 4 with WrapperQueryBuilder

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

the class FunctionScoreQueryBuilderTests method testRewriteWithFunction.

public void testRewriteWithFunction() throws IOException {
    QueryBuilder firstFunction = new WrapperQueryBuilder(new TermQueryBuilder(KEYWORD_FIELD_NAME, "1").toString());
    TermQueryBuilder secondFunction = new TermQueryBuilder(KEYWORD_FIELD_NAME, "2");
    QueryBuilder queryBuilder = randomBoolean() ? new WrapperQueryBuilder(new TermQueryBuilder(TEXT_FIELD_NAME, "bar").toString()) : new TermQueryBuilder(TEXT_FIELD_NAME, "bar");
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(firstFunction, new RandomScoreFunctionBuilder()), new FunctionScoreQueryBuilder.FilterFunctionBuilder(secondFunction, new RandomScoreFunctionBuilder()) });
    FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
    assertNotSame(functionScoreQueryBuilder, rewrite);
    assertEquals(rewrite.query(), new TermQueryBuilder(TEXT_FIELD_NAME, "bar"));
    assertEquals(rewrite.filterFunctionBuilders()[0].getFilter(), new TermQueryBuilder(KEYWORD_FIELD_NAME, "1"));
    assertSame(rewrite.filterFunctionBuilders()[1].getFilter(), secondFunction);
}
Also used : WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) 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) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) FilterFunctionBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)

Example 5 with WrapperQueryBuilder

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

the class AggregatorFactoriesTests method testRewriteAggregation.

public void testRewriteAggregation() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference bytesReference;
    try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
        builder.startObject();
        {
            builder.startObject("terms");
            {
                builder.array("title", "foo");
            }
            builder.endObject();
        }
        builder.endObject();
        bytesReference = BytesReference.bytes(builder);
    }
    FilterAggregationBuilder filterAggBuilder = new FilterAggregationBuilder("titles", new WrapperQueryBuilder(bytesReference));
    BucketScriptPipelineAggregationBuilder pipelineAgg = new BucketScriptPipelineAggregationBuilder("const", new Script("1"));
    AggregatorFactories.Builder builder = new AggregatorFactories.Builder().addAggregator(filterAggBuilder).addPipelineAggregator(pipelineAgg);
    AggregatorFactories.Builder rewritten = builder.rewrite(new QueryRewriteContext(xContentRegistry, null, null, () -> 0L));
    assertNotSame(builder, rewritten);
    Collection<AggregationBuilder> aggregatorFactories = rewritten.getAggregatorFactories();
    assertEquals(1, aggregatorFactories.size());
    assertThat(aggregatorFactories.iterator().next(), instanceOf(FilterAggregationBuilder.class));
    FilterAggregationBuilder rewrittenFilterAggBuilder = (FilterAggregationBuilder) aggregatorFactories.iterator().next();
    assertNotSame(filterAggBuilder, rewrittenFilterAggBuilder);
    assertNotEquals(filterAggBuilder, rewrittenFilterAggBuilder);
    // Check the filter was rewritten from a wrapper query to a terms query
    QueryBuilder rewrittenFilter = rewrittenFilterAggBuilder.getFilter();
    assertThat(rewrittenFilter, instanceOf(TermsQueryBuilder.class));
    // Check that a further rewrite returns the same aggregation factories builder
    AggregatorFactories.Builder secondRewritten = rewritten.rewrite(new QueryRewriteContext(xContentRegistry, null, null, () -> 0L));
    assertSame(rewritten, secondRewritten);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) Script(org.opensearch.script.Script) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AbstractPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) XContentType(org.opensearch.common.xcontent.XContentType) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)12 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)9 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)5 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)4 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 BytesReference (org.opensearch.common.bytes.BytesReference)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 XContentType (org.opensearch.common.xcontent.XContentType)2 IdsQueryBuilder (org.opensearch.index.query.IdsQueryBuilder)2 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)2 HashMap (java.util.HashMap)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 OpenSearchException (org.opensearch.OpenSearchException)1 SearchResponse (org.opensearch.action.search.SearchResponse)1