Search in sources :

Example 61 with SearchResponse

use of 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 62 with SearchResponse

use of 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)

Example 63 with SearchResponse

use of 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 64 with SearchResponse

use of 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 65 with SearchResponse

use of 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)

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