2012-10-23 23 views
4

所以我有一些數據,我想放入Solr的索引標準化表格,本與MySQL的多行映射項目到Solr

+----+--------------+--------------+---------+ 
| id |  name  | attribute | value | 
+----+--------------+--------------+---------+ 
| 1 | Apple  | color  | green | 
| 1 | Apple  | shape  | round | 
| 1 | Apple  | origin  | Belgium | 
| 2 | Motorbike | type   | fast | 
| 2 | Motorbike | nr of wheels | 2  | 
| 3 | Office chair | color  | grayish | 
| 3 | Office chair | spins  | yes  | 
+----+--------------+--------------+---------+ 

一些類似於現在,我寧願它被索引爲每個唯一ID(即項目)的一個文檔。但是,我將不得不將n個屬性合併到一個文檔中。要做到這一點,我需要用我的dataConfig做一些魔術。但是,我如何存儲和映射n個字段?這是使用動態字段的正確時機嗎?

這是我目前的嘗試。我很確定這是無效的。

<dataConfig> 
    <dataSource 
     type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
     url="jdbc:mysql://localhost/mystuff" 
     user="root" password="*****"/> 

    <document name="doc"> 
     <entity name="t1" query="select * from item_to_id_table"> 
      <field name="id" column="id"/> 
      <field name="name" column="name"/> 
      <entity name="t2" query="select * from my_flat_table" 
        cacheKey="t1.id" 
        cacheLookup="t2.id"> 

       <!-- alt 1 --> 
       <field name="$(t2.attribute)" column="value" /> 
       <!-- alt 2 --> 
       <entity name="properties" query="select property, value from t2" 
         cacheKey="$(t2.attribute)" 
         cacheLookup="property"> 
        <field name="$(properties.property)" column="value" /> 
       </entity> 
      </entity> 
     </entity> 

    </document> 
</dataConfig> 

我敢肯定,無論是兩個備選方案是有效的,我會盡快嘗試出來,除非我能想出更好的東西。也許腳本轉換爲第三種選擇。

這個用例是否合理與Solr一起使用?

+0

** [本博客文章](HTTP ://www.chrisumbel.com/article/solr_dataimporthandler_dih_scripttransformer)**看起來很有希望。 – worldsayshi

+0

[有點相關](http://stackoverflow.com/questions/7917317/dynamic-column-names-using-dih-dataimporthandler)。 – worldsayshi

回答

5

我按照here的方法解決了這個問題。

總之,我使用腳本轉換將「屬性」實體行轉換爲帶有前綴「p_」的字段。有些這樣的(例如代碼,可能存在錯誤):

<dataConfig> 
    <dataSource 
     type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
     url="jdbc:mysql://localhost/mystuff" 
     user="root" password="*****"/> 

    <script> 
    <![CDATA[ 
    function formProperty(row) { 
     var propName = row.get("property"); 
     var propVal = row.get("value"); 
     var fieldName = "p_" + propName; 
     row.put(fieldName,propVal); 
     return row; 
    } 
    ]]> 
    </script> 

    <document name="doc"> 
     <entity name="t1" query="select * from item_to_id_table"> 
      <field name="id" column="id"/> 
      <field name="name" column="name"/> 
      <entity name="t2" 
        query="select * from my_flat_table 
        where my_flat_table.id = ${t1.id}" 
        transformer="script:formProperty"> 
      </entity> 
     </entity> 
    </document> 
</dataConfig> 

我然後被映射到它們Solr的圖式中schema.xml中作爲動態字段

<dynamicField name="p_*" indexed="true" stored="true" type="string"/> 
+0

請將index =「true」修改爲indexed =「true」,將dynamicfield修改爲dynamicField。 – yAnTar

+0

修好了,謝謝。未經測試。 – worldsayshi

+0

並感謝解決方案 - 它的工作原理。 – yAnTar