sqlsolrdataimporthandler

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


How can I make the execution of a query in a sub-entity dependent on a field value from the main entity?

So as you can see in the (simplified) data config below, there's an entity categories_lvl_0 which holds an expensive query. I ONLY want to execute this query if: searchobject.objecttype=115

I tried making the categories_lvl_0 dependent on an if statement: IF (${searchobject.objecttype}=115), I tried both with and without wrapping the query in a BEGIN...END, but all these options throw the error:

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$1.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

How can I configure this?

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

Solution

  • Seems like writing more complex SQL in data-config is not possible so I ended up calling a stored procedure with parameters. In the stored procedure I can write more complex logic. Data-config sample:

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

    More details here: calling stored procedure from solr