使用数据导入处理程序连接来自不同列的值

问题描述

我有一个 Django 应用程序,它连接到 Solr 实例进行搜索。如何将名字和姓氏放在同一个 tune_author Solr 字段中(见下文)?

    <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>

现在,我在结果中将名字和姓氏作为单独的字段,而 tune_author 为空。我怎样才能得到一个 tune_author Solr 字段,里面填满了名字 + 姓氏?

编辑

好吧,我认为我对 sql 查询的理解还有一个更基本的问题。

我的情况是:

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

copy_table:
id,copy_title,source_id ...

tune_table:
id,tune_title,copy_id ...

所以,我想要一首曲子的作者全名。一首曲子属于一个副本。副本属于源。最后,一个来源有作者的 ID(并且 author_table 有作者的详细信息,我想从中重建全名)。我希望这能让事情更清楚。

解决方法

可以直接用SQL解决:

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

它会直接在您的 SQL 行中为您提供串联条目。在这种情况下,我认为您不需要字段条目,但如有必要:

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

您还可以在实体上使用 TemplateTransformer 来使用字符串模板:

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