我在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)
)
这很好用。但目前我必须在state
字段中搜索elasticsearch
中的完全匹配项。目前,如果我搜索New York
的条目,它也会提供New Hampshire
的结果。我找到This link并看到了弹性搜索文档,我需要在数据上添加mapping
。我的问题是如何在当前场景中添加数据映射?还是有更好的方法吗?
答案 0 :(得分:4)
删除现有索引
curl -XDELETE "http://hostname:9200/index/type"
删除现有的河流配置索引
curl -XDELETE "http://hostname:9200/_river"
创建映射到索引
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"
}
}
}
}'
完成这些步骤后,将河流插件配置同步mongodb到elasticsearch。
它有帮助......!