Search in sources :

Example 36 with SearchResponse

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

the class SearchQueryIT method testIdsQueryTestsIdIndexed.

public void testIdsQueryTestsIdIndexed() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("test"));
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), client().prepareIndex("test", "type1", "2").setSource("field1", "value2"), client().prepareIndex("test", "type1", "3").setSource("field1", "value3"));
    SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsQuery("type1").addIds("1", "3"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    // no type
    searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsQuery().addIds("1", "3"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("1", "3")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    // no type
    searchResponse = client().prepareSearch().setQuery(idsQuery().addIds("1", "3")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    searchResponse = client().prepareSearch().setQuery(idsQuery("type1").addIds("7", "10")).get();
    assertHitCount(searchResponse, 0L);
    // repeat..., with terms
    searchResponse = client().prepareSearch().setTypes("type1").setQuery(constantScoreQuery(termsQuery("_id", "1", "3"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 37 with SearchResponse

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

the class SearchQueryIT method testMustNot.

// see #2926
public void testMustNot() throws IOException, ExecutionException, InterruptedException {
    assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS)));
    indexRandom(true, client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar"), client().prepareIndex("test", "test", "2").setSource("description", "foo other anything"), client().prepareIndex("test", "test", "3").setSource("description", "foo other"), client().prepareIndex("test", "test", "4").setSource("description", "foo"));
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
    assertHitCount(searchResponse, 4L);
    searchResponse = client().prepareSearch("test").setQuery(boolQuery().mustNot(matchQuery("description", "anything").type(Type.BOOLEAN))).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
    assertHitCount(searchResponse, 2L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 38 with SearchResponse

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

the class SearchQueryIT method testMultiMatchQueryMinShouldMatch.

public void testMultiMatchQueryMinShouldMatch() {
    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();
    MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2");
    multiMatchQuery.useDisMax(true);
    multiMatchQuery.minimumShouldMatch("70%");
    SearchResponse searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    multiMatchQuery.minimumShouldMatch("30%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasId("1"));
    assertSecondHit(searchResponse, hasId("2"));
    multiMatchQuery.useDisMax(false);
    multiMatchQuery.minimumShouldMatch("70%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    multiMatchQuery.minimumShouldMatch("30%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasId("1"));
    assertSecondHit(searchResponse, hasId("2"));
    multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1");
    multiMatchQuery.minimumShouldMatch("100%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 0L);
    multiMatchQuery.minimumShouldMatch("70%");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    // Min should match > # optional clauses returns no docs.
    multiMatchQuery = multiMatchQuery("value1 value2 value3", "field1", "field2");
    multiMatchQuery.minimumShouldMatch("4");
    searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get();
    assertHitCount(searchResponse, 0L);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 39 with SearchResponse

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

the class SearchQueryIT method testQueryStringAnalyzedWildcard.

public void testQueryStringAnalyzedWildcard() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("value*")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("*ue*")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("*ue_1")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("val*e_1")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("v?l*e?1")).get();
    assertHitCount(searchResponse, 1L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 40 with SearchResponse

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

the class SearchQueryIT method testDateRangeInQueryStringWithTimeZone_7880.

// Issue #7880
public void testDateRangeInQueryStringWithTimeZone_7880() {
    //the mapping needs to be provided upfront otherwise we are not sure how many failures we get back
    //as with dynamic mappings some shards might be lacking behind and parse a different query
    assertAcked(prepareCreate("test").addMapping("type", "past", "type=date"));
    DateTimeZone timeZone = randomDateTimeZone();
    String now = ISODateTimeFormat.dateTime().print(new DateTime(timeZone));
    logger.info(" --> Using time_zone [{}], now is [{}]", timeZone.getID(), now);
    client().prepareIndex("test", "type", "1").setSource("past", now).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("past:[now-1m/m TO now+1m/m]").timeZone(timeZone.getID())).get();
    assertHitCount(searchResponse, 1L);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) 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