Search in sources :

Example 51 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringAnalyzeWildcard.

public void testSimpleQueryStringAnalyzeWildcard() throws ExecutionException, InterruptedException, IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "text").field("analyzer", "german").endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("test1").addMapping("type1", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("location", "Köln"));
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("Köln*").field("location")).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1L);
    assertSearchHits(searchResponse, "1");
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 52 with SearchResponse

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

the class SimpleQueryStringIT method testWithDate.

public void testWithDate() 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("f1", "foo", "f_date", "2015/09/02"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar", "f_date", "2015/09/01"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo bar")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("\"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1");
    assertHitCount(resp, 1L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar \"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("\"2015/09/02\" \"2015/09/01\"")).get();
    assertHits(resp.getHits(), "1", "2");
    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 53 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringFlags.

public void testSimpleQueryStringFlags() throws ExecutionException, InterruptedException {
    createIndex("test");
    indexRandom(true, 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", "quux baz eggplant"), client().prepareIndex("test", "type1", "5").setSource("body", "quux baz spaghetti"), client().prepareIndex("test", "type1", "6").setSource("otherbody", "spaghetti"));
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar").flags(SimpleQueryStringFlag.ALL)).get();
    assertHitCount(searchResponse, 3L);
    assertSearchHits(searchResponse, "1", "2", "3");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo | bar").defaultOperator(Operator.AND).flags(SimpleQueryStringFlag.OR)).get();
    assertHitCount(searchResponse, 3L);
    assertSearchHits(searchResponse, "1", "2", "3");
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo | bar").defaultOperator(Operator.AND).flags(SimpleQueryStringFlag.NONE)).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("3"));
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("baz | egg*").defaultOperator(Operator.AND).flags(SimpleQueryStringFlag.NONE)).get();
    assertHitCount(searchResponse, 0L);
    searchResponse = client().prepareSearch().setSource(new SearchSourceBuilder().query(QueryBuilders.simpleQueryStringQuery("foo|bar").defaultOperator(Operator.AND).flags(SimpleQueryStringFlag.NONE))).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("quuz~1 + egg*").flags(SimpleQueryStringFlag.WHITESPACE, SimpleQueryStringFlag.AND, SimpleQueryStringFlag.FUZZY, SimpleQueryStringFlag.PREFIX)).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("4"));
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 54 with SearchResponse

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

the class SimpleQueryStringIT method testSimpleQueryStringOnIndexMetaField.

public void testSimpleQueryStringOnIndexMetaField() throws Exception {
    client().prepareIndex("test", "type1", "1").setSource("foo", 123, "bar", "abc").get();
    client().prepareIndex("test", "type1", "2").setSource("foo", 234, "bar", "bcd").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("test").field("_index")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 55 with SearchResponse

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

the class SimpleQueryStringIT method testLenientFlagBeingTooLenient.

// Issue #7967
public void testLenientFlagBeingTooLenient() throws Exception {
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("num", 1, "body", "foo bar baz"), client().prepareIndex("test", "doc", "2").setSource("num", 2, "body", "eggplant spaghetti lasagna"));
    BoolQueryBuilder q = boolQuery().should(simpleQueryStringQuery("bar").field("num").field("body").lenient(true));
    SearchResponse resp = client().prepareSearch("test").setQuery(q).get();
    assertNoFailures(resp);
    // the bug is that this would be parsed into basically a match_all
    // query and this would match both documents
    assertHitCount(resp, 1);
    assertSearchHits(resp, "1");
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

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