gpt4 book ai didi

mongodb - 如何在elasticsearch中索引时进行映射

转载 作者:行者123 更新时间:2023-12-01 19:17:35 27 4
gpt4 key购买 nike

我在网站中使用 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 中的完全匹配项。目前,如果我搜索纽约的条目,它也会给出新罕布什尔州的结果。我找到了This link并看到elasticsearch文档,我需要在数据上添加映射。我的问题是如何在当前场景中添加数据映射?或者有什么更好的方法吗?

最佳答案

删除现有索引

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。

希望有帮助..!

关于mongodb - 如何在elasticsearch中索引时进行映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23238972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com