2012-01-13 43 views
3

我不懂java,我不懂XML,也不知道Lucene。現在,這是不可能的。我一直在努力使用apache solr/lucene創建一個小項目。我的問題是我無法索引xml文件。我想我理解它應該如何工作,但我可能是錯的。我不確定你需要哪些信息來幫助我,所以我只會發布代碼。需要幫助使用DataImportHandler將XML文件編入Solr

<dataConfig> 
<dataSource type="FileDataSource" encoding="UTF-8" /> 
<document> 
<!-- This first entity block will read all xml files in baseDir and feed it into the second entity block for handling. --> 
<entity name="AMMFdir" rootEntity="false" dataSource="null" 
     processor="FileListEntityProcessor" 
     fileName="^*\.xml$" recursive="true" 
     baseDir="C:\Documents and Settings\saperez\Desktop\Tomcat\apache-tomcat-7.0.23\webapps\solr\data\AMMF_New" 
     > 
<entity 
     processor="XPathEntityProcessor" 
     name="AMMF" 
     pk="AcquirerBID" 
     datasource="AMMFdir" 
     url="${AMMFdir.fileAbsolutePath}" 
     forEach="/AMMF/Merchants/Merchant/" 
     transformer="DateFormatTransformer, RegexTransformer" 
     > 

    <field column="AcquirerBID" xpath="/AMMF/Merchants/Merchant/AcquirerBID" /> 
    <field column="AcquirerName" xpath="/AMMF/Merchants/Merchant/AcquirerName" /> 
    <field column="AcquirerMerchantID" xpath="/AMMF/Merchants/Merchant/AcquirerMerchantID" /> 

</entity> 
</entity> 
</document> 

示例XML文件

<?xml version="1.0" encoding="utf-8"?> 
<AMMF xmlns="http://tempuri.org/XMLSchema.xsd" Version="11.2" CreateDate="2011-11-07T17:05:14" ProcessorBINCIB="422443" ProcessorName="WorldPay" FileSequence="18"> 
<Merchants Count="153"> 
    <Merchant ChangeIndicator="A" LocationCountry="840"> 
    <AcquirerBID>10029881</AcquirerBID> 
    <AcquirerName>WorldPay</AcquirerName> 
    <AcquirerMerchantID>*</AcquirerMerchantID> 
    <Merchant ChangeIndicator="A" LocationCountry="840"> 
    <AcquirerBID>10029882</AcquirerBID> 
    <AcquirerName>WorldPay2</AcquirerName> 
    <AcquirerMerchantID>Hello World!</AcquirerMerchantID> 
</Merchant> 
</Merchants> 

我有這樣的架構。

<field name="AcquirerBID" type="string" indexed="true" stored="true" required="true" /> 
<field name="AcquirerName" type="string" indexed="true" stored="true" /> 
<field name="AcquirerMerchantID" type="string" indexed="true" stored="true"/> 

我在配置中有這個。

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler" default="true" > 
<lst name="defaults"> 
<str name="config">AMMFconfig.xml</str> 
</lst> 
</requestHandler> 

回答

1

經常做的不是使用DIH的最好的事情。使用您知道的語言使用API​​和自定義腳本發佈這些數據有多困難?

這種方法的好處是雙重的:

  1. 你更多地瞭解你的系統,並知道它更好。
  2. 你不花時間去了解DIH。

缺點是你重新發明了一下輪子,但是DIH是一件很難理解的事情。

+0

如果完全沒有幫助,那麼這些都是一些有效的問題。我不編碼... – 2012-01-14 00:25:39

+2

我會說編碼是Solr的一個要求。我無法想象,如果工具包中沒有至少一種語言,你將能夠完成大量工作。 – mlissner 2012-01-14 01:35:50

2

示例XML格式不正確。這也許可以解釋的錯誤索引文件:

$ xmllint sample.xml 
sample.xml:13: parser error : expected '>' 
</Merchants> 
     ^
sample.xml:14: parser error : Premature end of data in tag Merchants line 3 
sample.xml:14: parser error : Premature end of data in tag AMMF line 2 

修正XML

這裏就是我想你的樣本數據應該像(沒有檢查XSD文件)

<?xml version="1.0" encoding="utf-8"?> 
<AMMF xmlns="http://tempuri.org/XMLSchema.xsd" Version="11.2" CreateDate="2011-11-07T17:05:14" ProcessorBINCIB="422443" ProcessorName="WorldPay" FileSequence="18"> 
    <Merchants Count="153"> 
    <Merchant ChangeIndicator="A" LocationCountry="840"> 
     <AcquirerBID>10029881</AcquirerBID> 
     <AcquirerName>WorldPay</AcquirerName> 
     <AcquirerMerchantID>*</AcquirerMerchantID> 
    </Merchant> 
    <Merchant ChangeIndicator="A" LocationCountry="840"> 
     <AcquirerBID>10029882</AcquirerBID> 
     <AcquirerName>WorldPay2</AcquirerName> 
     <AcquirerMerchantID>Hello World!</AcquirerMerchantID> 
    </Merchant> 
    </Merchants> 
</AMMF> 

替代解決方案

我知道你說你不是程序員,但如果你使用solrj接口,這個任務就簡單得多。

下面是一個常規例子索引你的示例XML

// 
// Dependencies 
// ============ 
import org.apache.solr.client.solrj.SolrServer 
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer 
import org.apache.solr.common.SolrInputDocument 

@Grapes([ 
    @Grab(group='org.apache.solr', module='solr-solrj', version='3.5.0'), 
]) 

// 
// Main 
// ===== 

SolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/"); 
def i = 1 

new File(".").eachFileMatch(~/.*\.xml/) { 

    it.withReader { reader -> 
     def ammf = new XmlSlurper().parse(reader) 

     ammf.Merchants.Merchant.each { merchant -> 
      SolrInputDocument doc = new SolrInputDocument(); 

      doc.addField("id",   i++) 
      doc.addField("bid_s",  merchant.AcquirerBID) 
      doc.addField("name_s",  merchant.AcquirerName) 
      doc.addField("merchantId_s", merchant.AcquirerMerchantID) 

      server.add(doc) 
     } 
    } 

} 

server.commit() 

Groovy是一種不需要編譯一個Java腳本語言。這與維護DIH配置文件一樣容易。

1

要了解DIH XML導入的工作原理,我建議您首先仔細閱讀DIH wiki中的本章:http://wiki.apache.org/solr/DataImportHandler#HttpDataSource_Example

在瀏覽器中打開Slashdot鏈接http://rss.slashdot.org/Slashdot/slashdot,然後右鍵單擊頁面並選擇查看源代碼。這個例子中使用了XML文件。 將其與DIH示例中的XPathEntityProcessor配置進行比較,您會看到在Solr中導入任何XML文件是多麼容易。

如果您需要更多幫助,請詢問...