Search in sources :

Example 6 with SearchRequest

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

the class SearchRequestTests method mutate.

private SearchRequest mutate(SearchRequest searchRequest) throws IOException {
    SearchRequest mutation = copyRequest(searchRequest);
    List<Runnable> mutators = new ArrayList<>();
    mutators.add(() -> mutation.indices(ArrayUtils.concat(searchRequest.indices(), new String[] { randomAsciiOfLength(10) })));
    mutators.add(() -> mutation.indicesOptions(randomValueOtherThan(searchRequest.indicesOptions(), () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()))));
    mutators.add(() -> mutation.types(ArrayUtils.concat(searchRequest.types(), new String[] { randomAsciiOfLength(10) })));
    mutators.add(() -> mutation.preference(randomValueOtherThan(searchRequest.preference(), () -> randomAsciiOfLengthBetween(3, 10))));
    mutators.add(() -> mutation.routing(randomValueOtherThan(searchRequest.routing(), () -> randomAsciiOfLengthBetween(3, 10))));
    mutators.add(() -> mutation.requestCache((randomValueOtherThan(searchRequest.requestCache(), () -> randomBoolean()))));
    mutators.add(() -> mutation.scroll(randomValueOtherThan(searchRequest.scroll(), () -> new Scroll(new TimeValue(randomNonNegativeLong() % 100000)))));
    mutators.add(() -> mutation.searchType(randomValueOtherThan(searchRequest.searchType(), () -> randomFrom(SearchType.values()))));
    mutators.add(() -> mutation.source(randomValueOtherThan(searchRequest.source(), this::createSearchSourceBuilder)));
    randomFrom(mutators).run();
    return mutation;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ArrayList(java.util.ArrayList) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 7 with SearchRequest

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

the class SearchRequestTests method copyRequest.

private static SearchRequest copyRequest(SearchRequest searchRequest) throws IOException {
    SearchRequest result = new SearchRequest();
    result.indices(searchRequest.indices());
    result.indicesOptions(searchRequest.indicesOptions());
    result.types(searchRequest.types());
    result.searchType(searchRequest.searchType());
    result.preference(searchRequest.preference());
    result.routing(searchRequest.routing());
    result.requestCache(searchRequest.requestCache());
    result.scroll(searchRequest.scroll());
    if (searchRequest.source() != null) {
        result.source(searchRequest.source());
    }
    return result;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest)

Example 8 with SearchRequest

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

the class ShardSearchTransportRequestTests method createShardSearchTransportRequest.

private ShardSearchTransportRequest createShardSearchTransportRequest() throws IOException {
    SearchRequest searchRequest = createSearchRequest();
    ShardId shardId = new ShardId(randomAsciiOfLengthBetween(2, 10), randomAsciiOfLengthBetween(2, 10), randomInt());
    final AliasFilter filteringAliases;
    if (randomBoolean()) {
        String[] strings = generateRandomStringArray(10, 10, false, false);
        filteringAliases = new AliasFilter(RandomQueryBuilder.createQuery(random()), strings);
    } else {
        filteringAliases = new AliasFilter(null, Strings.EMPTY_ARRAY);
    }
    return new ShardSearchTransportRequest(searchRequest, shardId, randomIntBetween(1, 100), filteringAliases, randomBoolean() ? 1.0f : randomFloat(), Math.abs(randomLong()));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SearchRequest(org.elasticsearch.action.search.SearchRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 9 with SearchRequest

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

the class SearchTemplateIT method testTemplateQueryAsEscapedStringStartingWithConditionalClause.

/**
     * Test that template can contain conditional clause. In this case it is at
     * the beginning of the string.
     */
public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String templateString = "{" + "  \"inline\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\"," + "  \"params\":{" + "    \"size\": 1," + "    \"use_size\": true" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(createParser(JsonXContent.jsonXContent, templateString));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 10 with SearchRequest

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

the class SearchTemplateIT method testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd.

/**
     * Test that template can contain conditional clause. In this case it is at
     * the end of the string.
     */
public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String templateString = "{" + "  \"inline\" : \"{ \\\"query\\\":{\\\"match_all\\\":{}} {{#use_size}}, \\\"size\\\": \\\"{{size}}\\\" {{/use_size}} }\"," + "  \"params\":{" + "    \"size\": 1," + "    \"use_size\": true" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(createParser(JsonXContent.jsonXContent, templateString));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)66 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 BytesArray (org.elasticsearch.common.bytes.BytesArray)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)6 ArrayList (java.util.ArrayList)5 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 Test (org.junit.Test)5 List (java.util.List)4