4

再一次.. 我有 2 个表“博客”和“评论”。一个博客可以包含 n 条评论(博客 --1:n-- 评论)。最新我使用以下选择将数据插入到 solr 索引中:

    <entity name="blog" dataSource="mssqlDatasource" pk="id"
        transformer="ClobTransformer"
        query="SELECT b.id, b.market, b.title AS blogTitle, b.message AS            
                blogMessage, c.message AS commentMessage, c.secondColumn 
                FROM blog b LEFT JOIN comment c ON b.id = c.source_id
                AND c.source_type = 'blog'">
        <field column="blogMessage" name="blogMessage" clob="true" />
        <field column="commentMessage" name="commentMessage" clob="true" />
    </entity>

索引结果如下所示:

    <doc>
     <str name="id">1</str>
     <str name="market">12</str>
     <str name="title">blog of title 1</str>
     <str name="blogMessage">message of blog 1</str>
     <str name="commentMessage">message of comment</str>
     <str name="scondColumn">Im the second column from comment</str>
    </doc>

    <doc>
     <str name="id">1</str>
     <str name="market">12</str>
     <str name="title">blog of title 1</str>
     <str name="blogMessage">message of blog 1</str>
     <str name="commentMessage">message of comment - Im the second comment</str>
     <str name="scondColumn">Im the second column from comment</str>       
    </doc>

我会说这很愚蠢,因为我在同一个博客中获得了太多索引数据,只是评论不同。是否可以将“评论”设置为“子实体”,如下所示:

    <entity name="blog" dataSource="mssqlDatasource" pk="id"
        transformer="ClobTransformer"
        query="SELECT b.id, b.market, b.title AS blogTitle, b.message AS            
                blogMessage
                FROM blog b">
         <field column="blogMessage" name="blogMessage" clob="true" />

          <entity name="comment" dataSource="mssqlDatasource" pk="id"
             transformer="ClobTransformer"
             query="SELECT c.message as commentMessage, c.secondColumn
                FROM comment c
                WHERE c.source_id = ${blog.id}">
            <field column="commentMessage" name="commentMessage" clob="true" />              
          </entity>
    </entity>

那可能吗?结果如何(直到星期一才能测试)?

4

1 回答 1

1

你快到了,如果你想为每个博客文档填写一个多值评论字段,你需要CachedSqlEntityProcessor

我的看起来很像这样(我省略了 clob 变压器位,但你显然需要它们)

<entity name="blog" 
        query="SELECT b.id, 
                      b.market, 
                      b.title AS blogTitle, 
                      b.message AS            
               blogMessage
               FROM blog b">
    <entity name="blog_comment" 
            query="SELECT c.message as commentMessage, 
                          c.secondColumn,
                          c.blog_id
            FROM comment c" 
     processor="CachedSqlEntityProcessor"  
     where="blog_id=blog.id"/>    
<entity>

文档应如下所示:

<doc>
   <str name="id">1</str>
   <str name="market">12</str>
   <str name="title">blog of title 1</str>
   <str name="blogMessage">message of blog 1</str>
   <arr name="commentMessage">
       <str>message of comment</str>
       <str>message of comment - Im the second comment</str>
   </arr>
   <arr name="secondColumn">
       <str>Im the second column from comment</str>
       <str>Im the second column from comment</str>
   </arr>
</doc>

如果您想避免重复值,您可能想要执行多个嵌套实体,一个查询每一列的实体。

于 2014-04-17T18:45:29.110 回答