2016-02-05 58 views
0

我试图导入到Solr 5.1.0和5.2.1中,该数据配置应该生成具有以下结构的文档:Solr 5.1.0和5.2.1:使用DIH创建亲子文档使用DIH

<parentDoc> 
    <someParentStuff/> 
    <childDoc> 
     <someChildStuff/> 
    </childDoc> 
</parentDoc> 

从我从this question about nested entities in DIH答案的一个理解,我的Solr的版本应该可以用下面的data-config.xml创建上面的结构:

<dataConfig> 
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
      url="" 
      user="" 
      password="" 
      batchSize="-1" 
    /> 
    <document name=""> 
     <entity rootEntity="true" name="parent" pk="parent_id" query="select * from parent"> 
      <field column="parent_id" name="parent_id" /> 

      <entity child="true" name="child" query="select * from child where parent_id='${parent.parent_id}'"> 
       <field column="parent_id" name="parent_id" /> 
       <field column="item_status" name="item_status" /> 
      </entity> 
     </entity> 
    </document> 
</dataConfig> 

然而,当我执行full-import,我得到:

<result name="response" numFound="2" start="0"> 
    <doc> 
    <long name="parent_id">477</long> <!-- This is from the child --> 
    <str name="item_status">WS</str> 
    </doc> 
    <doc> 
    <long name="parent_id">477</long> <!-- This is from the parent --> 
    </doc> 
</result> 

我理解的是你应该得到5.1.0之前的非规格化布局;不过,我预计:

<result name="response" numFound="1" start="0"> 
    <doc> 
     <long name="parent_id">477</long> 
     <doc> 
      <long name="parent_id">477</long> 
      <str name="item_status">WS</str> 
     </doc> 
    </doc> 
</result> 

我需要做什么来获得我想要的文档结构?我误解了DIH中的嵌套实体应该做什么?

回答

3

除非有人摇摆不定地告诉我,否则似乎我真的误解了在Solr 5.1.0+中创建亲子文档。我希望能够嵌套文件并让它们返回,但Solr无法实现(至少在这一点上,未来是个谜)。

Solr是一个平面文档模型。这意味着它不能像我原来的问题那样模仿亲子关系。没有嵌套。一切都是平坦的和非规范化的。

Solr做了什么,它将n个子文档添加到邻接块的父项旁边。例如:

childDoc1 childDoc2 childDoc3 parent

这种结构实际上反映了我“误”正从Solr中返回的文件:

<result name="response" numFound="2" start="0"> 
    <doc> 
    <long name="parent_id">477</long> <!-- This is from the child --> 
    <str name="item_status">WS</str> 
    </doc> 
    <doc> 
    <long name="parent_id">477</long> <!-- This is from the parent --> 
    </doc> 
</result> 

嵌套文档支持在DIH可用之后Solr的5.0实际上是对过去人们不得不索引嵌套文档的旧方式进行附加或完全替换,并且似乎也会为您同时更新子+父文档。很方便!

这样,那么,你如何表达父子关系时Solr的破坏那该多好,嵌套的文档模型,你曾计划?您必须获得父文档和子文档,并在您的应用程序中管理关系。你如何得到父母和孩子?

答案是块连接。在查询时

使用块中的连接,然后在您的应用程序,处理这些文件到您所需的结构。让我们看看两个块连接查询,因为它们起初看起来有点怪异。

{!parent which='type:parent'}item_id:5918307

此块连接查询说,“给我有与5918307的item_id一个或多个子文件,主文件。“

{!child of='type:parent'} (fieldA:TERM^100.0 OR fieldB:term^100.0 OR fieldC:term OR (fieldD:term^20.0)) AND (instock:true^9999.0)

此块连接查询说,‘给我一个或多个子文件的父文件包含单词‘术语’,并有库存。’

不要对孩子字段时不搜索做!child查询。所以,它引用了第一个例子,你就不会在搜索item_id,因为这会给你一个500错误。

你会在这些查询注意到type场。你必须把它添加到您的架构和数据配置你自己。在该模式中,它看起来像这样:

<!-- use this field to differentiate between parent and child docs --> 
<field name="type" type="string" indexed="true" stored="false" /> 
data-config.xml

然后,只需像做父如下:

if ('true' = 'true', 'parent', 'parent') as type

而对于孩子做同样的,不同的是用“孩子”之前你把“父母”放在哪里。

因此,最终可能最终做出两个查询,但似乎并没有像添加块连接解析器那样增加查询时间。我看到每个查询可能会多出50或100毫秒。

您通常也可以绕过需要嵌套的文档,因为您的连接很聪明。但是,我发现,因为子文档现在与父文档混合在一起,所以每个父文档都有一个“副本”,并在索引中包含特定的子文档信息。在这种情况下,您可以从第一个文档中获取已知的父字段及其子字段。对于其他文件,您只需抓住子域。

另一种选择是,当您只想要父文档并且不希望返回大量其他文档时,就是使用分组查询;不过,我不会推荐它。当我在一个返回大量结果的查询上尝试它时,我看到查询时间从10ms到250ms范围一直到500ms-1s范围。