如何通过JSON将查询设置为Elasticsearch SearchRequest?

时间:2018-01-23 10:09:17

标签: elasticsearch elasticsearch-java-api

Elasticsearch :6.1.2

我有一个通过JSON的输入查询,并希望使用high level Java API使用该查询数据构建搜索请求。

String jsonQuery = "..."
SearchRequest searchRequest = new SearchRequest()
SearchSourceBuilder builder = ?
searchRequest.source(builder);

我尝试通过以下方式构建构建器:

SearchSourceBuilder.fromXContent(XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, query));

但是产生了:

  

引起:org.elasticsearch.ElasticsearchException:namedObject is   这个解析器不支持   org.elasticsearch.common.xcontent.NamedXContentRegistry.parseNamedObject(NamedXContentRegistry.java:129)   〜[elasticsearch-6.1.2.jar:6.1.2] at   org.elasticsearch.common.xcontent.support.AbstractXContentParser.namedObject(AbstractXContentParser.java:402)   〜[elasticsearch-6.1.2.jar:6.1.2] at   org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder(AbstractQueryBuilder.java:313)   〜[elasticsearch-6.1.2.jar:6.1.2] at   org.elasticsearch.search.builder.SearchSourceBuilder.parseXContent(SearchSourceBuilder.java:1003)   〜[elasticsearch-6.1.2.jar:6.1.2] at   org.elasticsearch.search.builder.SearchSourceBuilder.fromXContent(SearchSourceBuilder.java:115)   〜[elasticsearch-6.1.2.jar:6.1.2]

3 个答案:

答案 0 :(得分:4)

我现在以这种方式生成SearchSourceBuilder:

mkdir ~/Downloads/gcc-4.9-deb && cd ~/Downloads/gcc-4.9-deb

wget http://launchpadlibrarian.net/247707088/libmpfr4_3.1.4-1_amd64.deb
wget http://launchpadlibrarian.net/253728424/libasan1_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728426/libgcc-4.9-dev_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728314/gcc-4.9-base_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728399/cpp-4.9_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728404/gcc-4.9_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728432/libstdc++-4.9-dev_4.9.3-13ubuntu2_amd64.deb
wget http://launchpadlibrarian.net/253728401/g++-4.9_4.9.3-13ubuntu2_amd64.deb

sudo dpkg -i gcc-4.9-base_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i libmpfr4_3.1.4-1_amd64.deb
sudo dpkg -i libasan1_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i libgcc-4.9-dev_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i cpp-4.9_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i gcc-4.9_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i libstdc++-4.9-dev_4.9.3-13ubuntu2_amd64.deb
sudo dpkg -i g++-4.9_4.9.3-13ubuntu2_amd64.deb

答案 1 :(得分:1)

我也面临着同样的问题。我们已经从ES 1.4.4更新到ES 6.4.2,并且我使用了ES提供的Java High Level Rest Client在ES中进行创建/更新/删除操作。我找不到一种简单的方法,可以使用高级Rest Client将上一个ES版本中使用的查询JSON转换为适当的searchRequest。以下是我最终使用的内容。

import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.Request;

public static Response executeGetRequest(RestHighLevelClient client,
            String api, String body) throws Exception {

            Request request = new Request("GET", api);
            if(body != null && !body.isEmpty()) {
                request.setJsonEntity(body);
            }            
            Response response = client.getLowLevelClient()
                    .performRequest(request);
            if (response == null) {
                throw new Exception(
                        "Get request have failed");
            }
            return response;        
    }

答案 2 :(得分:0)

在createParser方法中,将第一个参数NamedXContentRegistry.EMPTY替换为JsonXContent.jsonXContent