Search in sources :

Example 56 with SearchResponse

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

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

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

Example 59 with SearchResponse

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

the class SearchQueryIT method testMultiMatchQuery.

public void testMultiMatchQuery() throws Exception {
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3"), client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2"), client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1"));
    MultiMatchQueryBuilder builder = multiMatchQuery("value1 value2 value4", "field1", "field2");
    SearchResponse searchResponse = client().prepareSearch().setQuery(builder).addAggregation(AggregationBuilders.terms("field1").field("field1.keyword")).get();
    assertHitCount(searchResponse, 2L);
    // this uses dismax so scores are equal and the order can be arbitrary
    assertSearchHits(searchResponse, "1", "2");
    builder.useDisMax(false);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    client().admin().indices().prepareRefresh("test").get();
    builder = multiMatchQuery("value1", "field1", "field2").operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
    refresh();
    builder = multiMatchQuery("value1", "field1").field("field3", 1.5f).operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "1");
    client().admin().indices().prepareRefresh("test").get();
    builder = multiMatchQuery("value1").field("field1").field("field3", 1.5f).operator(// Operator only applies on terms inside a field! Fields are always OR-ed together.
    Operator.AND);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "3", "1");
    // Test lenient
    client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).get();
    refresh();
    builder = multiMatchQuery("value1", "field1", "field2", "field4");
    assertFailures(client().prepareSearch().setQuery(builder), RestStatus.BAD_REQUEST, containsString("NumberFormatException[For input string: \"value1\"]"));
    builder.lenient(true);
    searchResponse = client().prepareSearch().setQuery(builder).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("1"));
}
Also used : MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 60 with SearchResponse

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

the class SearchQueryIT method testFilterExistsMissing.

public void testFilterExistsMissing() throws Exception {
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x1", "x_1").field("field1", "value1_1").field("field2", "value2_1").endObject()), client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x2", "x_2").field("field1", "value1_2").endObject()), client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y1", "y_1").field("field2", "value2_3").endObject()), client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y2", "y_2").field("field3", "value3_4").endObject()));
    SearchResponse searchResponse = client().prepareSearch().setQuery(existsQuery("field1")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    searchResponse = client().prepareSearch().setQuery(constantScoreQuery(existsQuery("field1"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("_exists_:field1")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    searchResponse = client().prepareSearch().setQuery(existsQuery("field2")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    searchResponse = client().prepareSearch().setQuery(existsQuery("field3")).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("4"));
    // wildcard check
    searchResponse = client().prepareSearch().setQuery(existsQuery("x*")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
    // object check
    searchResponse = client().prepareSearch().setQuery(existsQuery("obj1")).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "2");
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

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