我ElasticSearch在一个网站中使用,其中我从中索引了数据MongoDB。
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也会给出的结果NewHampshire。我找到了此链接,并且看到了需要mapping在数据上添加的elasticsearch文档。我的问题是如何在当前情况下在数据上添加映射?还是有更好的方法呢?
state
elasticsearch
New York
NewHampshire
mapping
删除现有索引
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" } } } }'
完成这些步骤后,将river插件配置同步mongodb到elasticsearch。
希望能帮助到你..!