![](/img/trans.png)
[英]How to get nested documents by field value from other documents (ElasticSearch)?
[英]Elasticsearch Get the values of a field from all the documents
我正在嘗試從所有文檔中獲取字段的值。 我在沒有設置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();
如何從索引中的所有文檔中返回字段的所有值列表?
謝謝。
您無需通過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.