Search in sources :

Example 6 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class QueryProfilerIT method testProfileMatchesRegular.

/**
     * This test generates 1-10 random queries and executes a profiled and non-profiled
     * search for each query.  It then does some basic sanity checking of score and hits
     * to make sure the profiling doesn't interfere with the hits being returned
     */
public void testProfileMatchesRegular() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    List<String> stringFields = Arrays.asList("field1");
    List<String> numericFields = Arrays.asList("field2");
    indexRandom(true, docs);
    refresh();
    int iters = between(1, 10);
    for (int i = 0; i < iters; i++) {
        QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
        logger.info("Query: {}", q);
        SearchRequestBuilder vanilla = client().prepareSearch("test").setQuery(q).setProfile(false).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        SearchRequestBuilder profile = client().prepareSearch("test").setQuery(q).setProfile(true).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).execute().actionGet().getResponses();
        SearchResponse vanillaResponse = responses[0].getResponse();
        SearchResponse profileResponse = responses[1].getResponse();
        float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
        float profileMaxScore = profileResponse.getHits().getMaxScore();
        if (Float.isNaN(vanillaMaxScore)) {
            assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore));
        } else {
            assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
        }
        assertThat("Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
        SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
        SearchHit[] profileHits = profileResponse.getHits().getHits();
        for (int j = 0; j < vanillaHits.length; j++) {
            assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

Example 7 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class SimpleSearchIT method assertRescoreWindowFails.

private void assertRescoreWindowFails(int windowSize) {
    SearchRequestBuilder search = client().prepareSearch("idx").addRescorer(new QueryRescorerBuilder(matchAllQuery()).windowSize(windowSize));
    SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> search.get());
    assertThat(e.toString(), containsString("Rescore window [" + windowSize + "] is too large. It must " + "be less than [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.get(Settings.EMPTY)));
    assertThat(e.toString(), containsString("This limit can be set by changing the [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.getKey() + "] index level setting."));
}
Also used : QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException)

Example 8 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class MoreExpressionTests method testSpecialValueVariable.

public void testSpecialValueVariable() throws Exception {
    // i.e. _value for aggregations
    createIndex("test");
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2), client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4), client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8));
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.matchAllQuery()).addAggregation(AggregationBuilders.stats("int_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value * 3", Collections.emptyMap()))).addAggregation(AggregationBuilders.stats("double_agg").field("y").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value - 1.1", Collections.emptyMap()))).addAggregation(// specifically to test a script w/o _value
    AggregationBuilders.stats("const_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "3.0", Collections.emptyMap())));
    SearchResponse rsp = req.get();
    assertEquals(3, rsp.getHits().getTotalHits());
    Stats stats = rsp.getAggregations().get("int_agg");
    assertEquals(39.0, stats.getMax(), 0.0001);
    assertEquals(15.0, stats.getMin(), 0.0001);
    stats = rsp.getAggregations().get("double_agg");
    assertEquals(0.7, stats.getMax(), 0.0001);
    assertEquals(0.1, stats.getMin(), 0.0001);
    stats = rsp.getAggregations().get("const_agg");
    assertThat(stats.getMax(), equalTo(3.0));
    assertThat(stats.getMin(), equalTo(3.0));
    assertThat(stats.getAvg(), equalTo(3.0));
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class MoreExpressionTests method testScore.

public void testScore() throws Exception {
    createIndex("test");
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"), client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye"));
    ScoreFunctionBuilder<?> score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
    // make sure DF is consistent
    req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    SearchResponse rsp = req.get();
    assertSearchResponse(rsp);
    SearchHits hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals("1", hits.getAt(0).getId());
    assertEquals("3", hits.getAt(1).getId());
    assertEquals("2", hits.getAt(2).getId());
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 10 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.

the class MoreExpressionTests method buildRequest.

private SearchRequestBuilder buildRequest(String script, Object... params) {
    ensureGreen("test");
    Map<String, Object> paramsMap = new HashMap<>();
    assert (params.length % 2 == 0);
    for (int i = 0; i < params.length; i += 2) {
        paramsMap.put(params[i].toString(), params[i + 1]);
    }
    SearchRequestBuilder req = client().prepareSearch().setIndices("test");
    req.setQuery(QueryBuilders.matchAllQuery()).addSort(SortBuilders.fieldSort("_uid").order(SortOrder.ASC)).addScriptField("foo", new Script(ScriptType.INLINE, "expression", script, paramsMap));
    return req;
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7