Search in sources :

Example 36 with SearchResponse

use of 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 37 with SearchResponse

use of 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)

Example 38 with SearchResponse

use of 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 39 with SearchResponse

use of 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 40 with SearchResponse

use of 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)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1457 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)145 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)114 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)61 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50