Search code examples
solrdataimporthandler

Foreign key references in Solr dataImportHandler


I've just started using Solr. In my database I have a collection of folders containing two kinds of entities, lets call them barrels and monkeys. Folders contain barrels and barrels contain monkeys. Users should be able to search for barrels and monkeys, but they are only allowed to see certain folders and the search should not return barrels or monkeys in folders they are not allowed to see. I have a filter query which does this fine for the barrels, but I'm having trouble getting the data import handler to import the folder ids for the monkeys. My data-config file looks like this:

<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/myDB" user="myUser" password="pass"/>
    <document name="item">
        <entity name="barrels" query="select * from barrels where is_deleted=0" transformer="TemplateTransformer"
                deltaQuery="select barrel_id from barrels where last_modified > '${dataimporter.last_index_time}'">
            <field column="itemType" template="barrels" name="itemType"/>
            <field column="barrel_id" name="id" pk="true" template="barrel-${barrels.barrel_id}"/>
            <!--Other fields-->

            <field column="folder_id" name="folder_id"/>
        </entity>

        <entity name="monkeys" query="select * from monkeys where is_deleted=0" transformer="TemplateTransformer"
                deltaQuery="select monkey_id from monkeys where last_modified > '${dataimporter.last_index_time}'">
            <field column="itemType" template="monkeys" name="itemType"/>
            <field column="monkey_id" name="id" pk="true" template="monkey-${monkeys.monkey_id}"/>
            <field column="barrel_id" name="barrel_id"/>
            <!--Other fields-->

            <entity name="barrels" 
                    query="select folder_id from barrels where barrel_id='${monkeys.barrel_id}'">
                <field name="folder_id" column="folder_id" />
            </entity>
        </entity>
    </document>
</dataConfig>

When I change the '${monkeys.barrel_id}' in the foreign key query to 28, it works, but when I try and get it to use the correct id, it doesn't import anything.

Can anyone spot what I'm doing wrong, or tell me a good way to debug this kind of thing? E.g. how can I get it to tell me what value it has for '${monkeys.barrel_id}' ? All the relevant fields are defined in schema.xml. Since having this problem I've made sure the documents all have the same names as the tables, and tried changing various bits of query to upper case, but everything's in lower case in the database and it doesn't seem to help.


Solution

  • Having asked the question, I did manage to figure it out eventually. Here's what I learnt:

    1) Getting it to tell you the query is very useful, and it is just a matter of setting the logging level to fine. You have to set it to fine in all the relevant places though. So for my Standalone.xml (in WildFly), in addition to the

    <logger category="org.apache.solr">
        <level name="FINE"/>
    </logger>
    

    bit, I needed to set the file logger and another logging bit to fine. Really should have realised that earlier...

    2) The single quotes are not part of the expression evaluation syntax, they are just quotes. So you don't need them when dealing with ints. I guess the example that comes with solr uses string ids rather than int ids and that's why it has the quotes?

    3) Once I'd got rid of the quotes, changing the case did make a difference. For my database its preferred case was Barrel_ID for some reason. I hadn't tried it much with capitals at both ends but not in the middle, but that's what worked. So I guess the moral of the story is that it is worthwhile to try lots of different cases even if they seem silly.