Search in sources :

Example 56 with SearchResponse

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

the class SimpleQueryStringIT method testKeywordWithWhitespace.

public void testKeywordWithWhitespace() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f2", "Foo Bar"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar"));
    reqs.add(client().prepareIndex("test", "doc", "3").setSource("f1", "foo bar"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo")).get();
    assertHits(resp.getHits(), "3");
    assertHitCount(resp, 1L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar")).get();
    assertHits(resp.getHits(), "2", "3");
    assertHitCount(resp, 2L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 57 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringMinimumShouldMatch.

public void testSimpleQueryStringMinimumShouldMatch() throws Exception {
    createIndex("test");
    ensureGreen("test");
    indexRandom(true, false, client().prepareIndex("test", "type1", "1").setSource("body", "foo"), client().prepareIndex("test", "type1", "2").setSource("body", "bar"), client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), client().prepareIndex("test", "type1", "4").setSource("body", "foo baz bar"));
    logger.info("--> query 1");
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").minimumShouldMatch("2")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "4");
    logger.info("--> query 2");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").field("body").field("body2").minimumShouldMatch("2")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "4");
    // test case from #13884
    logger.info("--> query 3");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("body").field("body2").field("body3").minimumShouldMatch("-50%")).get();
    assertHitCount(searchResponse, 3L);
    assertSearchHits(searchResponse, "1", "3", "4");
    logger.info("--> query 4");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar baz").field("body").field("body2").minimumShouldMatch("70%")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "4");
    indexRandom(true, false, client().prepareIndex("test", "type1", "5").setSource("body2", "foo", "other", "foo"), client().prepareIndex("test", "type1", "6").setSource("body2", "bar", "other", "foo"), client().prepareIndex("test", "type1", "7").setSource("body2", "foo bar", "other", "foo"), client().prepareIndex("test", "type1", "8").setSource("body2", "foo baz bar", "other", "foo"));
    logger.info("--> query 5");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").field("body").field("body2").minimumShouldMatch("2")).get();
    assertHitCount(searchResponse, 4L);
    assertSearchHits(searchResponse, "3", "4", "7", "8");
    logger.info("--> query 6");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").minimumShouldMatch("2")).get();
    assertHitCount(searchResponse, 5L);
    assertSearchHits(searchResponse, "3", "4", "6", "7", "8");
    logger.info("--> query 7");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar baz").field("body2").field("other").minimumShouldMatch("70%")).get();
    assertHitCount(searchResponse, 3L);
    assertSearchHits(searchResponse, "6", "7", "8");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 58 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringLenient.

public void testSimpleQueryStringLenient() throws ExecutionException, InterruptedException {
    createIndex("test1", "test2");
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("field", "foo"), client().prepareIndex("test2", "type1", "10").setSource("field", 5));
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("field")).get();
    assertFailures(searchResponse);
    assertHitCount(searchResponse, 1L);
    assertSearchHits(searchResponse, "1");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo").field("field").lenient(true)).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1L);
    assertSearchHits(searchResponse, "1");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 59 with SearchResponse

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

the class SimpleQueryStringIT method testExplicitAllFieldsRequested.

public void testExplicitAllFieldsRequested() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index-with-all.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f2", "eggplant"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND)).get();
    assertHitCount(resp, 0L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND).useAllFields(true)).get();
    assertHits(resp.getHits(), "1");
    assertHitCount(resp, 1L);
    Exception e = expectThrows(Exception.class, () -> client().prepareSearch("test").setQuery(simpleQueryStringQuery("blah").field("f1").useAllFields(true)).get());
    assertThat(ExceptionsHelper.detailedMessage(e), containsString("cannot use [all_fields] parameter in conjunction with [fields]"));
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 60 with SearchResponse

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

the class SearchQueryIT method testMinScore.

public void testMinScore() throws ExecutionException, InterruptedException {
    createIndex("test");
    client().prepareIndex("test", "test", "1").setSource("score", 1.5).get();
    client().prepareIndex("test", "test", "2").setSource("score", 1.0).get();
    client().prepareIndex("test", "test", "3").setSource("score", 2.0).get();
    client().prepareIndex("test", "test", "4").setSource("score", 0.5).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(ScoreFunctionBuilders.fieldValueFactorFunction("score").missing(1.0)).setMinScore(1.5f)).get();
    assertHitCount(searchResponse, 2);
    assertFirstHit(searchResponse, hasId("3"));
    assertSecondHit(searchResponse, hasId("1"));
}
Also used : 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