使用HttpClient Post进行弹性搜索请求正文搜索

时间:2015-12-01 08:32:24

标签: java json elasticsearch httpclient

我在java中使用HttpClient进行elasticSearch,正如官方文档所说,有两种方法:使用简单的查询字符串作为参数,或者使用请求体。

当我使用Request Body Search时。如果使用curl,那将是:

curl -XGET 'http://192.168.11.131:9200/docmanager/user/_search' -d '{
    "query":{
        "term":{"userName":"account"}
    }
}

所以我创建了json字符串

{"query":{"term":{"userName":"account"}}}

并在Apache HttpClient中使用HttpPost(),代码如下:

    public List<Map<String, Object>> search(String index, String type,String filterString,
        QueryBuilder builder) {
    String searchUri="http://"+mMasterNodeUrl+":"+mMasterNodePort+"/"+index+"/"+type
    +"/_search";
    StringEntity filter=new StringEntity(filterString, "UTF-8");
    filter.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
    JsonNode rootJsonNode=sendHttpPost(searchUri, filter);  

此处filterString是我创建的json查询字符串

    private JsonNode sendHttpPost(String uri,StringEntity args){
    HttpPost httpPost=new HttpPost(uri);
    if(args!=null)
        httpPost.setEntity(args);
    HttpClientContext clientContext=createBasicAuthContext(mEsAdminName, mEsAdminPassowrd);
    CloseableHttpClient httpClient=HttpClientBuilder.create().build();
    CloseableHttpResponse httpResponse=null;
    try {
        httpResponse=httpClient.execute(mMasterNodeHttpHost, httpPost, clientContext);

有效。 但是,当我想做像https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-filter-context.html

这样的多条件搜索时
{"query":
    {"bool":
        {"must":[
            {"match":{"name":"test1"}},
            {"match":{"content":"some contents"}}],
         "filter":[
            {"range":{"date":{"to":"1448957966693"}}},
            {"range":{"level":{"from":"1","to":"2"}}}]
         }
     }
 }

我将上面的json字符串传递给方法List<Map<String, Object>> search(String index, String type,String filterString,QueryBuilder builder)

发生错误,来自httpResponse的错误是:

{"error":{"root_cause":[{"type":"parse_exception","reason":"Failed to derive xcontent"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"docmanager","node":"Gk5r4QaXQnar95GalR6anA","reason":{"type":"parse_exception","reason":"Failed to derive xcontent"}}]},"status":400}

似乎无法解析json字符串?但为什么它在第一个条件下工作?

更新

elasticsearch.log中的错误消息是:

[2015-12-01 21:53:33,643][DEBUG][action.search.type       ] [ezio] [docmanager][3], node[PZrESgD8TeSMUgTzJna12g], [P], v[32], s[STARTED], a[id=WBcMBH19SDG7cYpb6SCOUg]: Failed to execute [org.elasticsearch.action.search.SearchRequest@1dce9d35] lastShard [true]
RemoteTransportException[[ezio][192.168.123.6:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [_na_]]; nested: ElasticsearchParseException[Failed to derive xcontent];
Caused by: SearchParseException[failed to parse search source [_na_]]; nested: ElasticsearchParseException[Failed to derive xcontent];
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:860)
    at org.elasticsearch.search.SearchService.createContext(SearchService.java:663)
    at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:632)
    at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:374)
    at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:368)
    at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:365)
    at org.elasticsearch.shield.transport.ShieldServerTransportService$ProfileSecuredRequestHandler.messageReceived(ShieldServerTransportService.java:165)
    at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: ElasticsearchParseException[Failed to derive xcontent]
    at org.elasticsearch.common.xcontent.XContentFactory.xContent(XContentFactory.java:293)
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:829)
    ... 11 more

顺便说一句,json字符串是manully创建的,这有关系吗?

0 个答案:

没有答案