使用 Java 低级别休息客户端的弹性搜索

Elastic search using Java low level rest client

我正在尝试将我的弹性搜索 (6.6.1)、spring 引导 (2.1.3) 应用程序从 java 8 移动到 java 11。 以前,我使用 high-level java rest client 创建和搜索索引。 由于在模块化高级休息客户端 api 时存在问题 (https://github.com/elastic/elasticsearch/issues/38299),我正在尝试使用低级别休息客户端,但我无法获得任何搜索结果。

请查看部分代码片段 -

使用高级 rest 客户端创建的搜索索引

           IndexRequest indexRequest = new IndexRequest("legal3", "scee");
          IndexResponse indexResponse =  
          highlevelclient.index(indexRequest, RequestOptions.DEFAULT);

用于使用高级 rest 客户端进行搜索的查询

    this.client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9200)));
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.multiMatchQuery(text, “field1”));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(sourceBuilder);
    SearchResponse response1 = highlevelclient.search(searchRequest, RequestOptions.DEFAULT);

然后我使用低级休息客户端搜索相同的索引(使用高级休息客户端创建的索引)

        this.client = RestClient.builder(
                new HttpHost("localhost", 9200)).build();
        String query = "{\"query\":{\"match\":{\"field1\":\"" + text + "\"}}}";
        Response response = lowlevlelclient.performRequest("GET", "legal3", Collections.emptyMap(), new StringEntity(query, ContentType.APPLICATION_JSON));

但是只返回了headers,没有真正的数据。

{"legal3":{"aliases":{},"mappings":{"scee":{"properties”:”field1”:{“type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}},"settings":{"index":{"number_of_shards":"5","blocks":{"read_only_allow_delete":"true"},"provided_name":"legal3","creation_date":"1552209921359","number_of_replicas":"1","uuid":"Y_GyoagoTIezgztuUYrlBQ","version":{"created":"6060199"}}}}}

我觉得我在设置终点(performRequest 的第二个参数)时犯了一些错误,但我找不到关于这个的更多细节。

有人可以指点一下吗。

我终于找到了解决办法。正如预期的那样,问题出在端点上(performRequest 的第二个参数)

Response response = lowlevelclient.performRequest("GET", "/_search", Collections.emptyMap(), new StringEntity(query, ContentType.APPLICATION_JSON));

现在我得到了预期的结果