01
09
08
38
为了账号安全,请及时绑定邮箱和手机立即绑定

同义词分析器无法使用 python 进行弹性搜索

同义词分析器无法使用 python 进行弹性搜索

至尊宝的传说 2021-06-30 09:49:16
我在 python 代码中有一个如下所示的场景。在此,我试图明确地将 new york 和 ny 定义为同义词。但不幸的是它不起作用。你能指导我吗,因为我是弹性搜索的新手。我也在使用自定义分析器。我也有包含文本的文件同义词.txt:ny,newyork,nyorkfrom datetime import datetimefrom elasticsearch import Elasticsearches = Elasticsearch()keywords = ['thousand eyes', 'facebook', 'superdoc', 'quora', 'your story', 'Surgery', 'lending club', 'ad roll',            'the honest company', 'Draft kings', 'newyork']count = 1doc_setting = {    "settings": {        "analysis": {            "analyzer": {                "my_analyzer_keyword": {                    "type": "custom",                    "tokenizer": "keyword",                    "filter": [                        "asciifolding",                        "lowercase",                        "synonym"                    ]                },                "my_analyzer_shingle": {                    "type": "custom",                    "tokenizer": "standard",                    "filter": [                        "asciifolding",                        "lowercase",                        "synonym"                    ]                }            },            "filter": {                "synonym": {                    "type": "synonym",                    "synonyms_path": "synonyms.txt",                    "ignore_case": "true"                }            }        }    }, "mappings": {        "your_type": {            "properties": {                "keyword": {                    "type": "string",                    "index_analyzer": "my_analyzer_keyword",                    "search_analyzer": "my_analyzer_shingle"                }            }        }    }}
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

PUT /test_index


{

    "settings": {

        "analysis": {

            "analyzer": {

                "my_analyzer_keyword": {

                    "type": "custom",

                    "tokenizer": "keyword",

                    "filter": [

                        "asciifolding",

                        "lowercase",

                        "synonym"

                    ]

                },

                "my_analyzer_shingle": {

                    "type": "custom",

                    "tokenizer": "standard",

                    "filter": [

                        "asciifolding",

                        "lowercase",

                        "synonym"

                    ]

                }

            },

            "filter": {

                "synonym" : {

                        "type" : "synonym",

                        "lenient": true,

                        "synonyms" : ["ny,newyork,nyork"]

                    }

            }

        }

    }, "mappings": {

        "your_type": {

            "properties": {

                "keyword": {

                    "type": "text",

                    "analyzer": "my_analyzer_keyword",

                    "search_analyzer": "my_analyzer_shingle"

                }

            }

        }

    }

}

然后分析使用


POST /test_index/_analyze

{

    "analyzer" : "my_analyzer_shingle",

  "text" : "I saw news on ny news channel of lending club on facebook, your story and quorat"

}

我得到的令牌是


{

    "tokens": [

        {

            "token": "i",

            "start_offset": 0,

            "end_offset": 1,

            "type": "<ALPHANUM>",

            "position": 0

        },

        {

            "token": "saw",

            "start_offset": 2,

            "end_offset": 5,

            "type": "<ALPHANUM>",

            "position": 1

        },

        {

            "token": "news",

            "start_offset": 6,

            "end_offset": 10,

            "type": "<ALPHANUM>",

            "position": 2

        },

        {

            "token": "on",

            "start_offset": 11,

            "end_offset": 13,

            "type": "<ALPHANUM>",

            "position": 3

        },

        {

            "token": "ny",

            "start_offset": 14,

            "end_offset": 16,

            "type": "<ALPHANUM>",

            "position": 4

        },

        {

            "token": "newyork",

            "start_offset": 14,

            "end_offset": 16,

            "type": "SYNONYM",

            "position": 4

        },

        {

            "token": "nyork",

            "start_offset": 14,

            "end_offset": 16,

            "type": "SYNONYM",

            "position": 4

        },

        {

            "token": "news",

            "start_offset": 17,

            "end_offset": 21,

            "type": "<ALPHANUM>",

            "position": 5

        },

        {

            "token": "channel",

            "start_offset": 22,

            "end_offset": 29,

            "type": "<ALPHANUM>",

            "position": 6

        },

        {

            "token": "of",

            "start_offset": 30,

            "end_offset": 32,

            "type": "<ALPHANUM>",

            "position": 7

        },

        {

            "token": "lending",

            "start_offset": 33,

            "end_offset": 40,

            "type": "<ALPHANUM>",

            "position": 8

        },

        {

            "token": "club",

            "start_offset": 41,

            "end_offset": 45,

            "type": "<ALPHANUM>",

            "position": 9

        },

        {

            "token": "on",

            "start_offset": 46,

            "end_offset": 48,

            "type": "<ALPHANUM>",

            "position": 10

        },

        {

            "token": "facebook",

            "start_offset": 49,

            "end_offset": 57,

            "type": "<ALPHANUM>",

            "position": 11

        },

        {

            "token": "your",

            "start_offset": 59,

            "end_offset": 63,

            "type": "<ALPHANUM>",

            "position": 12

        },

        {

            "token": "story",

            "start_offset": 64,

            "end_offset": 69,

            "type": "<ALPHANUM>",

            "position": 13

        },

        {

            "token": "and",

            "start_offset": 70,

            "end_offset": 73,

            "type": "<ALPHANUM>",

            "position": 14

        },

        {

            "token": "quorat",

            "start_offset": 74,

            "end_offset": 80,

            "type": "<ALPHANUM>",

            "position": 15

        }

    ]

}

并且搜索产生


POST /test_index/_search

{

    "query" : {

        "match" : { "keyword" : "I saw news on ny news channel of lending club on facebook, your story and quora" }

    }

}


{

    "took": 36,

    "timed_out": false,

    "_shards": {

        "total": 5,

        "successful": 5,

        "skipped": 0,

        "failed": 0

    },

    "hits": {

        "total": 3,

        "max_score": 1.6858001,

        "hits": [

            {

                "_index": "test_index",

                "_type": "your_type",

                "_id": "4",

                "_score": 1.6858001,

                "_source": {

                    "keyword": "newyork"

                }

            },

            {

                "_index": "test_index",

                "_type": "your_type",

                "_id": "2",

                "_score": 1.1727304,

                "_source": {

                    "keyword": "facebook"

                }

            },

            {

                "_index": "test_index",

                "_type": "your_type",

                "_id": "5",

                "_score": 0.6931472,

                "_source": {

                    "keyword": "quora"

                }

            }

        ]

    }

}


查看完整回答
反对 回复 2021-07-13

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号