user595014
user595014

Reputation: 124

Grouping by query in solrj

Enviornment- solr-8.9.0

curl -G http://localhost:8983/solr/testColl/select?indent=true --data-urlencode "q=*:*" --data-urlencode "group=true" --data-urlencode "group.query=UnivesityName:'University~' AND UnivesityName:'Toronto~' --data-urlencode "group.query=UnivesityName:'University~' AND UnivesityName:'british~' AND UnivesityName:'columbia~'" --data-urlencode "group.limit=30"

The following code is solrj implementation of the above curl query

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;

    //Preparing the Solr query   
      SolrQuery query = new SolrQuery();

      query.add(CommonParams.Q, "*:*");
      query.set("group", "true");
      query.set("group.limit", "30");
      
      query.set("group.query", "UnivesityName:'University~' AND UnivesityName:'Toronto~'");

How can I set multiple group.query (1."UnivesityName:'University~' AND UnivesityName:'Toronto~'" 2.UnivesityName:'University~' AND UnivesityName:'british~' AND UnivesityName:'columbia~'") in solrj API in order to create multiple groups, as described in curl query?

Upvotes: 1

Views: 320

Answers (1)

cdrini
cdrini

Reputation: 1096

According to the docs, query.set overrides the prior value. Try using query.add with the same arguments, ie

query.add("group.query", "UnivesityName:'University~' AND UnivesityName:'Toronto~'");
query.add("group.query", "UnivesityName:'University~' AND UnivesityName:'british~' AND UnivesityName:'columbia~'");

The docs also say set takes an array, so you might be able to do this. This would be preferable, since it's a little less error prone since you can't forget that you added something.

query.set("group.query", [
  "UnivesityName:'University~' AND UnivesityName:'Toronto~'",
  "UnivesityName:'University~' AND UnivesityName:'british~' AND UnivesityName:'columbia~'"
]);

Note I haven't tested these.

Docs: https://solr.apache.org/docs/8_11_0/solr-solrj/org/apache/solr/common/params/ModifiableSolrParams.html (Note SolrQuery inherits from ModifiableSolrParams, which is what has the add/set methods)

Upvotes: 2

Related Questions