繁体   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