How can I perform grouping on Solr search results in Spring?

时间:2018-02-01 18:27:24

标签: java spring solr

The official Spring documentation provides the following example (slightly simplified here) for grouping results from a Solr query:

Field field = new SimpleField("popularity");
Query query = new SimpleQuery("inStock:true");

SimpleQuery groupQuery = new SimpleQuery(new SimpleStringCriteria("*:*"));
GroupOptions groupOptions = new GroupOptions()
    .addGroupByField(field)
    .addGroupByQuery(query);
groupQuery.setGroupOptions(groupOptions);

GroupPage<Product> page = solrTemplate.queryForGroupPage("collection-1", query, Product.class);

However, when I try this, it ignores the search conditions (inStock:true) and just performs the grouping on all results ("*:*")

1 个答案:

答案 0 :(得分:0)

There are several things about this example I had to modify to get it working. First of all, as you may have noticed, the argument query should actually be groupQuery in the queryForGroupPage call. I also removed the Query and put the actual search conditions in groupQuery. The working version is as follows, where groupByField is a SimpleField:

SimpleQuery groupQuery = new SimpleQuery(conditions);

GroupOptions groupOptions = new GroupOptions()
        .addGroupByField(groupByField);

groupQuery.setGroupOptions(groupOptions);

return solrTemplate.queryForGroupPage(groupQuery, YourObjectHere.class);