How do I create a composite key field in Apache Solr?

I have Apache Solr 3.5 which has SchemaXml like this:

<field name="appid" type="string" indexed="true" stored="true" required="true"/>
<field name="docid" type="string" indexed="true" stored="true" required="true"/>


I will need a field that concatenates them together and uses that like <uniqueKey>

. Nothing seems to have been inlined other than creating a multivalued field id

and using it <copyField>

, but it seems that uniqueKey requires an unambiguous field.

The only reason I need this is to allow clients to blindly trigger calls <add>

and display Solr if it's an add-on or update. So I don't care what the ID looks like.

I'm assuming I'll have to write my own parser or tokenizer? I'm just starting to learn Solr so I'm not 100% sure what I really need and would appreciate any hints on what I need to implement.


source to share

1 answer

I personally would give this load to users as it is quite easy for them to add a field for each document.

Otherwise, you will have to write a few lines of code I guess. You can write your own UpdateRequestProcessorFactory

that automatically adds a new field to each input document based on the value of other existing fields. You can use a separator and store it as a single value. On UpdateRequestProcessor

you should override the method processAdd

as follows:

public void processAdd(AddUpdateCommand cmd) throws IOException {
    SolrInputDocument doc = cmd.getSolrInputDocument();
    String appid = (String)doc.getFieldValue( "appid" );
    String docid = (String)doc.getFieldValue( "docid" );
    doc.addField("uniqueid", appid + "-" + docid);    
    // pass it up the chain


Then you have to add yours UpdateProcessor

to your configured updateRequestProcessorChain

as the first processor in the chain ( solrconfig.xml


<updateRequestProcessorChain name="mychain" >
    <processor class="my.package.MyUpdateRequestProcessorFactory" />
    <processor class="solr.RunUpdateProcessorFactory" />
    <processor class="solr.LogUpdateProcessorFactory" />


Hope it works, I haven't tried it. I've already done something like this, but not with uniqueKey or required fields, which is the only problem you could find. But I think that if you put updateProcessor at the beginning of the chain, it should work.



All Articles