Apache Solr:如何根据主实体的字段在子实体中执行查询

Apache Solr: How to make execution of a query in a sub-entity dependent on a field from the main entity

如何在依赖字段的子实体中执行查询 主要实体的价值?

正如您在下面的(简化的)数据配置中看到的那样,有一个实体 categories_lvl_0 包含一个昂贵的查询。我只想执行 此查询如果:searchobject.objecttype=115

我尝试使 categories_lvl_0 依赖于 if 语句:IF (${searchobject.objecttype}=115),我尝试过将查询包装在 BEGIN...END 中,但所有这些选项都会抛出错误:

Processing Document # 1 at org.apache.solr.handler.dataimport.DocBuilder.execute(DocBuilder.java:266) at org.apache.solr.handler.dataimport.DataImporter.doFullImport(DataImporter.java:422) at org.apache.solr.handler.dataimport.DataImporter.runCmd(DataImporter.java:487) at org.apache.solr.handler.dataimport.DataImporter.run(DataImporter.java:468) Caused by: java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: IF (1=115) SELECT slug_nl as slug_nl_0,slug_en as slug_en_0,label_nl as label_nl_0,label_en as label_en_0 FROM articlegroups ga WITH (NOLOCK) INNER JOIN products_category_mapping pcm on pcm.articlegroup_id=ga.id INNER JOIN products gp on gp.id=pcm.artikelid WHERE gp.artikelnummer='1000' AND ga.catlevel=0

我该如何配置?

<dataConfig> 
<dataSource name="ds-sql" driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;databaseName=mydb" user="myuser" password="mypwd" /> 

<document name="searchobjects"> 
    <entity name="searchobject" pk="id" dataSource="ds-sql" query="SELECT c.objecttype,c.id,c.title FROM locations c UNION SELECT 115 as objecttype,c.id,c.title FROM products c">  
        <field name="id" column="id" /> 
        <field name="objecttype" column="objecttype" /> 
        <field name="title" column="title" /> 

        <entity name="categories_lvl_0" dataSource="ds-sql" query="SELECT slug_nl as slug_nl_0,slug_en as slug_en_0,label_nl as label_nl_0,label_en as label_en_0 
        FROM articlegroups ga WITH (NOLOCK) 
        INNER JOIN products_category_mapping pcm on pcm.articlegroup_id=ga.id 
        INNER JOIN products gp on gp.id=pcm.artikelid 
        WHERE gp.artikelnummer='${searchobject.id}' AND ga.catlevel=0"> 
        </entity>   
    </entity> 
</document> 
</dataConfig> 

似乎在 data-config 中编写更复杂的 SQL 是不可能的,所以我最终调用了带参数的存储过程。在存储过程中我可以编写更复杂的逻辑。 Data-config 样本:

<entity name="categories_lvl_0" dataSource="ds-sql" query="[_getSolrProductCategoriesLvl0] '${searchobject.objecttype}', '${searchobject.id}'"> 
</entity>

此处有更多详细信息:calling stored procedure from solr