Search in sources :

Example 41 with SearchResponse

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

the class SearchQueryIT method testQueryStringAnalyzedWildcard.

public void testQueryStringAnalyzedWildcard() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("value*")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("*ue*")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("*ue_1")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("val*e_1")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("v?l*e?1")).get();
    assertHitCount(searchResponse, 1L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 42 with SearchResponse

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

the class SearchQueryIT method testDateRangeInQueryStringWithTimeZone_7880.

// Issue #7880
public void testDateRangeInQueryStringWithTimeZone_7880() {
    //the mapping needs to be provided upfront otherwise we are not sure how many failures we get back
    //as with dynamic mappings some shards might be lacking behind and parse a different query
    assertAcked(prepareCreate("test").addMapping("type", "past", "type=date"));
    DateTimeZone timeZone = randomDateTimeZone();
    String now = ISODateTimeFormat.dateTime().print(new DateTime(timeZone));
    logger.info(" --> Using time_zone [{}], now is [{}]", timeZone.getID(), now);
    client().prepareIndex("test", "type", "1").setSource("past", now).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("past:[now-1m/m TO now+1m/m]").timeZone(timeZone.getID())).get();
    assertHitCount(searchResponse, 1L);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 43 with SearchResponse

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

the class SearchQueryIT method testNGramCopyField.

// see #5120
public void testNGramCopyField() {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.my_ngram_analyzer.type", "custom").put("index.analysis.analyzer.my_ngram_analyzer.tokenizer", "my_ngram_tokenizer").put("index.analysis.tokenizer.my_ngram_tokenizer.type", "nGram").put("index.analysis.tokenizer.my_ngram_tokenizer.min_gram", "1").put("index.analysis.tokenizer.my_ngram_tokenizer.max_gram", "10").putArray("index.analysis.tokenizer.my_ngram_tokenizer.token_chars", new String[0]));
    assertAcked(builder.addMapping("test", "origin", "type=text,copy_to=meta", "meta", "type=text,analyzer=my_ngram_analyzer"));
    // we only have ngrams as the index analyzer so searches will get standard analyzer
    client().prepareIndex("test", "test", "1").setSource("origin", "C.A1234.5678").setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "1234")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "1234.56")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(termQuery("meta", "A1234")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(termQuery("meta", "a1234")).get();
    // it's upper case
    assertHitCount(searchResponse, 0L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "A1234").analyzer("my_ngram_analyzer")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "a1234").analyzer("my_ngram_analyzer")).get();
    assertHitCount(searchResponse, 1L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 44 with SearchResponse

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

the class SearchQueryIT method testMultiMatchLenientIssue3797.

// see #3797
public void testMultiMatchLenientIssue3797() {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", 123, "field2", "value2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(multiMatchQuery("value2", "field2").field("field1", 2).lenient(true).useDisMax(false)).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(multiMatchQuery("value2", "field2").field("field1", 2).lenient(true).useDisMax(true)).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(multiMatchQuery("value2").field("field2", 2).lenient(true)).get();
    assertHitCount(searchResponse, 1L);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 45 with SearchResponse

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

the class SearchQueryIT method testAllDocsQueryString.

// see #3521
public void testAllDocsQueryString() throws InterruptedException, ExecutionException {
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("foo", "bar"), client().prepareIndex("test", "type1", "2").setSource("foo", "bar"));
    int iters = scaledRandomIntBetween(100, 200);
    for (int i = 0; i < iters; i++) {
        SearchResponse searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("*:*^10.0").boost(10.0f)).get();
        assertHitCount(searchResponse, 2L);
        searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchAllQuery()))).get();
        assertHitCount(searchResponse, 2L);
        assertThat((double) searchResponse.getHits().getAt(0).getScore(), closeTo(2.0, 0.1));
        assertThat((double) searchResponse.getHits().getAt(1).getScore(), closeTo(2.0, 0.1));
    }
}
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