简体   繁体   中英

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?

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:

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

And for passing multiple IDs, you can pass the Array of ids to the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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