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

时间:2015-12-25 14:24:02

标签: java elasticsearch elasticsearch-plugin

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

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

如何从索引中的所有文档中返回字段的所有值列表?

谢谢。

1 个答案:

答案 0 :(得分:2)

您无需按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
}
相关问题