manu bhat
manu bhat

Reputation: 61

Java solrclient group query

I am building a chat like app and I am storing the chat conversation in Solr 6.5.1. I have created a message collection and every document corresponds to a single message exchanged between two users. A document has a fromId and a toId like below.

enter image description here

I want to group the messages by toId and show a count of how many messages per conversation with a user. I am able to test it using admin UI like so. enter image description here

But from my Java code using SolrJ I am unable to get the same result. Below is my code snippet

SolrQuery query = new SolrQuery();
query.set("wt", "json");
query.setQuery("fromId:1234");
query.set("group",true);
query.set("group.field", "toId");
try {
        GroupResponse gRes = client.query(query).getGroupResponse();
        List<GroupCommand> groupCommands = gRes.getValues();
        List<GroupResult> grs = new ArrayList<>();
        for(GroupCommand gc : groupCommands){
            GroupResult gr = new GroupResult();
            gr.setCount(gc.getMatches());
            gr.setGroupText(gc.getName());
            grs.add(gr);
        }
        return grs;
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
        return Collections.EMPTY_LIST;
    }

But this is not giving me both the groups , instead I am only getting one group with matched count as 6 . Please help

Upvotes: 0

Views: 781

Answers (1)

manu bhat
manu bhat

Reputation: 61

Answering my own question. Below is the change required to my code in order to get to the groups.

QueryResponse gRes = client.query(query);
        GroupResponse grpR = gRes.getGroupResponse();
        List<GroupCommand> groupCommands = gRes.getGroupResponse().getValues();
        List<GroupResult> grs = new ArrayList<>();
        for(GroupCommand gc : groupCommands){
            List<Group> groups = gc.getValues();
            for(Group group : groups){
                GroupResult gr = new GroupResult();

                gr.setGroupText(group.getGroupValue());
                gr.setCount(group.getResult().getNumFound());
                grs.add(gr);
            }
        }

Upvotes: 1

Related Questions