0

我正在使用 Elasticcsearch 7.3。我只想从我的索引中使用 JAVA Api 获取所有文档的两条记录。我正在使用以下代码,但它返回空对象。

        RestHighLevelClient client;

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.fetchSource("recipe_ID,recipe_url", null);
        sourceBuilder.from(0);
        SearchRequest searchRequest = new SearchRequest("recipes");
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHit searchHit = searchResponse.getHits().getAt(0);

        String resultString = searchHit.getSourceAsString();

        return resultString;

我只需要在结果中包含两个字段recipe_IDrecipe_url

4

1 回答 1

1

您走在正确的道路上,尽管源过滤要求您在数组中指定字段,如下所示:

    String[] includeFields = new String[] {"recipe_ID", "recipe_url"};
    sourceBuilder.fetchSource(includeFields, null);
于 2020-05-29T03:21:28.487 回答