Search in sources :

Example 21 with SearchRequest

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

the class RemoteRequestBuildersTests method testInitialSearchParamsSort.

public void testInitialSearchParamsSort() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
    // Test sort:_doc for versions that support it.
    Version remoteVersion = Version.fromId(between(Version.V_2_1_0_ID, Version.CURRENT.id));
    searchRequest.source().sort("_doc");
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "_doc:asc"));
    // Test search_type scan for versions that don't support sort:_doc.
    remoteVersion = Version.fromId(between(0, Version.V_2_1_0_ID - 1));
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("search_type", "scan"));
    // Test sorting by some field. Version doesn't matter.
    remoteVersion = Version.fromId(between(0, Version.CURRENT.id));
    searchRequest.source().sorts().clear();
    searchRequest.source().sort("foo");
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "foo:asc"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Version(org.elasticsearch.Version) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 22 with SearchRequest

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

the class RemoteRequestBuildersTests method testInitialSearchEntity.

public void testInitialSearchEntity() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(new SearchSourceBuilder());
    String query = "{\"match_all\":{}}";
    HttpEntity entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":true}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Source filtering is included if set up
    searchRequest.source().fetchSource(new String[] { "in1", "in2" }, new String[] { "out" });
    entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Invalid XContent fails
    RuntimeException e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{}, \"trailing\": {}")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))"));
    e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 23 with SearchRequest

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

the class RemoteRequestBuildersTests method testIntialSearchPath.

public void testIntialSearchPath() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
    assertEquals("/_search", initialSearchPath(searchRequest));
    searchRequest.indices("a");
    searchRequest.types("b");
    assertEquals("/a/b/_search", initialSearchPath(searchRequest));
    searchRequest.indices("a", "b");
    searchRequest.types("c", "d");
    assertEquals("/a,b/c,d/_search", initialSearchPath(searchRequest));
    searchRequest.indices("cat,");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("cat,", "dog");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("dog", "cat,");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("cat/");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("cat/", "dog");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("dog", "cat/");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("ok");
    searchRequest.types("cat,");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("cat,", "dog");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("dog", "cat,");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("cat/");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
    searchRequest.types("cat/", "dog");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
    searchRequest.types("dog", "cat/");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 24 with SearchRequest

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

the class RestReindexActionTests method testReindexFromRemoteRequestParsing.

public void testReindexFromRemoteRequestParsing() throws IOException {
    BytesReference request;
    try (XContentBuilder b = JsonXContent.contentBuilder()) {
        b.startObject();
        {
            b.startObject("source");
            {
                b.startObject("remote");
                {
                    b.field("host", "http://localhost:9200");
                }
                b.endObject();
                b.field("index", "source");
            }
            b.endObject();
            b.startObject("dest");
            {
                b.field("index", "dest");
            }
            b.endObject();
        }
        b.endObject();
        request = b.bytes();
    }
    try (XContentParser p = createParser(JsonXContent.jsonXContent, request)) {
        ReindexRequest r = new ReindexRequest(new SearchRequest(), new IndexRequest());
        RestReindexAction.PARSER.parse(p, r, null);
        assertEquals("localhost", r.getRemoteInfo().getHost());
        assertArrayEquals(new String[] { "source" }, r.getSearchRequest().indices());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 25 with SearchRequest

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

the class ReindexRequestTests method newRequest.

@Override
protected ReindexRequest newRequest() {
    ReindexRequest reindex = new ReindexRequest(new SearchRequest(), new IndexRequest());
    reindex.getSearchRequest().indices("source");
    reindex.getDestination().index("dest");
    return reindex;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

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