sal*_*hed 2 mongodb python-2.7 elasticsearch
我ElasticSearch在一个网站中使用,我从MongoDB.
def postToEs(self):
"""
put data to the elasticsearch
"""
es = Elasticsearch()
cursor = self.getMongoData()
for document in cursor:
esdoc={}
esdoc["category"] = document.get("category")
esdoc["description"] = document.get("description")
esdoc["title"] = document.get("title")
esdoc["link"] = document.get("link")
esdoc["state"] = document.get("state")
esdoc_id = esdoc["link"]
es.index(
index = 'news',
doc_type = 'allnews',
id = esdoc_id,
body = json.dumps(esdoc)
)
Run Code Online (Sandbox Code Playgroud)
这很好用。但目前我必须在该state字段中搜索elasticsearch. 目前,如果我搜索New York它的条目也会给出New Hampshire. 我找到了这个链接,并看到了我需要添加mapping到数据中的elasticsearch 文档。我的问题是如何在当前场景中为数据添加映射?或者有什么更好的方法吗?
删除现有索引
curl -XDELETE "http://hostname:9200/index/type"
Run Code Online (Sandbox Code Playgroud)
删除现有的河流配置索引
curl -XDELETE "http://hostname:9200/_river"
Run Code Online (Sandbox Code Playgroud)
创建到索引的映射
curl -XPUT "http://hostname:9200/index/type/_mapping" -d'
{
"allnews": {
"properties": {
"category": {
"type": "string"
},
"description": {
"type": "string"
},
"link": {
"type": "string"
},
"state": {
"type": "string",
"index" : "not_analyzed"
},
"title": {
"type": "string"
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
在这些步骤之后,将 River 插件配置同步 mongodb 到 elasticsearch。
希望能帮助到你..!