Search in sources :

Example 96 with QueryBuilder

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

the class FunctionScoreQueryBuilderTests method testParseSingleFunction.

public void testParseSingleFunction() throws IOException {
    String functionScoreQuery = "{\n" + "    \"function_score\":{\n" + "        \"query\":{\n" + "            \"term\":{\n" + "                \"field1\":\"value1\"\n" + "            }\n" + "        },\n" + "        \"gauss\":  {\n" + "            \"field_name\":  {\n" + "                \"origin\":0.5,\n" + "                \"scale\":0.6\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(1));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(MatchAllQueryBuilder.class));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
        GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
        assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
        assertThat(gaussDecayFunctionBuilder.getWeight(), nullValue());
        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 97 with QueryBuilder

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

the class AliasFilter method reparseFilter.

private QueryBuilder reparseFilter(QueryRewriteContext context) {
    if (reparseAliases) {
        // we are processing a filter received from a 5.0 node - we need to reparse this on the executing node
        final IndexMetaData indexMetaData = context.getIndexSettings().getIndexMetaData();
        /* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch
             * of dependencies we pass in a function that can perform the parsing. */
        CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> {
            try (XContentParser parser = XContentFactory.xContent(bytes).createParser(context.getXContentRegistry(), bytes)) {
                return context.newParseContext(parser).parseInnerQueryBuilder();
            }
        };
        return ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases);
    }
    return filter;
}
Also used : XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) IOException(java.io.IOException) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) CheckedFunction(org.elasticsearch.common.CheckedFunction) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Writeable(org.elasticsearch.common.io.stream.Writeable) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 98 with QueryBuilder

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

the class InnerHitsIT method testMatchesQueriesParentChildInnerHits.

public void testMatchesQueriesParentChildInnerHits() throws Exception {
    assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("index", "parent", "1").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index", "child", "1").setParent("1").setSource("field", "value1"));
    requests.add(client().prepareIndex("index", "child", "2").setParent("1").setSource("field", "value2"));
    requests.add(client().prepareIndex("index", "parent", "2").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index", "child", "3").setParent("2").setSource("field", "value1"));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("index").setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(response, 2);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
    assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
    QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None).innerHit(new InnerHitBuilder(), false);
    response = client().prepareSearch("index").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name2"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 99 with QueryBuilder

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

the class InnerHitsIT method testMatchesQueriesNestedInnerHits.

public void testMatchesQueriesNestedInnerHits() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", builder));
    ensureGreen();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = randomIntBetween(2, 35);
    requests.add(client().prepareIndex("test", "type1", "0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
    for (int i = 2; i < numDocs; i++) {
        requests.add(client().prepareIndex("test", "type1", String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    }
    indexRandom(true, requests);
    waitForRelocation(ClusterHealthStatus.GREEN);
    QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
    query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)), false);
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertAllSuccessful(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo((long) numDocs));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
    for (int i = 2; i < numDocs; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 100 with QueryBuilder

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

the class MatchedQueriesIT method testMatchedWithWrapperQuery.

public void testMatchedWithWrapperQuery() throws Exception {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get();
    refresh();
    QueryBuilder[] queries = new QueryBuilder[] { wrapperQuery(matchQuery("content", "amet").queryName("abc").buildAsBytes().utf8ToString()), constantScoreQuery(wrapperQuery(termQuery("content", "amet").queryName("abc").buildAsBytes().utf8ToString())) };
    for (QueryBuilder query : queries) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(query).get();
        assertHitCount(searchResponse, 1L);
        assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("abc"));
    }
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)158 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)51 SearchResponse (org.elasticsearch.action.search.SearchResponse)31 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)25 Map (java.util.Map)21 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)19 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)15 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)14 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)14 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)14 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)14 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)14 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)12 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)11 XContentParser (org.elasticsearch.common.xcontent.XContentParser)11 BytesRef (org.apache.lucene.util.BytesRef)10 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)10