开发者

Programmatically load data into solr using solrj and java

开发者 https://www.devze.com 2023-01-01 08:04 出处:网络
How can开发者_如何学Go I load data from an xml file into solr using the solrj API?Thanks Pascal. I miss worded my question, I\'m actually using groovy. But in any event your approach does work, but th

How can开发者_如何学Go I load data from an xml file into solr using the solrj API?


Thanks Pascal. I miss worded my question, I'm actually using groovy. But in any event your approach does work, but this was my solution:

CommonsHttpSolrServer server = SolrServerSingleton.getInstance().getServer(); 
def dataDir = System.getProperty("user.dir"); 
File xmlFile = new File(dataDir+"/book.xml"); 
def xml = xmlFile.getText(); 
DirectXmlRequest xmlreq = new DirectXmlRequest( "/update", xml); 
server.request(xmlreq);
server.commit(); 

The first arg to DirectXmlRequest is a url path, it must be "/update" and that the variable xml is a string containing the XML. For example

<add>
   <doc>
     <field name="title">blah</field>
   </doc>
</add>


With Java 6, you can use Xpath to fetch what you need from your xml file. Then, you populate a SolrInputDocument from what you extracted from the xml. When that document contains everything you need, you submit it to Solr using the add method of SolrServer.


SolrClient client = new HttpSolrClient("http://localhost:8983/solr/jiva/");
String dataDir = System.getProperty("user.dir");    
File xmlFile = new File(dataDir + "/Alovera-Juice.xml");
if (xmlFile.exists()) {
    InputStream is = new FileInputStream(xmlFile);
    String str = IOUtils.toString(is);
    DirectXmlRequest dxr = new DirectXmlRequest("/update", str);
    client.request(dxr);
    client.commit();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号