使用Java低级别rest客户端进行弹性搜索

问题描述 投票:1回答:1

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

请看一些代码 -

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

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

该查询用于使用高级休息客户端进行搜索

    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);

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

        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));

但它只返回标题,没有真正的数据。

{"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的第二个参数),但我找不到很多关于此的细节。

请有人请指教。

spring-boot elasticsearch java-11
1个回答
0
投票

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

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

现在我得到了预期的结果

© www.soinside.com 2019 - 2024. All rights reserved.