Programmatically load data into solr using solrj and java

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

+3
source share
3 answers

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

+4
source

. , groovy. , :

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(); 

DirectXmlRequest URL-, "/update" xml , XML.

<add>
   <doc>
     <field name="title">blah</field>
   </doc>
</add>
+5
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();
}
-1
source

Source: https://habr.com/ru/post/1747768/


All Articles