Search in sources :

Example 16 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class FunctionScoreQueryBuilderTests method testParseFunctionsArray.

public void testParseFunctionsArray() throws IOException {
    String functionScoreQuery = "{\n" + "    \"function_score\":{\n" + "        \"query\":{\n" + "            \"term\":{\n" + "                \"field1\":\"value1\"\n" + "            }\n" + "        },\n" + "        \"functions\":  [\n" + "            {\n" + "                \"random_score\":  {\n" + "                    \"seed\":123456\n" + "                },\n" + "                \"weight\": 3,\n" + "                \"filter\": {\n" + "                    \"term\":{\n" + "                        \"field2\":\"value2\"\n" + "                    }\n" + "                }\n" + "            },\n" + "            {\n" + "                \"filter\": {\n" + "                    \"term\":{\n" + "                        \"field3\":\"value3\"\n" + "                    }\n" + "                },\n" + "                \"weight\": 9\n" + "            },\n" + "            {\n" + "                \"gauss\":  {\n" + "                    \"field_name\":  {\n" + "                        \"origin\":0.5,\n" + "                        \"scale\":0.6\n" + "                    }\n" + "                }\n" + "            }\n" + "        ],\n" + "        \"boost\" : 3,\n" + "        \"score_mode\" : \"avg\",\n" + "        \"boost_mode\" : \"replace\",\n" + "        \"max_boost\" : 10\n" + "    }\n" + "}";
    QueryBuilder queryBuilder = parseQuery(functionScoreQuery);
    /*
         * given that we copy part of the decay functions as bytes, we test that fromXContent and toXContent both work no matter what the
         * initial format was
         */
    for (int i = 0; i <= XContentType.values().length; i++) {
        assertThat(queryBuilder, instanceOf(FunctionScoreQueryBuilder.class));
        FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) queryBuilder;
        assertThat(functionScoreQueryBuilder.query(), instanceOf(TermQueryBuilder.class));
        TermQueryBuilder termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.query();
        assertThat(termQueryBuilder.fieldName(), equalTo("field1"));
        assertThat(termQueryBuilder.value(), equalTo("value1"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders().length, equalTo(3));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(TermQueryBuilder.class));
        termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter();
        assertThat(termQueryBuilder.fieldName(), equalTo("field2"));
        assertThat(termQueryBuilder.value(), equalTo("value2"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter(), instanceOf(TermQueryBuilder.class));
        termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter();
        assertThat(termQueryBuilder.fieldName(), equalTo("field3"));
        assertThat(termQueryBuilder.value(), equalTo("value3"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getFilter(), instanceOf(MatchAllQueryBuilder.class));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(RandomScoreFunctionBuilder.class));
        RandomScoreFunctionBuilder randomScoreFunctionBuilder = (RandomScoreFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
        assertThat(randomScoreFunctionBuilder.getSeed(), equalTo(123456));
        assertThat(randomScoreFunctionBuilder.getWeight(), equalTo(3f));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction(), instanceOf(WeightBuilder.class));
        WeightBuilder weightBuilder = (WeightBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction();
        assertThat(weightBuilder.getWeight(), equalTo(9f));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
        GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction();
        assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
        assertThat(functionScoreQueryBuilder.boost(), equalTo(3f));
        assertThat(functionScoreQueryBuilder.scoreMode(), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG));
        assertThat(functionScoreQueryBuilder.boostMode(), equalTo(CombineFunction.REPLACE));
        assertThat(functionScoreQueryBuilder.maxBoost(), equalTo(10f));
        if (i < XContentType.values().length) {
            BytesReference bytes = ((AbstractQueryBuilder) queryBuilder).buildAsBytes(XContentType.values()[i]);
            try (XContentParser parser = createParser(XContentType.values()[i].xContent(), bytes)) {
                queryBuilder = parseQuery(parser);
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) 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) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentParser(org.elasticsearch.common.xcontent.XContentParser) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 17 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder 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 18 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class FunctionScoreQueryBuilderTests method testWeight1fStillProducesWeightFunction.

public void testWeight1fStillProducesWeightFunction() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String queryString = jsonBuilder().startObject().startObject("function_score").startArray("functions").startObject().startObject("field_value_factor").field("field", INT_FIELD_NAME).endObject().field("weight", 1.0).endObject().endArray().endObject().endObject().string();
    QueryBuilder query = parseQuery(queryString);
    assertThat(query, instanceOf(FunctionScoreQueryBuilder.class));
    FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) query;
    assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(FieldValueFactorFunctionBuilder.class));
    FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = (FieldValueFactorFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
    assertThat(fieldValueFactorFunctionBuilder.fieldName(), equalTo(INT_FIELD_NAME));
    assertThat(fieldValueFactorFunctionBuilder.factor(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_FACTOR));
    assertThat(fieldValueFactorFunctionBuilder.modifier(), equalTo(FieldValueFactorFunctionBuilder.DEFAULT_MODIFIER));
    assertThat(fieldValueFactorFunctionBuilder.getWeight(), equalTo(1f));
    assertThat(fieldValueFactorFunctionBuilder.missing(), nullValue());
    Query luceneQuery = query.toQuery(createShardContext());
    assertThat(luceneQuery, instanceOf(FunctionScoreQuery.class));
    FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) luceneQuery;
    assertThat(functionScoreQuery.getFunction(), instanceOf(WeightFactorFunction.class));
    WeightFactorFunction weightFactorFunction = (WeightFactorFunction) functionScoreQuery.getFunction();
    assertThat(weightFactorFunction.getWeight(), equalTo(1.0f));
    assertThat(weightFactorFunction.getScoreFunction(), instanceOf(FieldValueFactorFunction.class));
}
Also used : WeightFactorFunction(org.elasticsearch.common.lucene.search.function.WeightFactorFunction) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) QueryBuilders.functionScoreQuery(org.elasticsearch.index.query.QueryBuilders.functionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) TermQuery(org.apache.lucene.search.TermQuery) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FieldValueFactorFunction(org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction) Matchers.containsString(org.hamcrest.Matchers.containsString) 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)

Example 19 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class AbstractQueryTestCase method rewriteQuery.

private QueryBuilder rewriteQuery(QB queryBuilder, QueryRewriteContext rewriteContext) throws IOException {
    QueryBuilder rewritten = QueryBuilder.rewriteQuery(queryBuilder, rewriteContext);
    // extra safety to fail fast - serialize the rewritten version to ensure it's serializable.
    assertSerialization(rewritten);
    return rewritten;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder)

Example 20 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class AbstractQueryTestCase method parseQuery.

protected static QueryBuilder parseQuery(XContentParser parser) throws IOException {
    QueryParseContext context = createParseContext(parser);
    QueryBuilder parseInnerQueryBuilder = context.parseInnerQueryBuilder();
    assertNull(parser.nextToken());
    return parseInnerQueryBuilder;
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)371 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)58 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)29 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 IOException (java.io.IOException)23 SearchHit (org.elasticsearch.search.SearchHit)22 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)19 List (java.util.List)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15