I want to import a json file to ElasticSearch, however I could'nt digest how to import a json file to ElasticSearch. Please look over and iron out my view.
First, I tried following command to execute mapping.
curl -XPUT 'localhost:9200/library/book/_mapping' -d @mapping.json
The mapping.json is like this.
{
"book" : {
"_source": {
"enabled": true
},
"_index" : {
"enabled" : true
},
"_id" : {
"index": "not_analyzed",
"store" : "yes"
},
"properties" : {
"author" : {
"type" : "string"
},
"characters" : {
"type" : "string"
},
"copies" : {
"type" : "long",
"ignore_malformed" : false
},
"otitle" : {
"type" : "string"
},
"tags" : {
"type" : "string"
},
"title" : {
"type" : "string"
},
"year" : {
"type" : "long",
"ignore_malformed" : false,
"index" : "analyzed"
},
"available" : {
"type" : "boolean"
}
}
}
}
The return from my console is here.
{"acknowledged":true}
And then I tried this command to import documents.json to ElasticSearch
curl -s -XPOST 'localhost:9200/_bulk' --data-binary @documents.json
The documents.json is this.
{
"index":{
"_index":"library",
"_type":"book",
"_id":"1"
}
}{
"title":"All Quiet on the Western Front",
"otitle":"Im Westen nichts Neues",
"author":"Erich Maria Remarque",
"year":1929,
"characters":[
"Paul Bäumer",
"Albert Kropp",
"Haie Westhus",
"Fredrich Müller",
"Stanislaus Katczinsky",
"Tjaden"
],
"tags":[
"novel"
],
"copies":1,
"available":true,
"section":3
}{
"index":{
"_index":"library",
"_type":"book",
"_id":"2"
}
}{
"title":"Catch-22",
"author":"Joseph Heller",
"year":1961,
"characters":[
"John Yossarian",
"Captain Aardvark",
"Chaplain Tappman",
"Colonel Cathcart",
"Doctor Daneeka"
],
"tags":[
"novel"
],
"copies":6,
"available":false,
"section":1
}{
"index":{
"_index":"library",
"_type":"book",
"_id":"3"
}
}{
"title":"The Complete Sherlock Holmes",
"author":"Arthur Conan Doyle",
"year":1936,
"characters":[
"Sherlock Holmes",
"Dr. Watson",
"G. Lestrade"
],
"tags":[
],
"copies":0,
"available":false,
"section":12
}{
"index":{
"_index":"library",
"_type":"book",
"_id":"4"
}
}{
"title":"Crime and Punishment",
"otitle":"Преступлéние и наказáние",
"author":"Fyodor Dostoevsky",
"year":1886,
"characters":[
"Raskolnikov",
"Sofia Semyonovna Marmeladova"
],
"tags":[
],
"copies":0,
"available":true
}
The return from the console is here.
{
"took":4,
"errors":false,
"items":[
{
"index":{
"_index":"library",
"_type":"book",
"_id":"1",
"_version":1,
"status":201
}
},
{
"index":{
"_index":"library",
"_type":"book",
"_id":"2",
"_version":1,
"status":201
}
},
{
"index":{
"_index":"library",
"_type":"book",
"_id":"3",
"_version":1,
"status":201
}
},
{
"index":{
"_index":"library",
"_type":"book",
"_id":"4",
"_version":1,
"status":201
}
}
]
}
And I tried following command.
curl -XGET 'localhost:9200/library/book/_search?q=title:crime&pretty=true'
The return from the console is here.
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "library",
"_type" : "book",
"_id" : "4",
"_score" : 0.15342641
} ]
}
}
I could'nt point out why this returned json doesn't have '_source' key. How can i achieve this?
In your mapping, you need to add "store": "yes"
in your _source
field and it will work:
{
"book" : {
"_source": {
"enabled": true,
"store": "yes" <--- add this
},
"_index" : {
"enabled" : true
},
"_id" : {
"index": "not_analyzed",
"store" : "yes"
},
"properties" : {
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.