Search code examples
solr

How to count multi-valued Field in solr


I Want to count multi-valued field in SOLR.

I have two multi-valued fields store_id and filter_id and i want to count these field value like

store_id = {0,3,7} count_store_id = 3

filter_id = {12,13,20,22,59,61,62,145} count_filter_id = 8

enter image description here

and is that possible when store_id is update then count_store_id also update in solr by default

@@ Ashraful Islam - As you told me i'll change it but there is nothing going happen here i attach image find it.

enter image description here


Solution

  • Yes as suggested by Alexandre Rafalovitch, by using defining custom UpdaterequestProcessor you can get the count value of multivalued field.

    add below lines in your solrconfig.xml

    <updateRequestProcessorChain name="multivaluecountnum" default="true">
       <processor class="solr.CloneFieldUpdateProcessorFactory">
         <str name="source">store_id</str>
         <str name="dest">store_id_count</str>
       </processor>
    <processor class="solr.CloneFieldUpdateProcessorFactory">
         <str name="source">filter_id</str>
         <str name="dest">filter_id_count</str>
       </processor>
       <processor class="solr.CountFieldValuesUpdateProcessorFactory">
         <str name="fieldName">store_id_count</str>
       </processor>
     <processor class="solr.CountFieldValuesUpdateProcessorFactory">
         <str name="fieldName">filter_id_count</str>
       </processor>
       <processor class="solr.DefaultValueUpdateProcessorFactory">
         <str name="fieldName">store_id_count</str>
         <int name="value">0</int>
       </processor>
    <processor class="solr.DefaultValueUpdateProcessorFactory">
         <str name="fieldName">filter_id_count</str>
         <int name="value">0</int>
       </processor>
    <processor class="solr.LogUpdateProcessorFactory" />
      <processor class="solr.RunUpdateProcessorFactory" />
     </updateRequestProcessorChain>
    

    Do not forget to add RunUpdateProcessorFactory at the end of any chains you define in solrconfig.xml

    Add store_id_count and filter_id_count fields in schema file

       <field name="store_id_count" type="int" stored="true"/>
       <field name="filter_id_count" type="int" stored="true"/>
    

    Reindex docs and query, you will see two new fields store_id_count and filter_id_count in result.

    Hope this Helps, Vinod.