solr中一对多,多对多关系

先看下官方文档的例子:

假设有如下表结构:

example-schema.png

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查询记录有5条,使用Java查询:

HttpSolrClient solrServer = createSolrServer("goods");
SolrQuery query = new SolrQuery();
query.set("q", "*:*");
// 设置分页参数
query.setStart(1);
// 每一页多少值
query.setRows(10);
QueryResponse response = solrServer.query(query);
SolrDocumentList solrDocumentList = response.getResults();

每页10条,从第一页开始,一切看着没问题,可是结果只有4条,solr查询少了一条记录,百思不得其解...

原来数据库默认分页limit(mysql)是1,但solr是0,所以需要把query.setStart(1)--修改为-->query.setStart(0),从第一页开始,才是正确的!!!

solr继续学习中....

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 如果你想在Solr执行分组查询,并且你的字段包含多个值,你可以使用Solr的多值分组功能。为了使用这个功能,你需要使用Solr的分组查询语法,并在查询指定“group.field”参数来指定要分组的字段。然后,您可以使用Solr的“group.limit”参数来指定每个分组包含的文档数的最大值。这将确保您的分组查询结果仅包含与每个分组相关的最相关文档。从Solr 4.0开始,您还可以使用Solr的“group.format”参数来指定分组查询结果的格式,以便更好地满足您的需求。 ### 回答2: Solr是一个强大的搜索引擎,可以用于进行高效的分组查询。当我们需要对包含多个值的字段进行分组查询时,可以通过使用Solr的facet功能来实现。 在Solr,可以使用facet.field参数指定要进行分组的多值字段。例如,如果我们有一个字段名为"tags",该字段包含多个标签,可以通过将facet.field设置为"tags"来进行分组查询。在搜索请求,我们需要设置facet参数为true来启用分组查询。 当我们执行搜索请求时,Solr会计算每个标签的出现次数,并返回每个标签及其对应的文档数。这样,我们可以获得每个标签的分组统计信息。 我们还可以通过设置facet.limit参数来限制返回的分组结果数量。如果我们想要获取前n个最常见的标签,可以将facet.limit设置为n。 另外,我们还可以对分组结果进行排序,以便按照某个特定的标准对标签进行排序。例如,我们可以使用facet.sort参数将分组结果按照文档数目或者标签名称进行排序。 除了对整个多值字段进行分组查询,我们还可以在分组查询指定条件。例如,我们可以使用facet.query参数来指定额外的条件,并对满足条件的文档进行分组查询。 总结来说,Solr可以通过使用facet功能对包含多个值的字段进行分组查询。我们可以指定要进行分组的字段,并可以设置额外的条件、排序和限制来获取所需的分组统计信息。 ### 回答3: Solr是一个开源的搜索平台,它支持多值字段的分组查询。在Solr,可以使用facet查询来实现分组查询,并且可以应用于包含多值的字段。 要对包含多值的字段进行分组查询,首先需要在solrconfig.xml文件配置相应的字段类型。可以使用Solr提供的一些已有的多值字段类型,例如"string_mv"、"text_general_mv"等,也可以根据需要自定义字段类型。 在查询时,可以使用facet.field参数指定需要进行分组查询的字段名。当指定的字段是一个多值字段时,Solr会根据每个值进行分组。结果将包含该字段的每个值作为分组的key,并统计每个分组出现的次数。 例如,假设有一个包含多个标签的字段"tags",现在需要对"tags"字段进行分组查询。可以使用如下查询语句: ``` /select?q=*:*&facet=true&facet.field=tags ``` 执行该查询后,Solr将返回一个结果集,其包含了"tags"字段的每个值作为分组的key,并统计了每个分组出现的次数。这样就实现了对包含多值的字段进行分组查询。 需要注意的是,对于大量数据或者需要更复杂的聚合操作的情况,可能需要使用更高级的分组查询功能,例如利用facet.pivot参数进行多级分组或者使用facet.query参数进行条件过滤等。 综上所述,Solr支持对包含多值的字段进行分组查询,通过配置字段类型和使用facet查询参数,可以轻松实现这一功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值