You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by dn...@tutanota.com.INVALID on 2020/07/03 07:53:25 UTC

Changing Response for Group Query - Custom Request Handler

Dear Community, 

I am currently working on a Solr Custom Plugin, which - for a group query - adds both total matches and number of groups to the response and also keeps the response format as if it is not a group query. One additional requirement is that numFound should contain the number of groups instead of total matches. This whole thing is required since we have some limitations on the consumer side and want to keep the same response format.

If I use group.main=true, I get the results in "usual" format, but the group.ngroups=true does not effect anymore. So, I decided to create a custom request handler in order to get number of groups and create the response format which we need for the requester. This format is basically:

---{
//...
"response": {
    "numFound": 25,  // contains number of grouped results --> for pagination reasons
    "total": 401     // contains number of total results
    "start": 0,
    "docs": [
      {
        "id": "26207825"
      },
// ...
}

---

My first question would be: is this a good approach with a custom request handler, or is there a better/easier approach to achieve this.

I went ahead and (tried) to implement the custom request handler. From a group request (group.format=simple&&group.ngroups=true), I can get a "docList" (or docSlice) from the "grouped" part, but basically putting this docList to the response gives me the same result with group.main=true. Therefore I thought, I would create a new docSlice with mostly the content from the old docSlice and put this in the response. That works for start=0, but as soon as I start pagination and set start anything else than zero, I get an error. Because, even though the start is not zero, the docs[] holds skipped results documents to, but with document iterator I can only access a subset of it, so my new docSlice would only contain a subset of documents. Here is the method for creating a new docSlice:

---private DocSlice getGroupedDocList(SimpleOrderedMap grouped, int numGroups) {

    DocSlice docSlice = (DocSlice) grouped.get("doclist");
    int offset = docSlice.offset();
    int docSize = docSlice.size();
    int len = offset + docSize;

    boolean hasScore = docSlice.hasScores();

    int[] docs = new int[len];
    float[] scores = new float[len];

    DocIterator iterator = docSlice.iterator();
    int i = 0;
    while (i < len && iterator.hasNext()) {
        docs[i] = iterator.nextDoc();
        LOGGER.error("Doc added: " + docs[i]);
        if (hasScore) { scores[i] = iterator.score(); }
        i++;
    }
    long matches = numGroups;
    float maxScore = docSlice.maxScore();

    DocSlice newDocSlice = new DocSlice(offset, len, docs, scores, matches, maxScore);
    return newDocSlice;
}
---

I am kind of stuck at this point. Can someone maybe help me?

Best regards,
Deniz C.