在弹性搜索索引时如何做映射 [英] How to do mapping while indexing in elasticsearch

查看:95
本文介绍了在弹性搜索索引时如何做映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ElasticSearch 在我从 MongoDB 中索引数据的网站。

I am using ElasticSearch in a website where i index data from 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)
        )

这个效果很好。但是,目前我必须在状态字段中搜索 elasticsearch 中的完全匹配项。目前,如果我搜索 New York 的条目,它也会得到 New Hampshire 的结果。我发现此链接,并看到弹性搜索文档,我需要添加对数据进行映射。我的问题是如何在当前场景中添加数据映射?或者有什么更好的方法吗?

This worked good. But currently I have to search in the state field for an exact match in elasticsearch. Currently if I search for entries for New York it also gives result of New Hampshire. I have found This link and saw elasticsearch documentation that I need to add mapping on the data. My question is how do I add a mapping on the data in the current scenario? or is there any better way of doing it?

推荐答案

删除现有索引

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到弹性搜索。

After these steps put the river plugin config sync mongodb to elasticsearch.

HOpe它有帮助..!

HOpe it helps..!

这篇关于在弹性搜索索引时如何做映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆