Search in sources :

Example 51 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 52 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 53 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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)

Example 54 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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 55 with SearchResponse

use of org.graylog.shaded.elasticsearch7.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)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1722 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)275 SearchHit (org.elasticsearch.search.SearchHit)227 Script (org.elasticsearch.script.Script)225 ArrayList (java.util.ArrayList)217 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)139 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)97 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)95 SearchHits (org.elasticsearch.search.SearchHits)92 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)84 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)82 Test (org.junit.Test)75 Map (java.util.Map)72 SearchRequest (org.elasticsearch.action.search.SearchRequest)70