【问题标题】:Elasticsearch Get the values of a field from all the documentsElasticsearch 从所有文档中获取某个字段的值
【发布时间】:2016-03-31 12:57:13
【问题描述】:

我正在尝试从所有文档中获取字段的值。我在没有设置 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();

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

谢谢。

【问题讨论】:

    标签: java elasticsearch elasticsearch-plugin


    【解决方案1】:

    您需要搜索所有文档,而不是通过 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
    }
    

    【讨论】:

    • 谢谢您,先生! :) 只需对此进行一些修复,它就奏效了。因为我使用的是 ES 2.1.0,这就是我设置响应的最终代码的样子:SearchResponse response = NodeClient().prepareSearch("index-name").setTypes("user").setSource("id").setFrom(0).setSize(1000).execute().actionGet();
    • 很好,setTypes 而不是 setType,我已经修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多