solr中一对多,多对多关系

先看下官方文档的例子:

假设有如下表结构:

solr中一对多,多对多关系

data-config.xml:一对多,多对多的关系写法如下:

<dataConfig>
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />
    <document name="products">
        <entity name="item" query="select * from item">
            <field column="ID" name="id" />
            <field column="NAME" name="name" />
            <field column="MANU" name="manu" />
            <field column="WEIGHT" name="weight" />
            <field column="PRICE" name="price" />
            <field column="POPULARITY" name="popularity" />
            <field column="INSTOCK" name="inStock" />
            <field column="INCLUDES" name="includes" />

            <entity name="feature" query="select description from feature where item_id='${item.ID}'">
                <field name="features" column="description" />
            </entity>
            <entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
                <entity name="category" query="select description from category where id = '${item_category.CATEGORY_ID}'">
                    <field column="description" name="description" />
                </entity>
            </entity>
        </entity>
    </document>
</dataConfig>

一对多写法:

<entity name="feature" query="select description from feature where item_id='${item.id}'">
    <field name="feature" column="description" />
</entity>

多对多写法:

<entity name="item_category" query="select category_id from item_category where item_id='${item.id}'">
    <entity name="category" query="select description from category where id = '${item_category.category_id}'">
        <field column="description" name="description" />
    </entity>
</entity>

本人使用的是以上的写法,api中也给出了另一种写法,但自己没有测试过:

<dataConfig>
    <dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />
    <document>
        <entity name="item" query="select * from item">
            <entity name="feature" query="select description as features from feature where item_id='${item.ID}'"/>
            <entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
                <entity name="category" query="select description as cat from category where id = '${item_category.CATEGORY_ID}'"/>
            </entity>
        </entity>
    </document>
</dataConfig>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

刚开始配置完成后,得出的结果始终是一对一:

如上图:假如:item表和item_category表是一对多,但我这边始终得出的结果是一对一,如:name和description不是一个name对应多个description(实际数据库的结果是一对多的)有点不解;

其实,除了data-config.xml文件需要配置外,还有个重要的文件,managed-schema文件(本人使用的是solr7,老版本里是schema.xml,操作都一样,只要名称不同)也需要修改:

添加索引字段:

<field name="id" type="string" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="manu" type="string" indexed="true" stored="true" />
<field name="weight" type="string" indexed="true" stored="true" />
<field name="price" type="string" indexed="true" stored="true" />
<field name="popularity" type="string" indexed="true" stored="true" />
<field name="inStock" type="string" indexed="true" stored="true" />
<field name="includes" type="string" indexed="true" stored="true" />

<field name="features" type="string" indexed="true" stored="true" />
<field name="cat" type="string" indexed="true" stored="true" />

这个是最开始的配置,达不到理想的结果:

由于本人刚学习solr,不太熟悉,网上百度了一翻,才发现还有个:multiValued(多值的)属性,想要的是一个name对应多个description,那么就应该把description配置成多值的,所以添加multiValued属性,修改managed-schema文件:

<field name="id" type="string" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="manu" type="string" indexed="true" stored="true" />
<field name="weight" type="string" indexed="true" stored="true" />
<field name="price" type="string" indexed="true" stored="true" />
<field name="popularity" type="string" indexed="true" stored="true" />
<field name="inStock" type="string" indexed="true" stored="true" />
<field name="includes" type="string" indexed="true" stored="true" />

<field name="features" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>

再次导入数据,得到想要的结果了

solr中一对多,多对多关系

solr继续学习中....