[英]Elasticsearch Get the values of a field from all the documents
I'm trying to get the values of a field from all the documents. 我正在尝试从所有文档中获取字段的值。 I tried the following without setting the id, in hopes that it would returns all the results, however, it objected and gave an error that I had to set the ID, which resulted in returning the value of just one field that has that id that I had specified:
我在没有设置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();
How can I return a list of all the values of a field from all the documents I have in an index? 如何从索引中的所有文档中返回字段的所有值列表?
Thank you. 谢谢。
Instead of getting a single document by id, you need to perform a search for all documents: 您无需通过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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.