【问题标题】:Jest mapping with getSourceAsObjectList()使用 getSourceAsObjectList() 的 Jest 映射
【发布时间】:2016-04-20 02:43:42
【问题描述】:

我尝试将弹性数据映射到列表。由于不推荐使用 getSourceAsObjectList() 方法,因此我编写了这样的代码。

class activityObj {
    private String name;
    private String oid;
    public String getName()
    {
        return name;
    }
    public void setName( String name)
    {
        this.name = name;
    }
    public String getOid()
    {
        return oid;
    }
    public void setOid( String oid)
    {
        this.oid = oid;
    }
}

List< Hit<activityObj, Void>> hits = result.getHits(activityObj.class);
List< activityObj> list = new ArrayList<activityObj>();

for (SearchResult.Hit<activityObj, Void> hit: hits) {
    activityObj objectSource = hit.source; // null point error
    list.add(objectSource);
    logger.info( "hits : " + objectSource.getName());
}

它不像我预期的那样工作并且看起来很乱(它使用两个列表值来映射结果)。我做错了吗?

【问题讨论】:

    标签: elasticsearch elasticsearch-jest


    【解决方案1】:

    Michael 的回答帮助了我,并且是一个很好的方法。这是我想出的,包括我用来进行查询和获得结果的所有部分。

    在我的例子中,我想获取从结果中每个文档的来源收集的用户 ID 列表。

    class DocSource {
        private Integer userId;
    
        public Integer getUserId() {
            return userId;
        }
    }
    
    String query = "{\n"
            + "    \"_source\": [\n"
            + "        \"userId\"\n"
            + "    ],\n"
            + "    \"query\": {\n"
            + "           <some query config here>"
            + "        }\n"
            + "   }\n"
            + "}";
    
    Search search = new Search.Builder(query)
        .addIndex("my-index")
        .build();
    
    SearchResult result = jestClient.execute(search);
    List<SearchResult.Hit<DocSource, Void>> hits = result.getHits(DocSource.class);
    
    List<Integer> listOfIds = hits.stream()
        .map(h -> h.source.getUserId())
        .collect(Collectors.toList());
    

    【讨论】:

      【解决方案2】:

      你有没有尝试过这样的事情:

      List< activityObj> list = hits.stream().map(h -> h.source).collect(Collectors.toList());
      

      【讨论】:

        猜你喜欢
        • 2016-08-13
        • 2019-09-29
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        相关资源
        最近更新 更多