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