9

I have a multivalue field called category(which is also a store field) in which i need to add only distinct values

<field name="category">value1</field>
<field name="category">value2</field>

If I do a update as follows
<add>
<doc>
<field name="id">E02</field>
<field name="category" update="add">value2</field>
</doc>
</add>

I get the value2 stored twice
<field name="category">value1</field>
<field name="category">value2</field>
<field name="category">value2</field>

I need to store/update only disctinct values in category fields which is a multivalue fields. How to do this solr?

Thanks in advance, Jagadesh.

Jagadesh
  • 6,489
  • 8
  • 29
  • 30

2 Answers2

6

This functionality has been added for Solr 7.3.

You can use the add-distinct atomic update action, instead of add

See the Jira issue (SOLR-11267) and the documentation in the Reference Guide:

add-distinct

Adds the specified values to a multiValued field, only if not already present. May be specified as a single value, or as a list.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
2

One can "set" instead of "add", to recreate a stored field in partial document updates. So if it is the case where you have all the field values,just stick them in a Set and then repopulate the field. You have all the data to recreate it because of stored field requirement.

Ion Cojocaru
  • 2,583
  • 15
  • 16
  • If i try this, yes `value2`is saved once but the old values `value1` is lost. – Jagadesh Apr 09 '13 at 12:50
  • you need to set all previous + yours if it's distinct – Ion Cojocaru Apr 09 '13 at 12:52
  • Jus a clarification. What if the field is not a stored field? – Jagadesh Apr 09 '13 at 12:53
  • partial updates are applied only to Solr >= 4.0 and stored field is a hard requirement. There is no magic there, Solr needs it to reconstruct(delete and re-indexe) the underlying Lucene document, also increasing the _version_ number. In older versions you had to do it manually. Read more http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/ – Ion Cojocaru Apr 09 '13 at 13:01