Search in sources :

Example 1 with ConstantScoreQueryBuilder

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

the class NestedSortBuilderTests method testRewrite.

/**
 * Test that filters and inner nested sorts get rewritten
 */
public void testRewrite() throws IOException {
    QueryBuilder filterThatRewrites = new MatchNoneQueryBuilder() {

        @Override
        protected QueryBuilder doRewrite(org.opensearch.index.query.QueryRewriteContext queryShardContext) throws IOException {
            return new MatchAllQueryBuilder();
        }
    };
    // test that filter gets rewritten
    NestedSortBuilder original = new NestedSortBuilder("path").setFilter(filterThatRewrites);
    QueryRewriteContext mockRewriteContext = Mockito.mock(QueryRewriteContext.class);
    NestedSortBuilder rewritten = original.rewrite(mockRewriteContext);
    assertNotSame(rewritten, original);
    assertNotSame(rewritten.getFilter(), original.getFilter());
    // test that inner nested sort gets rewritten
    original = new NestedSortBuilder("path");
    original.setNestedSort(new NestedSortBuilder("otherPath").setFilter(filterThatRewrites));
    rewritten = original.rewrite(mockRewriteContext);
    assertNotSame(rewritten, original);
    assertNotSame(rewritten.getNestedSort(), original.getNestedSort());
    // test that both filter and inner nested sort get rewritten
    original = new NestedSortBuilder("path");
    original.setFilter(filterThatRewrites);
    original.setNestedSort(new NestedSortBuilder("otherPath").setFilter(filterThatRewrites));
    rewritten = original.rewrite(mockRewriteContext);
    assertNotSame(rewritten, original);
    assertNotSame(rewritten.getFilter(), original.getFilter());
    assertNotSame(rewritten.getNestedSort(), original.getNestedSort());
    // test that original stays unchanged if no element rewrites
    original = new NestedSortBuilder("path");
    original.setFilter(new MatchNoneQueryBuilder());
    original.setNestedSort(new NestedSortBuilder("otherPath").setFilter(new MatchNoneQueryBuilder()));
    rewritten = original.rewrite(mockRewriteContext);
    assertSame(rewritten, original);
    assertSame(rewritten.getFilter(), original.getFilter());
    assertSame(rewritten.getNestedSort(), original.getNestedSort());
    // test that rewrite works recursively
    original = new NestedSortBuilder("firstLevel");
    ConstantScoreQueryBuilder constantScoreQueryBuilder = new ConstantScoreQueryBuilder(filterThatRewrites);
    original.setFilter(constantScoreQueryBuilder);
    NestedSortBuilder nestedSortThatRewrites = new NestedSortBuilder("thirdLevel").setFilter(filterThatRewrites);
    original.setNestedSort(new NestedSortBuilder("secondLevel").setNestedSort(nestedSortThatRewrites));
    rewritten = original.rewrite(mockRewriteContext);
    assertNotSame(rewritten, original);
    assertNotSame(rewritten.getFilter(), constantScoreQueryBuilder);
    assertNotSame(((ConstantScoreQueryBuilder) rewritten.getFilter()).innerQuery(), constantScoreQueryBuilder.innerQuery());
    assertEquals("secondLevel", rewritten.getNestedSort().getPath());
    assertNotSame(rewritten.getNestedSort(), original.getNestedSort());
    assertEquals("thirdLevel", rewritten.getNestedSort().getNestedSort().getPath());
    assertNotSame(rewritten.getNestedSort().getNestedSort(), nestedSortThatRewrites);
}
Also used : MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with ConstantScoreQueryBuilder

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

the class PercolatorFieldMapperTests method testUnsupportedQueries.

public void testUnsupportedQueries() {
    RangeQueryBuilder rangeQuery1 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("2017-01-01||/D");
    RangeQueryBuilder rangeQuery2 = new RangeQueryBuilder("field").from("2016-01-01||/D").to("now");
    PercolatorFieldMapper.verifyQuery(rangeQuery1);
    PercolatorFieldMapper.verifyQuery(rangeQuery2);
    HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery)));
    PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1)));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(hasChildQuery)));
    PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(hasChildQuery, new MatchAllQueryBuilder())));
    PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder()));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(hasChildQuery, new RandomScoreFunctionBuilder())));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery)));
    HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false);
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery));
    expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery)));
}
Also used : FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder)

Example 3 with ConstantScoreQueryBuilder

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

the class PercolatorFieldMapper method verifyQuery.

/**
 * Fails if a percolator contains an unsupported query. The following queries are not supported:
 * 1) a has_child query
 * 2) a has_parent query
 */
static void verifyQuery(QueryBuilder queryBuilder) {
    if (queryBuilder.getName().equals("has_child")) {
        throw new IllegalArgumentException("the [has_child] query is unsupported inside a percolator query");
    } else if (queryBuilder.getName().equals("has_parent")) {
        throw new IllegalArgumentException("the [has_parent] query is unsupported inside a percolator query");
    } else if (queryBuilder instanceof BoolQueryBuilder) {
        BoolQueryBuilder boolQueryBuilder = (BoolQueryBuilder) queryBuilder;
        List<QueryBuilder> clauses = new ArrayList<>();
        clauses.addAll(boolQueryBuilder.filter());
        clauses.addAll(boolQueryBuilder.must());
        clauses.addAll(boolQueryBuilder.mustNot());
        clauses.addAll(boolQueryBuilder.should());
        for (QueryBuilder clause : clauses) {
            verifyQuery(clause);
        }
    } else if (queryBuilder instanceof ConstantScoreQueryBuilder) {
        verifyQuery(((ConstantScoreQueryBuilder) queryBuilder).innerQuery());
    } else if (queryBuilder instanceof FunctionScoreQueryBuilder) {
        verifyQuery(((FunctionScoreQueryBuilder) queryBuilder).query());
    } else if (queryBuilder instanceof BoostingQueryBuilder) {
        verifyQuery(((BoostingQueryBuilder) queryBuilder).negativeQuery());
        verifyQuery(((BoostingQueryBuilder) queryBuilder).positiveQuery());
    } else if (queryBuilder instanceof DisMaxQueryBuilder) {
        DisMaxQueryBuilder disMaxQueryBuilder = (DisMaxQueryBuilder) queryBuilder;
        for (QueryBuilder innerQueryBuilder : disMaxQueryBuilder.innerQueries()) {
            verifyQuery(innerQueryBuilder);
        }
    }
}
Also used : FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) ArrayList(java.util.ArrayList) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder)

Aggregations

ConstantScoreQueryBuilder (org.opensearch.index.query.ConstantScoreQueryBuilder)3 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)2 BoostingQueryBuilder (org.opensearch.index.query.BoostingQueryBuilder)2 DisMaxQueryBuilder (org.opensearch.index.query.DisMaxQueryBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 QueryBuilder (org.opensearch.index.query.QueryBuilder)2 FunctionScoreQueryBuilder (org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder)2 ArrayList (java.util.ArrayList)1 AbstractQueryBuilder.parseInnerQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder)1 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)1 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)1 RangeQueryBuilder (org.opensearch.index.query.RangeQueryBuilder)1 RandomScoreFunctionBuilder (org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder)1 HasChildQueryBuilder (org.opensearch.join.query.HasChildQueryBuilder)1 HasParentQueryBuilder (org.opensearch.join.query.HasParentQueryBuilder)1