Search in sources :

Example 11 with SearchHit

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

the class SearchScrollIT method testSimpleScrollQueryThenFetchSmallSizeUnevenDistribution.

public void testSimpleScrollQueryThenFetchSmallSizeUnevenDistribution() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 3)).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    for (int i = 0; i < 100; i++) {
        String routing = "0";
        if (i > 90) {
            routing = "1";
        } else if (i > 60) {
            routing = "2";
        }
        client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", i).setRouting(routing).execute().actionGet();
    }
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchAllQuery()).setSize(3).setScroll(TimeValue.timeValueMinutes(2)).addSort("field", SortOrder.ASC).execute().actionGet();
    try {
        long counter = 0;
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(3));
        for (SearchHit hit : searchResponse.getHits()) {
            assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
        }
        for (int i = 0; i < 32; i++) {
            searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
            assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
            assertThat(searchResponse.getHits().getHits().length, equalTo(3));
            for (SearchHit hit : searchResponse.getHits()) {
                assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
            }
        }
        // and now, the last one is one
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(1));
        for (SearchHit hit : searchResponse.getHits()) {
            assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
        }
        // a the last is zero
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
        assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
        assertThat(searchResponse.getHits().getHits().length, equalTo(0));
        for (SearchHit hit : searchResponse.getHits()) {
            assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
        }
    } finally {
        clearScroll(searchResponse.getScrollId());
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 12 with SearchHit

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

the class FieldSortIT method testIssue2986.

public void testIssue2986() {
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type", "field1", "type=keyword").get());
    client().prepareIndex("test", "post", "1").setSource("{\"field1\":\"value1\"}", XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "post", "2").setSource("{\"field1\":\"value2\"}", XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "post", "3").setSource("{\"field1\":\"value3\"}", XContentType.JSON).execute().actionGet();
    refresh();
    SearchResponse result = client().prepareSearch("test").setQuery(matchAllQuery()).setTrackScores(true).addSort("field1", SortOrder.ASC).execute().actionGet();
    for (SearchHit hit : result.getHits()) {
        assertFalse(Float.isNaN(hit.getScore()));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 13 with SearchHit

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

the class SimpleSortIT method testSimpleSorts.

public void testSimpleSorts() throws Exception {
    Random random = random();
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("str_value").field("type", "keyword").endObject().startObject("boolean_value").field("type", "boolean").endObject().startObject("byte_value").field("type", "byte").endObject().startObject("short_value").field("type", "short").endObject().startObject("integer_value").field("type", "integer").endObject().startObject("long_value").field("type", "long").endObject().startObject("float_value").field("type", "float").endObject().startObject("double_value").field("type", "double").endObject().endObject().endObject().endObject()));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        builders.add(client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("str_value", new String(new char[] { (char) (97 + i), (char) (97 + i) })).field("boolean_value", true).field("byte_value", i).field("short_value", i).field("integer_value", i).field("long_value", i).field("float_value", 0.1 * i).field("double_value", 0.1 * i).endObject()));
    }
    Collections.shuffle(builders, random);
    for (IndexRequestBuilder builder : builders) {
        builder.execute().actionGet();
        if (random.nextBoolean()) {
            if (random.nextInt(5) != 0) {
                refresh();
            } else {
                client().admin().indices().prepareFlush().get();
            }
        }
    }
    refresh();
    // STRING script
    int size = 1 + random.nextInt(10);
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['str_value'].value", Collections.emptyMap());
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort(new ScriptSortBuilder(script, ScriptSortType.STRING)).get();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getId(), equalTo(Integer.toString(i)));
        String expected = new String(new char[] { (char) (97 + i), (char) (97 + i) });
        assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).get();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        SearchHit searchHit = searchResponse.getHits().getAt(i);
        assertThat(searchHit.getId(), equalTo(Integer.toString(9 - i)));
        String expected = new String(new char[] { (char) (97 + (9 - i)), (char) (97 + (9 - i)) });
        assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    assertNoFailures(searchResponse);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) Random(java.util.Random) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 14 with SearchHit

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

the class IndexWithShadowReplicasIT method testShadowReplicasUsingFieldData.

public void testShadowReplicasUsingFieldData() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);
    internalCluster().startNodes(3, nodeSettings);
    String IDX = "test";
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 2).put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).build();
    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=keyword").get();
    ensureGreen(IDX);
    client().prepareIndex(IDX, "doc", "1").setSource("foo", "foo").get();
    client().prepareIndex(IDX, "doc", "2").setSource("foo", "bar").get();
    client().prepareIndex(IDX, "doc", "3").setSource("foo", "baz").get();
    client().prepareIndex(IDX, "doc", "4").setSource("foo", "eggplant").get();
    flushAndRefresh(IDX);
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).addDocValueField("foo").addSort("foo", SortOrder.ASC).get();
    assertHitCount(resp, 4);
    assertOrderedSearchHits(resp, "2", "3", "4", "1");
    SearchHit[] hits = resp.getHits().getHits();
    assertThat(hits[0].field("foo").getValue().toString(), equalTo("bar"));
    assertThat(hits[1].field("foo").getValue().toString(), equalTo("baz"));
    assertThat(hits[2].field("foo").getValue().toString(), equalTo("eggplant"));
    assertThat(hits[3].field("foo").getValue().toString(), equalTo("foo"));
}
Also used : Path(java.nio.file.Path) SearchHit(org.elasticsearch.search.SearchHit) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 15 with SearchHit

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

the class TokenCountFieldMapperIntegrationIT method assertSearchReturns.

private void assertSearchReturns(SearchResponse result, String... ids) {
    assertThat(result.getHits().getTotalHits(), equalTo((long) ids.length));
    assertThat(result.getHits().getHits().length, equalTo(ids.length));
    List<String> foundIds = new ArrayList<>();
    for (SearchHit hit : result.getHits()) {
        foundIds.add(hit.getId());
    }
    assertThat(foundIds, containsInAnyOrder(ids));
    for (SearchHit hit : result.getHits()) {
        String id = hit.getId();
        if (id.equals("single")) {
            assertSearchHit(hit, 4);
        } else if (id.equals("bulk1")) {
            assertSearchHit(hit, 3);
        } else if (id.equals("bulk2")) {
            assertSearchHit(hit, 5);
        } else if (id.equals("multi")) {
            assertSearchHit(hit, 2, 7);
        } else if (id.equals("multibulk1")) {
            assertSearchHit(hit, 1, 8);
        } else if (id.equals("multibulk2")) {
            assertSearchHit(hit, 6, 10);
        } else {
            throw new ElasticsearchException("Unexpected response!");
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException)

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