import data from JSON in solr

2019-04-25 13:04发布

问题:

Currently I am using XML file in solr. I index xml file's data using DataimportHandler with XPathentityProcessor.

Now I want to import data from json file.

Is there any example ?

Regards, Sagar

回答1:

What you need is something like

curl 'http://localhost:8983/solr/update/json?commit=true' --data-binary @books.json -H 'Content-type:application/json'

Taken from the example.

Source: https://wiki.apache.org/solr/UpdateJSON



回答2:

DataImportHandler does not allow you to use JSON as a source. The only way is to use Update Handler which can handle JSON natively. But that has to be in the JSON structure Solr expects (array of hashes or hash of command/hashes).



回答3:

if you dont want to use curl command, you can fire the command directly on the browser and get the desired result:

http://localhost:8983/solr/update/json?commit=true --data-binary @books.json -H 'Content-type:application/json'

Put the json file in /example/exampledocs folder.This is default directory path in solr. If you are using java or php etc then there are several classes and methods that you use and then you wont require to mention the entire command as above.Is that what you were asking for?



回答4:

You can also update your docs by including the ?commit=true statement within the url of a curl command like so.

curl -X POST -H "Content-Type: application/json" -u "{usernamne}":"{password}" "https://your_host/solr/your_collection/update/json?commit=true" --data-binary @/path/to/your/data/your_data.json


回答5:

You can use REST api to send data to Solr. Please use this path:

localhost:8983/solr/simple2/update?commit=true
//(simple2 is the core name and localhost:8983 is server path.)

and you have to define

:content_type => 'application/json'

in request header.Along with it you can send json file/data to solr using post request.

For more information you can visit http://geekdirt.com/blog/indexing-in-solr-using-json-and-rest-apis/



回答6:

If you want import part or the entire collection from a json format, well, there is an alternative.

I wrote a java tool: https://github.com/freedev/solr-import-export-json

This is a java application that imports and exports a Solr collection using SolrJ. Every document has to be a json object and in the file you are importing you must have a list of lines whereas each line is a json object.

{ "id": 1, "date": "20160101T00:00:00", "text": "some text" } 
{ "id": 2, "date": "20160102T00:00:00", "text": "some text" } 
{ "id": 3, "date": "20160103T00:00:00", "text": "some text" } 

I haven't tried with nested documents, and the keys of json document should be exactly the names of the Solr fields.



标签: json solr