0

I have a document in solr as

<entity name="Category" dataSource="ds1" pk="CategoryId"
    query="SELECT CategoryId, Description, ImageUrl FROM Category">
    <field column="CategoryId" name="CategoryId" />
    <entity name="PackCategory" pk="PackId, CategoryId"
     query="SELECT PackId FROM PackCategory WHERE CategoryId = ${Category.CategoryId}" >
     <entity name="Pack" pk="PackId"
      query="SELECT PackId, IsActive FROM Pack WHERE PackId = ${PackCategory.PackId}" >
      <field column="IsActive" name="IsActive" />
      <entity name="PartnerPackTrans" pk="PackId, PartnerId" transformer="TemplateTransformer">
       query="SELECT PartnerId FROM PartnerPackTrans WHERE PackId = ${Pack.PackId}" >
           <field column="PartnerId" name="PartnerId" />
      </entity>
</entity>
     </entity>
    </entity>
   </entity>

My unique id is a combination of (Cateogry.CategoryId, Pack.PackId, PartnerPackTrans.PartnerId). How can I define this is solr in schema.xml in unique field

Update After going through various post I added this my solr-config file

 <updateRequestProcessorChain name="id">
  <processor class="solr.CloneFieldUpdateProcessorFactory">
    <str name="source">CategoryId</str>
    <str name="source">PartnerId</str>
    <str name="dest">id</str>
  </processor>
  <processor class="solr.ConcatFieldUpdateProcessorFactory">
    <str name="fieldName">id</str>
    <str name="delimiter">-</str>
  </processor>
  <processor class="solr.LogUpdateProcessorFactory" />
  <processor class="solr.RunUpdateProcessorFactory" />
</updateRequestProcessorChain>

But I get this error in solr Document is missing mandatory uniqueKey field: id

i am refering this post Solr Composite Unique key from existing fields in schema

I am using solr version 5.3

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
Nipun
  • 4,119
  • 5
  • 47
  • 83

1 Answers1

1

In Solr, you can add a unique key:

The declaration can be used to inform Solr that there is a field in your index which should be unique for all documents. If a document is added that contains the same value for this field as an existing document, the old document will be deleted.

So, in your case, if your field is called unique, you have to add:

<uniqueKey>unique</uniqueKey>

UPDATE

To answer to your comment, in your schema.xml, you can create a new field:

<field name="unique" type="text_general" indexed="true" stored="true"  multiValued="true" />

After that, you can copy the fields that you want in your new field:

<copyField source="Cateogry.CategoryId" dest="unique"/>
<copyField source="Pack.PackId" dest="unique"/>
<copyField source="PartnerPackTrans.PartnerId" dest="unique"/>
alexf
  • 1,303
  • 9
  • 20
  • how can I add a combination of columns in this? My columns are coming from different entities. – Nipun Oct 13 '15 at 10:39
  • I tried this http://stackoverflow.com/questions/17806821/solr-composite-unique-key-from-existing-fields-in-schema but it gives me error "your document do not have a unique field id, where to add id in the document – Nipun Oct 13 '15 at 11:21
  • 1
    it's not recommended to use `text_general` as unique key type (https://cwiki.apache.org/confluence/display/SOLR/UniqueKey) – tObi Jul 27 '20 at 18:01
  • 2
    tObi is right, you shouldn't use `text_general`, for the reasons he linked to in that link. However I couldn't figure out what type would be wise. The link says `It is strongly advised to use one of the un-analyzed types (e.g. string)`, but which type is for string? I finally found https://solr.apache.org/guide/6_6/field-types-included-with-solr.html#field-types-included-with-solr, which shows it as `StrField`, class `solr.StrField`. Hope that helps someone. – Tyler Collier Feb 01 '22 at 21:12