Elasticsearch 从所有文档中获取某个字段的值

Elasticsearch Get the values of a field from all the documents

我正在尝试从所有文档中获取某个字段的值。我在不设置 ID 的情况下尝试了以下操作,希望它会 return 所有结果,但是,它反对并给出了一个错误,我必须设置 ID,这导致 returning只有一个具有我指定的 ID 的字段的值:

GetResponse response = NodeClient().prepareGet().setIndex("index-name")
               .setType("user").setId("1")
               .setFields("id").execute().actionGet();
GetField field = response.getField("id");
result = field.getValues();

如何return 列出索引中所有文档中某个字段的所有值?

谢谢。

您需要搜索所有文档,而不是通过 id 获取单个文档:

SearchResponse response = NodeClient().prepareSearch("index-name")
    .setTypes("user")
    .setSource("id")             <---- list the fields you want to retrieve
    .setFrom(0)
    .setSize(1000)               <---- increase this is you have more documents
    .execute().actionGet();

for (SearchHit hit : response.getHits()) { 
    String id = hit.getSource().get("id");
    // do something with the id value
}