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

时间:2019-04-04 05:29:50

标签: spring-boot elasticsearch java-11

我正在尝试将我的弹性搜索(6.6.1)和spring boot(2.1.3)应用程序从Java 8迁移到Java 11。 以前,我使用高级Java Rest客户端创建和搜索索引。 由于在对高级Rest Client API进行模块化方面存在问题(https://github.com/elastic/elasticsearch/issues/38299,因此我尝试使用低级Rest Client,但无法获得任何搜索结果。

请参阅一些代码片段-

使用高级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);

然后,我使用低级别的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的第二个参数)时犯了一些错误,但我找不到更多细节。

有人可以请教吗

1 个答案:

答案 0 :(得分:0)

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

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

现在我得到了预期的结果