Search in sources :

Example 96 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse 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 97 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MoreExpressionTests method testFunction.

public void testFunction() throws Exception {
    createIndex("test");
    ensureGreen("test");
    client().prepareIndex("test", "doc", "1").setSource("foo", 4).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse rsp = buildRequest("doc['foo'] + abs(1)").get();
    assertSearchResponse(rsp);
    assertEquals(1, rsp.getHits().getTotalHits());
    assertEquals(5.0, rsp.getHits().getAt(0).field("foo").getValue(), 0.0D);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 98 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MoreExpressionTests method testDateObjectMethods.

public void testDateObjectMethods() throws Exception {
    ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "date0", "type=date", "date1", "type=date"));
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), client().prepareIndex("test", "doc", "2").setSource("date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z"));
    SearchResponse rsp = buildRequest("doc['date0'].date.secondOfMinute - doc['date0'].date.minuteOfHour").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    SearchHits hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(-11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date0'].date.getHourOfDay() + doc['date1'].date.dayOfMonth").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(24.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].date.monthOfYear + 1").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(10.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].date.year").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(1985.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(1983.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 99 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class MoreExpressionTests method testGeo.

public void testGeo() throws Exception {
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "test").startObject("location").field("lat", 61.5240).field("lon", 105.3188).endObject().endObject()).execute().actionGet();
    refresh();
    // access .lat
    SearchResponse rsp = buildRequest("doc['location'].lat").get();
    assertSearchResponse(rsp);
    assertEquals(1, rsp.getHits().getTotalHits());
    assertEquals(61.5240, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    // access .lon
    rsp = buildRequest("doc['location'].lon").get();
    assertSearchResponse(rsp);
    assertEquals(1, rsp.getHits().getTotalHits());
    assertEquals(105.3188, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    // access .empty
    rsp = buildRequest("doc['location'].empty ? 1 : 0").get();
    assertSearchResponse(rsp);
    assertEquals(1, rsp.getHits().getTotalHits());
    assertEquals(0, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
    // call haversin
    rsp = buildRequest("haversin(38.9072, 77.0369, doc['location'].lat, doc['location'].lon)").get();
    assertSearchResponse(rsp);
    assertEquals(1, rsp.getHits().getTotalHits());
    assertEquals(3170D, rsp.getHits().getAt(0).field("foo").getValue(), 50D);
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 100 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class PercolatorQuerySearchIT method testPercolateScriptQuery.

public void testPercolateScriptQuery() throws IOException {
    client().admin().indices().prepareCreate("index").addMapping("type", "query", "type=percolator").get();
    client().prepareIndex("index", "type", "1").setSource(jsonBuilder().startObject().field("query", QueryBuilders.scriptQuery(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "1==1", Collections.emptyMap()))).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).execute().actionGet();
    SearchResponse response = client().prepareSearch("index").setQuery(new PercolateQueryBuilder("query", "type", jsonBuilder().startObject().field("field1", "b").endObject().bytes(), XContentType.JSON)).get();
    assertHitCount(response, 1);
    assertSearchHits(response, "1");
}
Also used : Script(org.elasticsearch.script.Script) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1722 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)275 SearchHit (org.elasticsearch.search.SearchHit)227 Script (org.elasticsearch.script.Script)225 ArrayList (java.util.ArrayList)217 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 Matchers.containsString (org.hamcrest.Matchers.containsString)156 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)143 HashMap (java.util.HashMap)139 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)97 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)95 SearchHits (org.elasticsearch.search.SearchHits)92 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)84 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)82 Test (org.junit.Test)75 Map (java.util.Map)72 SearchRequest (org.elasticsearch.action.search.SearchRequest)70