简体   繁体   English

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

[英]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.

相关问题 如何从其他文档(ElasticSearch)中按字段值获取嵌套文档? - How to get nested documents by field value from other documents (ElasticSearch)? java 获取 elasticsearch 中某个字段的所有值 - java get all values for a field in elasticsearch Firebase,从所有文档中获取arraylist字段 - Firebase, get an arraylist field from all documents Elasticsearch,如何获取字段的所有唯一值和总唯一值的计数? - Elasticsearch, how to get all unique values of a field and count of total unique values? ElasticSearch 将所有字段值返回为 null - ElasticSearch returning all Field values as null 从Elasticsearch获取必填字段 - Get Required Field From Elasticsearch 如何使用Mongodb中的聚合和相应的java代码获取具有特定字段最小值的所有文档 - how to get all the documents having minimum values of a particular field using aggregation in Mongodb with corresponding java code Elasticsearch-Java RestHighLevelClient-如何使用滚动API获取所有文档 - Elasticsearch - Java RestHighLevelClient - how to get all documents using scroll api Elasticsearch获取字段的值-Java API - Elasticsearch Get the values of a field - java API ElasticSearch 按字段将文档聚合到数组中 - ElasticSearch aggregate documents by field into array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM