Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr : How can I group on two different fields?

Tags:

solr

My schema is like :

product_id  
category_id

A category contains products.
In solr 3.6, I group results on category_id and it works well.

I just added a new field:

group_id

A group contains products that vary on size or color.
Example: shoes in blue, red and yellow are 3 differents products and have the same group_id.

Additionally to the result grouping on field category_id, I would like to have in my results only one product for a group_id, assuming group_id can be null (for products that aren't part of a group).

To follow the example of the shoes, it means that for the request "shoe", only one of the 3 products should be in results.

I thought to do a second result grouping on group_id, but I doesn't seem possible to do that way.

Any idea?

EDIT : For now, i process the results in php to delete documents that have a group_id that is already in the results. I leave this subject open, in case someone finds how to group on 2 fields

like image 605
Elodie Avatar asked Apr 02 '13 07:04

Elodie


2 Answers

If your aim is to get grouping counts based on multiple "group by" fields, you can use pivot faceting to achieve this.

&facet.pivot=category_id,group_id

Solr will give you back a hierarchy of grouped result counts, following the page of search results, under the facet_pivot element.

http://wiki.apache.org/solr/SimpleFacetParameters?highlight=%28pivot%29#Pivot_.28ie_Decision_Tree.29_Faceting

like image 119
RMorrisey Avatar answered Oct 21 '22 14:10

RMorrisey


It is not possible to group by query on two fields. If you need count then you can use facet.field(For single field) or facet.pivot(For multiple field). It is not actually group but you can get count of that group for multiple field.

Example Output:

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <lst name="responseHeader">
        <bool name="zkConnected">true</bool>
        <int name="status">0</int>
        <int name="QTime">306</int>
    </lst>
    <result name="response" numFound="667" start="0" maxScore="0.70710677">
        <doc>
            <int name="idField">7393</int>
            <int name="field_one">12</int>
        </doc>
    </result>
    <lst name="facet_counts">
        <lst name="facet_queries"/>
        <lst name="facet_fields"/>
        <lst name="facet_ranges"/>
        <lst name="facet_intervals"/>
        <lst name="facet_heatmaps"/>
        <lst name="facet_pivot">
            <arr name="field_one,field_two">
                <lst>
                    <str name="field">field_one</str>
                    <int name="value">3</int>
                    <int name="count">562</int>
                    <arr name="pivot">
                        <lst>
                            <str name="field">field_two</str>
                            <bool name="value">true</bool>
                            <int name="count">347</int>
                        </lst>
                        <lst>
                            <str name="field">field_two</str>
                            <bool name="value">false</bool>
                            <int name="count">215</int>
                        </lst>
                    </arr>
                </lst>
                <lst>
                    <str name="field">field_one</str>
                    <int name="value">12</int>
                    <int name="count">105</int>
                    <arr name="pivot">
                        <lst>
                            <str name="field">field_two</str>
                            <bool name="value">true</bool>
                            <int name="count">97</int>
                        </lst>
                        <lst>
                            <str name="field">field_two</str>
                            <bool name="value">false</bool>
                            <int name="count">8</int>
                        </lst>
                    </arr>
                </lst>
            </arr>
        </lst>
    </lst>
</response>

Example Query :

http://192.168.100.145:7983/solr/<collection>/select?facet.pivot=field_one,field_two&facet=on&fl=idField,field_one&indent=on&q=field_one:(3%2012)&rows=1&wt=xml
like image 20
Sunil Kanzar Avatar answered Oct 21 '22 13:10

Sunil Kanzar