Java 类org.elasticsearch.action.search.SearchRequest 实例源码

项目:elasticsearch_my    文件:RestSearchTemplateAction.java   
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (request.hasContentOrSourceParam() == false) {
        throw new ElasticsearchException("request body is required");
    }

    // Creates the search request with all required params
    SearchRequest searchRequest = new SearchRequest();
    RestSearchAction.parseSearchRequest(searchRequest, request, null);

    // Creates the search template request
    SearchTemplateRequest searchTemplateRequest;
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        searchTemplateRequest = PARSER.parse(parser, new SearchTemplateRequest(), null);
    }
    searchTemplateRequest.setRequest(searchRequest);

    return channel -> client.execute(SearchTemplateAction.INSTANCE, searchTemplateRequest, new RestStatusToXContentListener<>(channel));
}
项目:es-sql    文件:ActionRequestRestExecuter.java   
/**
 * Execute the ActionRequest and returns the REST response using the channel.
 */
public void execute() throws Exception {
       ActionRequest request = requestBuilder.request();

       //todo: maby change to instanceof multi?
       if(requestBuilder instanceof JoinRequestBuilder){
           executeJoinRequestAndSendResponse();
       }
    else if (request instanceof SearchRequest) {
        client.search((SearchRequest) request, new RestStatusToXContentListener<SearchResponse>(channel));
    } else if (requestBuilder instanceof SqlElasticDeleteByQueryRequestBuilder) {
           throw new UnsupportedOperationException("currently not support delete on elastic 2.0.0");
       }
       else if(request instanceof GetIndexRequest) {
           this.requestBuilder.getBuilder().execute( new GetIndexRequestRestListener(channel, (GetIndexRequest) request));
       }


    else {
        throw new Exception(String.format("Unsupported ActionRequest provided: %s", request.getClass().getName()));
    }
}
项目:elasticsearch_my    文件:SearchTemplateIT.java   
/**
 * 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));
}
项目:elasticsearch_my    文件:SearchTemplateIT.java   
/**
 * 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));
}
项目:elasticsearch_my    文件:RestUpdateByQueryAction.java   
@Override
@SuppressWarnings("unchecked")
protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOException {
    /*
     * Passing the search request through UpdateByQueryRequest first allows
     * it to set its own defaults which differ from SearchRequest's
     * defaults. Then the parse can override them.
     */
    UpdateByQueryRequest internal = new UpdateByQueryRequest(new SearchRequest());

    Map<String, Consumer<Object>> consumers = new HashMap<>();
    consumers.put("conflicts", o -> internal.setConflicts((String) o));
    consumers.put("script", o -> internal.setScript(parseScript((Map<String, Object>)o)));

    parseInternalRequest(internal, request, consumers);

    internal.setPipeline(request.param("pipeline"));
    return internal;
}
项目:Elasticsearch    文件:DfsOnlyRequest.java   
public DfsOnlyRequest(Fields termVectorsFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
    super(indices);

    // build a search request with a query of all the terms
    final BoolQueryBuilder boolBuilder = boolQuery();
    for (String fieldName : termVectorsFields) {
        if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
            continue;
        }
        Terms terms = termVectorsFields.terms(fieldName);
        TermsEnum iterator = terms.iterator();
        while (iterator.next() != null) {
            String text = iterator.term().utf8ToString();
            boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
        }
    }
    // wrap a search request object
    this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
}
项目:elasticsearch_my    文件:TransportReindexAction.java   
/**
 * Throws an ActionRequestValidationException if the request tries to index
 * back into the same index or into an index that points to two indexes.
 * This cannot be done during request validation because the cluster state
 * isn't available then. Package private for testing.
 */
static void validateAgainstAliases(SearchRequest source, IndexRequest destination, RemoteInfo remoteInfo,
                                     IndexNameExpressionResolver indexNameExpressionResolver, AutoCreateIndex autoCreateIndex,
                                     ClusterState clusterState) {
    if (remoteInfo != null) {
        return;
    }
    String target = destination.index();
    if (false == autoCreateIndex.shouldAutoCreate(target, clusterState)) {
        /*
         * If we're going to autocreate the index we don't need to resolve
         * it. This is the same sort of dance that TransportIndexRequest
         * uses to decide to autocreate the index.
         */
        target = indexNameExpressionResolver.concreteIndexNames(clusterState, destination)[0];
    }
    for (String sourceIndex : indexNameExpressionResolver.concreteIndexNames(clusterState, source)) {
        if (sourceIndex.equals(target)) {
            ActionRequestValidationException e = new ActionRequestValidationException();
            e.addValidationError("reindex cannot write into an index its reading from [" + target + ']');
            throw e;
        }
    }
}
项目:database-transform-tool    文件:ElasticsearchHighRestFactory.java   
public String selectMatchAll(String indexs,String types,String field,String value){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
            search.query(QueryBuilders.matchQuery(field, value));
        }
        search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:Elasticsearch    文件:SearchServiceTransportAction.java   
public void sendFreeContext(DiscoveryNode node, final long contextId, SearchRequest request) {
    transportService.sendRequest(node, FREE_CONTEXT_ACTION_NAME, new SearchFreeContextRequest(request, contextId), new ActionListenerResponseHandler<SearchFreeContextResponse>(new ActionListener<SearchFreeContextResponse>() {
        @Override
        public void onResponse(SearchFreeContextResponse response) {
            // no need to respond if it was freed or not
        }

        @Override
        public void onFailure(Throwable e) {

        }
    }) {
        @Override
        public SearchFreeContextResponse newInstance() {
            return new SearchFreeContextResponse();
        }
    });
}
项目:Elasticsearch    文件:CountRequest.java   
public SearchRequest toSearchRequest() {
    SearchRequest searchRequest = new SearchRequest(indices());
    searchRequest.indicesOptions(indicesOptions());
    searchRequest.types(types());
    searchRequest.routing(routing());
    searchRequest.preference(preference());
    searchRequest.source(source());
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0);
    if (minScore() != DEFAULT_MIN_SCORE) {
        searchSourceBuilder.minScore(minScore());
    }
    if (terminateAfter() != DEFAULT_TERMINATE_AFTER) {
        searchSourceBuilder.terminateAfter(terminateAfter());
    }
    searchRequest.extraSource(searchSourceBuilder);
    return searchRequest;
}
项目:elasticsearch_my    文件:RoundTripTests.java   
public void testDeleteByQueryRequest() throws IOException {
    DeleteByQueryRequest delete = new DeleteByQueryRequest(new SearchRequest());
    randomRequest(delete);
    DeleteByQueryRequest tripped = new DeleteByQueryRequest();
    roundTrip(delete, tripped);
    assertRequestEquals(delete, tripped);

    // Try slices with a version that doesn't support slices. That should fail.
    delete.setSlices(between(2, 1000));
    Exception e = expectThrows(IllegalArgumentException.class, () -> roundTrip(Version.V_5_0_0_rc1, delete, null));
    assertEquals("Attempting to send sliced reindex-style request to a node that doesn't support it. "
            + "Version is [5.0.0-rc1] but must be [5.1.1]", e.getMessage());

    // Try without slices with a version that doesn't support slices. That should work.
    tripped = new DeleteByQueryRequest();
    delete.setSlices(1);
    roundTrip(Version.V_5_0_0_rc1, delete, tripped);
    assertRequestEquals(delete, tripped);
}
项目:elasticsearch_my    文件:RemoteScrollableHitSourceTests.java   
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    final ExecutorService directExecutor = EsExecutors.newDirectExecutorService();
    threadPool = new TestThreadPool(getTestName()) {
        @Override
        public ExecutorService executor(String name) {
            return directExecutor;
        }

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            command.run();
            return null;
        }
    };
    retries = 0;
    searchRequest = new SearchRequest();
    searchRequest.scroll(timeValueMinutes(5));
    searchRequest.source(new SearchSourceBuilder().size(10).version(true).sort("_doc").size(123));
    retriesAllowed = 0;
}
项目:elasticsearch_my    文件:RemoteRequestBuildersTests.java   
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"));
}
项目:elasticsearch_my    文件:RemoteRequestBuildersTests.java   
public void testInitialSearchParamsFields() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());

    // Test request without any fields
    Version remoteVersion = VersionUtils.randomVersion(random());
    assertThat(initialSearchParams(searchRequest, remoteVersion),
            not(either(hasKey("stored_fields")).or(hasKey("fields"))));

    // Setup some fields for the next two tests
    searchRequest.source().storedField("_source").storedField("_id");

    // Test stored_fields for versions that support it
    remoteVersion = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0_alpha4, null);
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("stored_fields", "_source,_id"));

    // Test fields for versions that support it
    remoteVersion = VersionUtils.randomVersionBetween(random(), null, Version.V_5_0_0_alpha3);
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("fields", "_source,_id"));
}
项目:elasticsearch_my    文件:RemoteRequestBuildersTests.java   
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"));
}
项目:elasticsearch_my    文件:AbstractBulkByScrollRequest.java   
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    searchRequest = new SearchRequest();
    searchRequest.readFrom(in);
    abortOnVersionConflict = in.readBoolean();
    size = in.readVInt();
    refresh = in.readBoolean();
    timeout = new TimeValue(in);
    activeShardCount = ActiveShardCount.readFrom(in);
    retryBackoffInitialTime = new TimeValue(in);
    maxRetries = in.readVInt();
    requestsPerSecond = in.readFloat();
    if (in.getVersion().onOrAfter(Version.V_5_1_1_UNRELEASED)) {
        slices = in.readVInt();
    } else {
        slices = 1;
    }
}
项目:talk-observing-distributed-systems    文件:ElasticsearchTweetRepository.java   
public String find() {
  try {
    final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.sort("_score");
    searchSourceBuilder.sort("_uid");

    final String queryText = "*";
    final SimpleQueryStringBuilder simpleQueryStringBuilder =
        new SimpleQueryStringBuilder(queryText);
    simpleQueryStringBuilder.defaultOperator(Operator.AND);
    searchSourceBuilder.query(simpleQueryStringBuilder);
    searchSourceBuilder.size(500);

    //LOGGER.info("Elasticsearch query: {}", searchSourceBuilder.toString());

    final SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("tweets");
    searchRequest.types("tweet");
    searchRequest.source(searchSourceBuilder);
    final SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
    return searchResponse.toString();
  } catch (IOException e) {
    e.printStackTrace();
    return e.getMessage();
  }
}
项目:elasticsearch_my    文件:AsyncBulkByScrollActionTests.java   
@Before
public void setupForTest() {
    // Fill the context with something random so we can make sure we inherited it appropriately.
    expectedHeaders.clear();
    expectedHeaders.put(randomSimpleString(random()), randomSimpleString(random()));

    setupClient(new TestThreadPool(getTestName()));
    firstSearchRequest = new SearchRequest();
    testRequest = new DummyAbstractBulkByScrollRequest(firstSearchRequest);
    listener = new PlainActionFuture<>();
    scrollId = null;
    taskManager = new TaskManager(Settings.EMPTY);
    testTask = (WorkingBulkByScrollTask) taskManager.register("don'tcare", "hereeither", testRequest);

    localNode = new DiscoveryNode("thenode", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    taskId = new TaskId(localNode.getId(), testTask.getId());
}
项目:elasticsearch_my    文件:IndicesRequestIT.java   
public void testSearchQueryThenFetch() throws Exception {
    interceptTransportActions(SearchTransportService.QUERY_ACTION_NAME,
            SearchTransportService.FETCH_ID_ACTION_NAME, SearchTransportService.FREE_CONTEXT_ACTION_NAME);

    String[] randomIndicesOrAliases = randomIndicesOrAliases();
    for (int i = 0; i < randomIndicesOrAliases.length; i++) {
        client().prepareIndex(randomIndicesOrAliases[i], "type", "id-" + i).setSource("field", "value").get();
    }
    refresh();

    SearchRequest searchRequest = new SearchRequest(randomIndicesOrAliases).searchType(SearchType.QUERY_THEN_FETCH);
    SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().search(searchRequest).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L));

    clearInterceptedActions();
    assertSameIndices(searchRequest, SearchTransportService.QUERY_ACTION_NAME, SearchTransportService.FETCH_ID_ACTION_NAME);
    //free context messages are not necessarily sent, but if they are, check their indices
    assertSameIndicesOptionalRequests(searchRequest, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
}
项目:elasticsearch_my    文件:IndicesRequestIT.java   
public void testSearchDfsQueryThenFetch() throws Exception {
    interceptTransportActions(SearchTransportService.DFS_ACTION_NAME, SearchTransportService.QUERY_ID_ACTION_NAME,
            SearchTransportService.FETCH_ID_ACTION_NAME, SearchTransportService.FREE_CONTEXT_ACTION_NAME);

    String[] randomIndicesOrAliases = randomIndicesOrAliases();
    for (int i = 0; i < randomIndicesOrAliases.length; i++) {
        client().prepareIndex(randomIndicesOrAliases[i], "type", "id-" + i).setSource("field", "value").get();
    }
    refresh();

    SearchRequest searchRequest = new SearchRequest(randomIndicesOrAliases).searchType(SearchType.DFS_QUERY_THEN_FETCH);
    SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().search(searchRequest).actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), greaterThan(0L));

    clearInterceptedActions();
    assertSameIndices(searchRequest, SearchTransportService.DFS_ACTION_NAME, SearchTransportService.QUERY_ID_ACTION_NAME,
            SearchTransportService.FETCH_ID_ACTION_NAME);
    //free context messages are not necessarily sent, but if they are, check their indices
    assertSameIndicesOptionalRequests(searchRequest, SearchTransportService.FREE_CONTEXT_ACTION_NAME);
}
项目:css-elasticsearch    文件:ElasticsearchHighRestFactory.java   
public String selectAll(String indexs,String types,String condition){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        search.query(QueryBuilders.queryStringQuery(condition)); 
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:css-elasticsearch    文件:ElasticsearchHighRestFactory.java   
public String selectMatchAll(String indexs,String types,String field,String value){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
            search.query(QueryBuilders.matchQuery(field, value));
        }
        search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:css-elasticsearch    文件:ElasticsearchExtendHighRestFactory.java   
public String selectTermAll(String indexs,String types,String field,String value){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
            search.query(QueryBuilders.termQuery(field, value));
        }
        search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:elasticsearch_my    文件:SearchTemplateRequest.java   
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    request = in.readOptionalStreamable(SearchRequest::new);
    simulate = in.readBoolean();
    explain = in.readBoolean();
    profile = in.readBoolean();
    scriptType = ScriptType.readFrom(in);
    script = in.readOptionalString();
    if (in.readBoolean()) {
        scriptParams = in.readMap();
    }
}
项目:elasticsearch_my    文件:SearchTemplateIT.java   
/**
 * Test that template can be expressed as a single escaped string.
 */
public void testTemplateQueryAsEscapedString() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String query =
              "{" + "  \"inline\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\","
            + "  \"params\":{"
            + "    \"size\": 1"
            + "  }"
            + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(createParser(JsonXContent.jsonXContent, query));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
项目:elasticsearch_my    文件:SearchTemplateIT.java   
public void testIndexedTemplateWithArray() throws Exception {
    String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
    assertAcked(
            client().admin().cluster().preparePutStoredScript()
                    .setLang(MustacheScriptEngineService.NAME)
                    .setId("4")
                    .setContent(jsonBuilder().startObject().field("template", multiQuery).endObject().bytes(), XContentType.JSON)
    );
    BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
    bulkRequestBuilder.get();
    client().admin().indices().prepareRefresh().get();

    Map<String, Object> arrayTemplateParams = new HashMap<>();
    String[] fieldParams = {"foo", "bar"};
    arrayTemplateParams.put("fieldParam", fieldParams);

    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client())
            .setRequest(new SearchRequest("test").types("type"))
            .setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams)
            .get();
    assertHitCount(searchResponse.getResponse(), 5);
}
项目:Elasticsearch    文件:IndexShard.java   
public long getShardDocNumByType(String type) {
    String query = String.format(DOCNUM_QUERY_PATTERN, 1);
    SearchRequest searchRequest = generateLocalSearchRequest(query, shardId.getIndex(), type);
    ShardSearchLocalRequest request = new ShardSearchLocalRequest(shardId, 1, SearchType.QUERY_AND_FETCH,
            searchRequest.source(), searchRequest.types(), true);
    request.putHeader("search_source", "reindex");
    QueryFetchSearchResult result = shardSearchService.executeFetchPhase(request);
    return result.queryResult().topDocs().totalHits;
}
项目:Elasticsearch    文件:PutWarmerRequest.java   
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    name = in.readString();
    if (in.readBoolean()) {
        searchRequest = new SearchRequest();
        searchRequest.readFrom(in);
    }
    readTimeout(in);
}
项目:elasticsearch_my    文件:RemoteRequestBuilders.java   
static String initialSearchPath(SearchRequest searchRequest) {
    // It is nasty to build paths with StringBuilder but we'll be careful....
    StringBuilder path = new StringBuilder("/");
    addIndexesOrTypes(path, "Index", searchRequest.indices());
    addIndexesOrTypes(path, "Type", searchRequest.types());
    path.append("_search");
    return path.toString();
}
项目:elasticsearch_my    文件:RemoteRequestBuilders.java   
static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReference query) {
    // EMPTY is safe here because we're not calling namedObject
    try (XContentBuilder entity = JsonXContent.contentBuilder();
            XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, query)) {
        entity.startObject();

        entity.field("query"); {
            /* We're intentionally a bit paranoid here - copying the query as xcontent rather than writing a raw field. We don't want
             * poorly written queries to escape. Ever. */
            entity.copyCurrentStructure(queryParser);
            XContentParser.Token shouldBeEof = queryParser.nextToken();
            if (shouldBeEof != null) {
                throw new ElasticsearchException(
                        "query was more than a single object. This first token after the object is [" + shouldBeEof + "]");
            }
        }

        if (searchRequest.source().fetchSource() != null) {
            entity.field("_source", searchRequest.source().fetchSource());
        } else {
            entity.field("_source", true);
        }

        entity.endObject();
        BytesRef bytes = entity.bytes().toBytesRef();
        return new ByteArrayEntity(bytes.bytes, bytes.offset, bytes.length, ContentType.APPLICATION_JSON);
    } catch (IOException e) {
        throw new ElasticsearchException("unexpected error building entity", e);
    }
}
项目:elasticsearch_my    文件:RestReindexActionTests.java   
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());
    }
}
项目:elasticsearch_my    文件:RoundTripTests.java   
public void testReindexRequest() throws IOException {
    ReindexRequest reindex = new ReindexRequest(new SearchRequest(), new IndexRequest());
    randomRequest(reindex);
    reindex.getDestination().version(randomFrom(Versions.MATCH_ANY, Versions.MATCH_DELETED, 12L, 1L, 123124L, 12L));
    reindex.getDestination().index("test");
    if (randomBoolean()) {
        int port = between(1, Integer.MAX_VALUE);
        BytesReference query = new BytesArray(randomAsciiOfLength(5));
        String username = randomBoolean() ? randomAsciiOfLength(5) : null;
        String password = username != null && randomBoolean() ? randomAsciiOfLength(5) : null;
        int headersCount = randomBoolean() ? 0 : between(1, 10);
        Map<String, String> headers = new HashMap<>(headersCount);
        while (headers.size() < headersCount) {
            headers.put(randomAsciiOfLength(5), randomAsciiOfLength(5));
        }
        TimeValue socketTimeout = parseTimeValue(randomPositiveTimeValue(), "socketTimeout");
        TimeValue connectTimeout = parseTimeValue(randomPositiveTimeValue(), "connectTimeout");
        reindex.setRemoteInfo(new RemoteInfo(randomAsciiOfLength(5), randomAsciiOfLength(5), port, query, username, password, headers,
                socketTimeout, connectTimeout));
    }
    ReindexRequest tripped = new ReindexRequest();
    roundTrip(reindex, tripped);
    assertRequestEquals(reindex, tripped);

    // Try slices with a version that doesn't support slices. That should fail.
    reindex.setSlices(between(2, 1000));
    Exception e = expectThrows(IllegalArgumentException.class, () -> roundTrip(Version.V_5_0_0_rc1, reindex, null));
    assertEquals("Attempting to send sliced reindex-style request to a node that doesn't support it. "
            + "Version is [5.0.0-rc1] but must be [5.1.1]", e.getMessage());

    // Try without slices with a version that doesn't support slices. That should work.
    tripped = new ReindexRequest();
    reindex.setSlices(1);
    roundTrip(Version.V_5_0_0_rc1, reindex, tripped);
    assertRequestEquals(Version.V_5_0_0_rc1, reindex, tripped);
}
项目:elasticsearch_my    文件:ReindexRequestTests.java   
@Override
protected ReindexRequest newRequest() {
    ReindexRequest reindex = new ReindexRequest(new SearchRequest(), new IndexRequest());
    reindex.getSearchRequest().indices("source");
    reindex.getDestination().index("dest");
    return reindex;
}
项目:elasticsearch_my    文件:RemoteRequestBuildersTests.java   
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/");
}
项目:elasticsearch_my    文件:UpdateByQueryRequestTests.java   
public void testUpdateByQueryRequestImplementsIndicesRequestReplaceable() {
    int numIndices = between(1, 100);
    String[] indices = new String[numIndices];
    for (int i = 0; i < numIndices; i++) {
        indices[i] = randomSimpleString(random(), 1, 30);
    }

    SearchRequest searchRequest = new SearchRequest(indices);
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    searchRequest.indicesOptions(indicesOptions);

    UpdateByQueryRequest request = new UpdateByQueryRequest(searchRequest);
    for (int i = 0; i < numIndices; i++) {
        assertEquals(indices[i], request.indices()[i]);
    }

    assertSame(indicesOptions, request.indicesOptions());
    assertSame(request.indicesOptions(), request.getSearchRequest().indicesOptions());

    int numNewIndices = between(1, 100);
    String[] newIndices = new String[numNewIndices];
    for (int i = 0; i < numNewIndices; i++) {
        newIndices[i] = randomSimpleString(random(), 1, 30);
    }
    request.indices(newIndices);
    for (int i = 0; i < numNewIndices; i++) {;
        assertEquals(newIndices[i], request.indices()[i]);
    }
    for (int i = 0; i < numNewIndices; i++) {;
        assertEquals(newIndices[i], request.getSearchRequest().indices()[i]);
    }
}
项目:elasticsearch_my    文件:TransportNoopSearchAction.java   
@Override
protected void doExecute(SearchRequest request, ActionListener<SearchResponse> listener) {
    listener.onResponse(new SearchResponse(new InternalSearchResponse(
        new SearchHits(
            new SearchHit[0], 0L, 0.0f),
        new InternalAggregations(Collections.emptyList()),
        new Suggest(Collections.emptyList()),
        new SearchProfileShardResults(Collections.emptyMap()), false, false, 1), "", 1, 1, 0, new ShardSearchFailure[0]));
}
项目:elasticsearch_my    文件:ContextAndHeaderTransportIT.java   
public void testThatRelevantHttpHeadersBecomeRequestHeaders() throws IOException {
    final String IRRELEVANT_HEADER = "SomeIrrelevantHeader";
    Response response = getRestClient().performRequest("GET", "/" + queryIndex + "/_search",
            new BasicHeader(CUSTOM_HEADER, randomHeaderValue), new BasicHeader(IRRELEVANT_HEADER, randomHeaderValue));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    List<RequestAndHeaders> searchRequests = getRequests(SearchRequest.class);
    assertThat(searchRequests, hasSize(greaterThan(0)));
    for (RequestAndHeaders requestAndHeaders : searchRequests) {
        assertThat(requestAndHeaders.headers.containsKey(CUSTOM_HEADER), is(true));
        // was not specified, thus is not included
        assertThat(requestAndHeaders.headers.containsKey(IRRELEVANT_HEADER), is(false));
    }
}
项目:elasticsearch_my    文件:RandomSearchRequestGenerator.java   
/**
 * Build a random search request.
 *
 * @param randomSearchSourceBuilder builds a random {@link SearchSourceBuilder}. You can use
 *        {@link #randomSearchSourceBuilder(Supplier, Supplier, Supplier, Supplier, Supplier)}.
 */
public static SearchRequest randomSearchRequest(Supplier<SearchSourceBuilder> randomSearchSourceBuilder) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    if (randomBoolean()) {
        searchRequest.indices(generateRandomStringArray(10, 10, false, false));
    }
    if (randomBoolean()) {
        searchRequest.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()));
    }
    if (randomBoolean()) {
        searchRequest.types(generateRandomStringArray(10, 10, false, false));
    }
    if (randomBoolean()) {
        searchRequest.preference(randomAsciiOfLengthBetween(3, 10));
    }
    if (randomBoolean()) {
        searchRequest.requestCache(randomBoolean());
    }
    if (randomBoolean()) {
        searchRequest.routing(randomAsciiOfLengthBetween(3, 10));
    }
    if (randomBoolean()) {
        searchRequest.scroll(randomPositiveTimeValue());
    }
    if (randomBoolean()) {
        searchRequest.searchType(randomFrom(SearchType.values()));
    }
    if (randomBoolean()) {
        searchRequest.source(randomSearchSourceBuilder.get());
    }
    return searchRequest;
}
项目:Elasticsearch    文件:RestPutWarmerAction.java   
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    PutWarmerRequest putWarmerRequest = new PutWarmerRequest(request.param("name"));
    SearchRequest searchRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")))
            .types(Strings.splitStringByCommaToArray(request.param("type")))
            .requestCache(request.paramAsBoolean("request_cache", null))
            .source(request.content());
    searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions()));
    putWarmerRequest.searchRequest(searchRequest);
    putWarmerRequest.timeout(request.paramAsTime("timeout", putWarmerRequest.timeout()));
    putWarmerRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putWarmerRequest.masterNodeTimeout()));
    client.admin().indices().putWarmer(putWarmerRequest, new AcknowledgedRestListener<PutWarmerResponse>(channel));
}
项目:elasticsearch_my    文件:ShardSearchLocalRequest.java   
ShardSearchLocalRequest(SearchRequest searchRequest, ShardId shardId, int numberOfShards,
                        AliasFilter aliasFilter, float indexBoost, long nowInMillis) {
    this(shardId, numberOfShards, searchRequest.searchType(),
            searchRequest.source(), searchRequest.types(), searchRequest.requestCache(), aliasFilter, indexBoost);
    this.scroll = searchRequest.scroll();
    this.nowInMillis = nowInMillis;
}