簡體   English   中英

如何將json文件導入ElasticSearch?

[英]How to import a json file to 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'鍵。 我怎樣才能做到這一點?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM