如何将json文件导入ElasticSearch?

时间:2015-12-15 16:14:00

标签: json curl elasticsearch

我想将一个json文件导入ElasticSearch,但我无法消化如何将json文件导入ElasticSearch。请仔细检查并理解我的观点。

我尝试的程序

首先,我尝试按照命令执行映射。

curl -XPUT 'localhost:9200/library/book/_mapping' -d @mapping.json

mapping.json就像这样。

{
  "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"
      }
    }
  }
}

从我的控制台返回此处。

{"acknowledged":true}

然后我尝试使用此命令将documents.json导入ElasticSearch

curl -s -XPOST 'localhost:9200/_bulk' --data-binary @documents.json

documents.json就是这个。

{  
   "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
}

从控制台返回此处。

{  
   "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
         }
      }
   ]
}

我尝试了以下命令。

curl -XGET 'localhost:9200/library/book/_search?q=title:crime&pretty=true'

从控制台返回此处。

{
  "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
    } ]
  }
}

我无法指出为什么这回归json没有' _source'键。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

在您的地图中,您需要在"store": "yes"字段中添加_source,它才有效:

{
  "book" : {
    "_source": {
      "enabled": true,
      "store": "yes"                 <--- add this
    },
    "_index" : {
      "enabled" : true
    },
    "_id" : {
      "index": "not_analyzed",
      "store" : "yes"
    },
    "properties" : {
...