Search in sources :

Example 1 with WrapperQueryBuilder

use of org.elasticsearch.index.query.WrapperQueryBuilder in project elasticsearch by elastic.

the class FunctionScoreQueryBuilderTests method testRewriteWithFunction.

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

Example 2 with WrapperQueryBuilder

use of org.elasticsearch.index.query.WrapperQueryBuilder in project elasticsearch by elastic.

the class FiltersAggsRewriteIT method testWrapperQueryIsRewritten.

public void testWrapperQueryIsRewritten() throws IOException {
    createIndex("test", Settings.EMPTY, "test", "title", "type=text");
    client().prepareIndex("test", "test", "1").setSource("title", "foo bar baz").get();
    client().prepareIndex("test", "test", "2").setSource("title", "foo foo foo").get();
    client().prepareIndex("test", "test", "3").setSource("title", "bar baz bax").get();
    client().admin().indices().prepareRefresh("test").get();
    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 = builder.bytes();
    }
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("titles", new FiltersAggregator.KeyedFilter("titleterms", new WrapperQueryBuilder(bytesReference)));
    SearchResponse searchResponse = client().prepareSearch("test").setSize(0).addAggregation(builder).get();
    assertEquals(3, searchResponse.getHits().getTotalHits());
    InternalFilters filters = searchResponse.getAggregations().get("titles");
    assertEquals(1, filters.getBuckets().size());
    assertEquals(2, filters.getBuckets().get(0).getDocCount());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) XContentType(org.elasticsearch.common.xcontent.XContentType) FiltersAggregator(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with WrapperQueryBuilder

use of org.elasticsearch.index.query.WrapperQueryBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testPassQueryOrFilterAsJSONString.

public void testPassQueryOrFilterAsJSONString() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "type1", "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.elasticsearch.index.query.BoolQueryBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) QueryBuilders.spanMultiTermQueryBuilder(org.elasticsearch.index.query.QueryBuilders.spanMultiTermQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder)

Example 4 with WrapperQueryBuilder

use of org.elasticsearch.index.query.WrapperQueryBuilder in project elasticsearch by elastic.

the class FunctionScoreQueryBuilderTests method testRewrite.

public void testRewrite() throws IOException {
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString())).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM).setMinScore(1).maxBoost(100);
    FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
    assertNotSame(functionScoreQueryBuilder, rewrite);
    assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
    assertEquals(rewrite.boostMode(), CombineFunction.REPLACE);
    assertEquals(rewrite.scoreMode(), FiltersFunctionScoreQuery.ScoreMode.SUM);
    assertEquals(rewrite.getMinScore(), 1f, 0.0001);
    assertEquals(rewrite.maxBoost(), 100f, 0.0001);
}
Also used : WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder)

Aggregations

WrapperQueryBuilder (org.elasticsearch.index.query.WrapperQueryBuilder)4 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryBuilders.spanMultiTermQueryBuilder (org.elasticsearch.index.query.QueryBuilders.spanMultiTermQueryBuilder)1 RandomQueryBuilder (org.elasticsearch.index.query.RandomQueryBuilder)1 FilterFunctionBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)1 FiltersAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)1 FiltersAggregator (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator)1 InternalFilters (org.elasticsearch.search.aggregations.bucket.filters.InternalFilters)1