Search in sources :

Example 31 with SearchHit

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

the class TransportTwoNodesSearchIT method testQueryThenFetchWithFrom.

public void testQueryThenFetchWithFrom() throws Exception {
    Set<String> fullExpectedIds = prepareData();
    SearchSourceBuilder source = searchSource().query(matchAllQuery()).explain(true);
    Set<String> collectedIds = new TreeSet<>();
    SearchResponse searchResponse = client().search(searchRequest("test").source(source.from(0).size(60)).searchType(QUERY_THEN_FETCH)).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(60));
    for (int i = 0; i < 60; i++) {
        SearchHit hit = searchResponse.getHits().getHits()[i];
        collectedIds.add(hit.getId());
    }
    searchResponse = client().search(searchRequest("test").source(source.from(60).size(60)).searchType(QUERY_THEN_FETCH)).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(40));
    for (int i = 0; i < 40; i++) {
        SearchHit hit = searchResponse.getHits().getHits()[i];
        collectedIds.add(hit.getId());
    }
    assertThat(collectedIds, equalTo(fullExpectedIds));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) TreeSet(java.util.TreeSet) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 32 with SearchHit

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

the class TransportTwoNodesSearchIT method testQueryThenFetch.

public void testQueryThenFetch() throws Exception {
    prepareData();
    int total = 0;
    SearchResponse searchResponse = client().prepareSearch("test").setSearchType(QUERY_THEN_FETCH).setQuery(termQuery("multi", "test")).setSize(60).setExplain(true).addSort("nid", SortOrder.DESC).setScroll(TimeValue.timeValueSeconds(30)).get();
    while (true) {
        assertNoFailures(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        SearchHit[] hits = searchResponse.getHits().getHits();
        if (hits.length == 0) {
            // finished
            break;
        }
        for (int i = 0; i < hits.length; ++i) {
            SearchHit hit = hits[i];
            assertThat(hit.getExplanation(), notNullValue());
            assertThat("id[" + hit.getId() + "]", hit.getId(), equalTo(Integer.toString(100 - total - i - 1)));
        }
        total += hits.length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueSeconds(30)).get();
    }
    clearScroll(searchResponse.getScrollId());
    assertEquals(100, total);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 33 with SearchHit

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

the class ExplainableScriptIT method testNativeExplainScript.

public void testNativeExplainScript() throws InterruptedException, IOException, ExecutionException {
    List<IndexRequestBuilder> indexRequests = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        indexRequests.add(client().prepareIndex("test", "type").setId(Integer.toString(i)).setSource(jsonBuilder().startObject().field("number_field", i).field("text", "text").endObject()));
    }
    indexRandom(true, true, indexRequests);
    client().admin().indices().prepareRefresh().execute().actionGet();
    ensureYellow();
    SearchResponse response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().explain(true).query(functionScoreQuery(termQuery("text", "text"), scriptFunction(new Script(ScriptType.INLINE, "native", "native_explainable_script", Collections.emptyMap()))).boostMode(CombineFunction.REPLACE)))).actionGet();
    ElasticsearchAssertions.assertNoFailures(response);
    SearchHits hits = response.getHits();
    assertThat(hits.getTotalHits(), equalTo(20L));
    int idCounter = 19;
    for (SearchHit hit : hits.getHits()) {
        assertThat(hit.getId(), equalTo(Integer.toString(idCounter)));
        assertThat(hit.getExplanation().toString(), containsString(Double.toString(idCounter) + " = This script returned " + Double.toString(idCounter)));
        assertThat(hit.getExplanation().toString(), containsString("freq=1.0 = termFreq=1.0"));
        assertThat(hit.getExplanation().getDetails().length, equalTo(2));
        idCounter--;
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript) Script(org.elasticsearch.script.Script) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 34 with SearchHit

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

the class SearchFieldsIT method testStoredFieldsWithoutSource.

public void testStoredFieldsWithoutSource() throws Exception {
    createIndex("test");
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", false).endObject().startObject("properties").startObject("byte_field").field("type", "byte").field("store", true).endObject().startObject("short_field").field("type", "short").field("store", true).endObject().startObject("integer_field").field("type", "integer").field("store", true).endObject().startObject("long_field").field("type", "long").field("store", true).endObject().startObject("float_field").field("type", "float").field("store", true).endObject().startObject("double_field").field("type", "double").field("store", true).endObject().startObject("date_field").field("type", "date").field("store", true).endObject().startObject("boolean_field").field("type", "boolean").field("store", true).endObject().startObject("binary_field").field("type", "binary").field("store", true).endObject().endObject().endObject().endObject().string();
    client().admin().indices().preparePutMapping().setType("type1").setSource(mapping, XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("byte_field", (byte) 1).field("short_field", (short) 2).field("integer_field", 3).field("long_field", 4L).field("float_field", 5.0f).field("double_field", 6.0d).field("date_field", Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC))).field("boolean_field", true).field("binary_field", Base64.getEncoder().encodeToString("testing text".getBytes("UTF-8"))).endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addStoredField("byte_field").addStoredField("short_field").addStoredField("integer_field").addStoredField("long_field").addStoredField("float_field").addStoredField("double_field").addStoredField("date_field").addStoredField("boolean_field").addStoredField("binary_field").execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    Set<String> fields = new HashSet<>(searchResponse.getHits().getAt(0).getFields().keySet());
    assertThat(fields, equalTo(newHashSet("byte_field", "short_field", "integer_field", "long_field", "float_field", "double_field", "date_field", "boolean_field", "binary_field")));
    SearchHit searchHit = searchResponse.getHits().getAt(0);
    assertThat(searchHit.getFields().get("byte_field").getValue().toString(), equalTo("1"));
    assertThat(searchHit.getFields().get("short_field").getValue().toString(), equalTo("2"));
    assertThat(searchHit.getFields().get("integer_field").getValue(), equalTo((Object) 3));
    assertThat(searchHit.getFields().get("long_field").getValue(), equalTo((Object) 4L));
    assertThat(searchHit.getFields().get("float_field").getValue(), equalTo((Object) 5.0f));
    assertThat(searchHit.getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
    String dateTime = Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC));
    assertThat(searchHit.getFields().get("date_field").getValue(), equalTo((Object) dateTime));
    assertThat(searchHit.getFields().get("boolean_field").getValue(), equalTo((Object) Boolean.TRUE));
    assertThat(searchHit.getFields().get("binary_field").getValue(), equalTo(new BytesArray("testing text".getBytes("UTF8"))));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Sets.newHashSet(org.elasticsearch.common.util.set.Sets.newHashSet) HashSet(java.util.HashSet)

Example 35 with SearchHit

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

the class SearchFieldsIT method testScriptFields.

public void testScriptFields() throws Exception {
    assertAcked(prepareCreate("index").addMapping("type", "s", "type=keyword", "l", "type=long", "d", "type=double", "ms", "type=keyword", "ml", "type=long", "md", "type=double").get());
    final int numDocs = randomIntBetween(3, 8);
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    for (int i = 0; i < numDocs; ++i) {
        reqs.add(client().prepareIndex("index", "type", Integer.toString(i)).setSource("s", Integer.toString(i), "ms", new String[] { Integer.toString(i), Integer.toString(i + 1) }, "l", i, "ml", new long[] { i, i + 1 }, "d", i, "md", new double[] { i, i + 1 }));
    }
    indexRandom(true, reqs);
    ensureSearchable();
    SearchRequestBuilder req = client().prepareSearch("index");
    for (String field : Arrays.asList("s", "ms", "l", "ml", "d", "md")) {
        req.addScriptField(field, new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + field + "'].values", Collections.emptyMap()));
    }
    SearchResponse resp = req.get();
    assertSearchResponse(resp);
    for (SearchHit hit : resp.getHits().getHits()) {
        final int id = Integer.parseInt(hit.getId());
        Map<String, SearchHitField> fields = hit.getFields();
        assertThat(fields.get("s").getValues(), equalTo(Collections.<Object>singletonList(Integer.toString(id))));
        assertThat(fields.get("l").getValues(), equalTo(Collections.<Object>singletonList((long) id)));
        assertThat(fields.get("d").getValues(), equalTo(Collections.<Object>singletonList((double) id)));
        assertThat(fields.get("ms").getValues(), equalTo(Arrays.<Object>asList(Integer.toString(id), Integer.toString(id + 1))));
        assertThat(fields.get("ml").getValues(), equalTo(Arrays.<Object>asList((long) id, id + 1L)));
        assertThat(fields.get("md").getValues(), equalTo(Arrays.<Object>asList((double) id, id + 1d)));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHitField(org.elasticsearch.search.SearchHitField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)318 SearchResponse (org.elasticsearch.action.search.SearchResponse)225 SearchHits (org.elasticsearch.search.SearchHits)87 ArrayList (java.util.ArrayList)83 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)64 HashMap (java.util.HashMap)37 Map (java.util.Map)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)37 IOException (java.io.IOException)35 Test (org.junit.Test)32 TimeValue (org.elasticsearch.common.unit.TimeValue)29 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)22 List (java.util.List)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)17 SearchHitField (org.elasticsearch.search.SearchHitField)16 HashSet (java.util.HashSet)15 Text (org.elasticsearch.common.text.Text)14