Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically load data into solr using solrj and java

Tags:

java

xml

solr

solrj

How can I load data from an xml file into solr using the solrj API?

like image 729
maximus Avatar asked Jan 28 '26 08:01

maximus


1 Answers

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>
like image 71
maximus Avatar answered Jan 30 '26 23:01

maximus