2011-08-16 40 views
4

我有一些XML可以攝入到Solr中,這聽起來像是一個用於DataImportHandler解決的用例。我想要做的是從一個XML屬性和另一個屬性的值中提取列名。這裏是我的意思的例子:Solr DataImportHandler:我可以從XPathEntityProcessor的xml屬性獲取動態字段名稱嗎?

<document> 
    <data ref="reference.foo"> 
    <value>bar</value> 
    </data> 
</document> 

從這個XML片段,我想添加一個字段名稱reference.foo和值bar。 DataImportHandler包含一個用於處理XML文檔的XPathEntityProcessor。我已經嘗試過使用它,如果我給它一個已知的列名稱(例如,<field column="ref" xpath="/document/data/@ref">),但它無法找到任何文檔或示例來建議如何執行我想要的操作,或者它無法完成。所以:

  • 我可以使用XPathEntityProcessor來做到這一點嗎?如果是這樣,怎麼樣?
  • 如果不是,我可以使用DataImportHandler以其他方式執行此操作嗎?
  • 還是我離開寫我自己的進口處理程序?

回答

5

我沒有設法找到一個方法來做到這一點沒有帶來一個變壓器,而是通過一個簡單的ScriptTransformer我的工作了。它是這樣的:

... 
<script> 
function makePair(row) { 
    var theKey = row.get("theKey"); 
    var theValue = row.get("theValue"); 

    row.put(theKey, theValue); 
    row.remove("theKey"); 
    row.remove("theValue"); 

    return row; 
} 
</script> 

... 

<entity name="..." 
    processor="XPathEntityProcessor" 
    transformer="script:makePair" 
    forEach="/document" 
    ...> 

    <field column="theKey" xpath="/document/data/@ref" /> 
    <field column="theValue" xpath="/document/data/value" /> 
</entity> 
... 

希望能幫助別人!

注意,如果你的dynamicField是多值的,你必須遍歷theKey,因爲row.get(「theKey」)將是一個列表。

1

你想要做的是選擇一個屬性值的節點鍵控。

從你的榜樣,你可以這樣做:

<field column="ref" xpath="/document/data[@ref='reference.foo']"/> 
+0

如果你已經知道'ref'的值是你想要的值,這會很有幫助。在我的情況下,我不知道這個值,所以我不能把它放在'xpath'屬性中。雖然謝謝! – rathstar

+0

哦,我明白了。對不起,這對你沒有幫助,但很高興你知道。 – inanutshellus

相關問題