Search code examples
mysqlsolrdataimporthandler

Concatenate values from different columns with Data Import Handler


I have a Django application which connects to a Solr instance for searching. How can I put the firstname and the surname in the same tune_author Solr field (see below)?

    <entity name="tune" query="select id,type,name,start_page,alternate_spellings,item_id from bassculture_tune">
      <field column="id" name="id"/>
      <field column="type" name="type"/>
      <field column="name" name="name"/>
      <entity name="tune_author" query="select id,firstname,surname from bassculture_author where id=${tune.item_id.source_id.author_id}">

        </entity>

Right now I get the firstname and surname as separate fields in my results, and tune_author is empty. How can I get a tune_author Solr field filled with firstname + surname?

EDIT

OK, I think there's also a more basic issue with my understanding of SQL querying.

My situation is:

author_table:
id, firstname, surname ...
    
source_table:
id, source_title, author_id ... 

copy_table:
id, copy_title, source_id ...

tune_table:
id, tune_title, copy_id ...

So, I want the author's full name of a tune. A tune belongs to a copy. A copy belongs to a source. A source, finally, has the author's ID (and the author_table has the author's details from which I want to reconstruct the full name). I hope this makes things clearer.


Solution

  • You can solve it directly in SQL:

    SELECT id, CONCAT(firstname, ' ', surname) AS tune_author FROM ...
    

    It'll give you the concatenated entry directly in your SQL row. I don't think you'll need a field entry in that case, but if necessary:

    <field column="tune_author" name="tune_author"/>
    

    You can also use a TemplateTransformer on your entity to use a string template:

    <entity name="tune_author" query="..." transformer="TemplateTransformer">
        <field column="tune_author" template="${tune_author.firstname} ${tune_author.surname}" />
    </entity>