简体   繁体   English

查询以获取弹性搜索中 id 列表的结果

[英]query to getting result for a list of ids in elastic search

I have a Query for getting lastSeenTime only for one user but what I need is to get a map of ids by their last seen for a list of users in elastic search can somebody help me with converting this query to find last seen of a list of users ssoIds?我有一个只为一个用户获取 lastSeenTime 的查询,但我需要的是通过他们在弹性搜索中最后一次看到的用户列表来获取 id 的地图,有人可以帮助我转换此查询以找到最后一次看到的列表用户 ssoIds?

static Map<String, Object> getLastSeen(String ssoId) {
    SearchResponse response = transportClient.prepareSearch(ChatSettings.ELASTIC_LAST_SEEN_INDEX_NAME)
            .setTypes(ChatSettings.ELASTIC_DB_NAME)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(QueryBuilders.idsQuery().addIds(ssoId))         
            .setFrom(0).setSize(1).setExplain(true)
            .get();
    checkResponse(response);
    Map<String, Object> result = null;
    if (response.getHits().getTotalHits() > 0) {
        result = response.getHits().getAt(0).getSource();
    }
    return result;
}

actually I want something like this其实我想要这样的东西

static Map<String, Object> getLastSeens(List<String> ssoIdList)
{
  //elsticQuery
}

You can use fetch in your elastic query to return only selected fields:您可以在弹性查询中使用 fetch 仅返回选定的字段:

.setFetchSource(new String[]{"field1","field2}, null)

And for passing multiple IDs, you can pass the Array of ids to the idsQuery()对于传递多个 ID,您可以将 id数组传递给idsQuery()

So, in your case it will become:所以,在你的情况下,它将变成:

SearchResponse response = transportClient
        //.prepareSearch(ChatSettings.ELASTIC_LAST_SEEN_INDEX_NAME) // you might need to pass the columns here
        .setTypes(ChatSettings.ELASTIC_DB_NAME)
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setFetchSource(new String[]{"id","lastSeenTime"}, null) // or you can pass the columns here
        .setQuery(QueryBuilders.idsQuery().addIds(ssoIds)) // where ssoIds is a array of Ids        
        .setFrom(0).setSize(1).setExplain(true)
        .get();

post this, rest of the code will work as it is.发布此内容,其余代码将按原样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM