如何将json文件导入ElasticSearch



我想把一个json文件导入到ElasticSearch,但我无法理解如何将json文件导出到Elastic Search。请看过去,把我的视野弄清楚。

我试过的程序

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

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"键。我怎样才能做到这一点?

在映射中,您需要在_source字段中添加"store": "yes",它将起作用:

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

最新更新