Search in sources :

Example 31 with SearchResponse

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

the class SearchScrollWithFailingNodesIT method testScanScrollWithShardExceptions.

public void testScanScrollWithShardExceptions() throws Exception {
    internalCluster().startNode();
    internalCluster().startNode();
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), 1)));
    List<IndexRequestBuilder> writes = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        writes.add(client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject().field("field", i).endObject()));
    }
    indexRandom(false, writes);
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
    assertAllSuccessful(searchResponse);
    long numHits = 0;
    do {
        numHits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
        assertAllSuccessful(searchResponse);
    } while (searchResponse.getHits().getHits().length > 0);
    assertThat(numHits, equalTo(100L));
    clearScroll("_all");
    internalCluster().stopRandomNonMasterNode();
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
    assertThat(searchResponse.getSuccessfulShards(), lessThan(searchResponse.getTotalShards()));
    numHits = 0;
    int numberOfSuccessfulShards = searchResponse.getSuccessfulShards();
    do {
        numHits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
        assertThat(searchResponse.getSuccessfulShards(), equalTo(numberOfSuccessfulShards));
    } while (searchResponse.getHits().getHits().length > 0);
    assertThat(numHits, greaterThan(0L));
    clearScroll(searchResponse.getScrollId());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 32 with SearchResponse

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

the class SearchQueryIT method testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses.

public void testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses() throws IOException {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
    client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get();
    refresh();
    BoolQueryBuilder boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(1)).minimumShouldMatch(2);
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);
    boolQuery = boolQuery().should(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3)).minimumShouldMatch(1);
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    boolQuery = boolQuery().must(termQuery("field1", "value1")).must(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 33 with SearchResponse

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

the class SearchQueryIT method testSpecialRangeSyntaxInQueryString.

public void testSpecialRangeSyntaxInQueryString() {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get();
    client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>19")).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("2"));
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>20")).get();
    assertHitCount(searchResponse, 0L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>=20")).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("2"));
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>11")).get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:<20")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:<=20")).get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("+num:>11 +num:<20")).get();
    assertHitCount(searchResponse, 1L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 34 with SearchResponse

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

the class SearchQueryIT method testMultiMatchQueryZeroTermsQuery.

public void testMultiMatchQueryZeroTermsQuery() {
    assertAcked(prepareCreate("test").addMapping("type1", "field1", "type=text,analyzer=classic", "field2", "type=text,analyzer=classic"));
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").get();
    refresh();
    BoolQueryBuilder boolQuery = boolQuery().must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE)).must(// Fields are ORed together
    multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.NONE));
    SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 0L);
    boolQuery = boolQuery().must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL)).must(multiMatchQuery("value4", "field1", "field2").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 1L);
    boolQuery = boolQuery().must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQuery.ZeroTermsQuery.ALL));
    searchResponse = client().prepareSearch().setQuery(boolQuery).get();
    assertHitCount(searchResponse, 2L);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 35 with SearchResponse

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

the class SearchQueryIT method testBasicQueryById.

public void testBasicQueryById() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
    client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2").addIds("1", "2")).get();
    assertHitCount(searchResponse, 2L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1")).get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1", "2")).get();
    assertHitCount(searchResponse, 2L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("1", "2")).get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    searchResponse = client().prepareSearch().setQuery(idsQuery(Strings.EMPTY_ARRAY).addIds("1")).get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2", "type3").addIds("1", "2", "3", "4")).get();
    assertHitCount(searchResponse, 2L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1693 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)272 SearchHit (org.elasticsearch.search.SearchHit)225 Script (org.elasticsearch.script.Script)223 ArrayList (java.util.ArrayList)212 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)134 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)94 SearchHits (org.elasticsearch.search.SearchHits)89 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)83 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)76 Test (org.junit.Test)73 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)72 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 Map (java.util.Map)67