Search in sources :

Example 61 with SearchResponse

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

the class SearchQueryIT method testCustomWordDelimiterQueryString.

// see #3898
public void testCustomWordDelimiterQueryString() {
    assertAcked(client().admin().indices().prepareCreate("test").setSettings("analysis.analyzer.my_analyzer.type", "custom", "analysis.analyzer.my_analyzer.tokenizer", "whitespace", "analysis.analyzer.my_analyzer.filter", "custom_word_delimiter", "analysis.filter.custom_word_delimiter.type", "word_delimiter", "analysis.filter.custom_word_delimiter.generate_word_parts", "true", "analysis.filter.custom_word_delimiter.generate_number_parts", "false", "analysis.filter.custom_word_delimiter.catenate_numbers", "true", "analysis.filter.custom_word_delimiter.catenate_words", "false", "analysis.filter.custom_word_delimiter.split_on_case_change", "false", "analysis.filter.custom_word_delimiter.split_on_numerics", "false", "analysis.filter.custom_word_delimiter.stem_english_possessive", "false").addMapping("type1", "field1", "type=text,analyzer=my_analyzer", "field2", "type=text,analyzer=my_analyzer"));
    client().prepareIndex("test", "type1", "1").setSource("field1", "foo bar baz", "field2", "not needed").get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(queryStringQuery("foo.baz").useDisMax(false).defaultOperator(Operator.AND).field("field1").field("field2")).get();
    assertHitCount(response, 1L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 62 with SearchResponse

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

the class SearchQueryIT method testSpanMultiTermQuery.

public void testSpanMultiTermQuery() throws IOException {
    createIndex("test");
    client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar", "count", 1).get();
    client().prepareIndex("test", "test", "2").setSource("description", "foo other anything", "count", 2).get();
    client().prepareIndex("test", "test", "3").setSource("description", "foo other", "count", 3).get();
    client().prepareIndex("test", "test", "4").setSource("description", "fop", "count", 4).get();
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(spanOrQuery(spanMultiTermQueryBuilder(fuzzyQuery("description", "fop")))).get();
    assertHitCount(response, 4);
    response = client().prepareSearch("test").setQuery(spanOrQuery(spanMultiTermQueryBuilder(prefixQuery("description", "fo")))).get();
    assertHitCount(response, 4);
    response = client().prepareSearch("test").setQuery(spanOrQuery(spanMultiTermQueryBuilder(wildcardQuery("description", "oth*")))).get();
    assertHitCount(response, 3);
    response = client().prepareSearch("test").setQuery(spanOrQuery(spanMultiTermQueryBuilder(QueryBuilders.rangeQuery("description").from("ffa").to("foo")))).execute().actionGet();
    assertHitCount(response, 3);
    response = client().prepareSearch("test").setQuery(spanOrQuery(spanMultiTermQueryBuilder(regexpQuery("description", "fo{2}")))).get();
    assertHitCount(response, 3);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 63 with SearchResponse

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

the class SearchQueryIT method testQueryStringParserCache.

public void testQueryStringParserCache() throws Exception {
    createIndex("test");
    indexRandom(true, false, client().prepareIndex("test", "type", "1").setSource("nameTokens", "xyz"));
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.queryStringQuery("xyz").boost(100)).get();
    assertThat(response.getHits().getTotalHits(), equalTo(1L));
    assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
    float first = response.getHits().getAt(0).getScore();
    for (int i = 0; i < 100; i++) {
        response = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.queryStringQuery("xyz").boost(100)).get();
        assertThat(response.getHits().getTotalHits(), equalTo(1L));
        assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
        float actual = response.getHits().getAt(0).getScore();
        assertThat(i + " expected: " + first + " actual: " + actual, Float.compare(first, actual), equalTo(0));
    }
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 64 with SearchResponse

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

the class SearchQueryIT method testSimpleSpan.

// see #2994
public void testSimpleSpan() throws IOException, ExecutionException, InterruptedException {
    createIndex("test");
    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(spanOrQuery(spanTermQuery("description", "bar"))).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(spanNearQuery(spanTermQuery("description", "foo"), 3).addClause(spanTermQuery("description", "other"))).get();
    assertHitCount(searchResponse, 3L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 65 with SearchResponse

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

the class FieldSortIT method testSortOnRareField.

public void testSortOnRareField() throws IOException {
    assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("string_values").field("type", "keyword").endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1", Integer.toString(1)).setSource(jsonBuilder().startObject().array("string_values", "01", "05", "10", "08").endObject()).execute().actionGet();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(1)));
    assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("10"));
    client().prepareIndex("test", "type1", Integer.toString(2)).setSource(jsonBuilder().startObject().array("string_values", "11", "15", "20", "07").endObject()).execute().actionGet();
    for (int i = 0; i < 15; i++) {
        client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
    }
    refresh();
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(2).addSort("string_values", SortOrder.DESC).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
    assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
    assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
    client().prepareIndex("test", "type1", Integer.toString(3)).setSource(jsonBuilder().startObject().array("string_values", "02", "01", "03", "!4").endObject()).execute().actionGet();
    for (int i = 0; i < 15; i++) {
        client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
    }
    refresh();
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(3));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
    assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
    assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
    assertThat(searchResponse.getHits().getAt(2).getId(), equalTo(Integer.toString(3)));
    assertThat(searchResponse.getHits().getAt(2).getSortValues()[0], equalTo("03"));
    for (int i = 0; i < 15; i++) {
        client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
        refresh();
    }
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(3));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
    assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
    assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
    assertThat(searchResponse.getHits().getAt(2).getId(), equalTo(Integer.toString(3)));
    assertThat(searchResponse.getHits().getAt(2).getSortValues()[0], equalTo("03"));
}
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