2

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

Garvit Joshi
  • 79
  • 1
  • 10

2 Answers2

2

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.

Vinod
  • 1,965
  • 1
  • 9
  • 18
  • Thanks Vinod, Now its working when i import data from mysql database. – Garvit Joshi Feb 08 '17 at 06:32
  • but there is one issue when i insert new document then it will not working. it is working only when i import data from data not when i insert a new document in data base. – Garvit Joshi Feb 08 '17 at 06:49
  • this process happens at solr side. for every doc during indexing into solr, one new field (count, in your case)is computed and added to doc. then updated doc with new field is indexed into Solr. – Vinod Feb 08 '17 at 06:56
  • Wow, very helpful answer, but I have one question: How can perform similar behavior with partial update of cloned multivalued field? I am performing an update of multivalued field (adding one new value to it) and it seems that in this case counter is set to 1 instead of old-counter-value +1. – ydrozhdzhal Nov 02 '17 at 17:56
1

You can do this with a custom UpdateRequestProcessor chain that uses CountFieldValuesUpdateProcessorFactory.

Alexandre Rafalovitch
  • 9,709
  • 1
  • 24
  • 27