elasticsearch IK 分词怎么无效呀?

2020-05-20 23:06:01 +08:00
 dyllen

index test的 mapping 定义:

"content": {
	"type": "text",
	"analyzer": "ik_smart"
},
"title": {
	"type": "text",
	"analyzer": "ik_smart"
}

测试分词:

http://127.0.0.1:9200/_analyze

提交参数:

{
    "text": "中国美国英国",
    "analyzer": "ik_smart"
}

返回

{
    "tokens": [
        {
            "token": "中国",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "美国",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "英国",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 2
        }
    ]
}

_search 测试一下索引 test

get body

{
    "size": 20,
    "query": {
        "match": {
            "content": "广州人"
        }
    }
}

返回:

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.84268904,
        "hits": [
            {
                "_index": "dcc-speechcrafts",
                "_type": "dcc-speechcraft",
                "_id": "AXIyjmXuVhRXxkRgwNlT",
                "_score": 0.84268904,
                "_source": {
                    "title": "",
                    "content": "qefdygyrfh 广州人"
                }
            }
        ]
    }
}

第二次_search 测试一下

get body

{
    "size": 20,
    "query": {
        "match": {
            "content": "广州"
        }
    }
}

返回:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

问题,我那条记录内容包含广州人这三个字,为什么分别用广州人广州两个词去查询,一次有结果,一次没结果呀?按道理用广州去查询应该也是返回一样的结果的呀,这什么问题?

elasticsearch 5.6.16

2945 次点击
所在节点    Elasticsearch
6 条回复
fancy967
2020-05-20 23:50:03 +08:00
没有研究过 ik_smart 这个 analyzer,不过把广州人、qefdygyrfh 广州人和广州这个三个词放进_analyze 测试一下看返回的 token 能不能匹配上吗不就知道原因了吗
misaka19000
2020-05-20 23:53:06 +08:00
是不是人匹配了但是广州没匹配
CoolSpring
2020-05-21 08:58:14 +08:00
https://github.com/medcl/elasticsearch-analysis-ik
引用一下
“ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query ;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。”

这里的问题应该是 ik_smart 在生成索引时只分出了“广州人”一个词,而根据倒排索引的原理用“广州”就搜不到了。
网络上有一些文章的建议是,索引时用 ik_max_word,搜索时用 ik_smart 。(不过也有其他的坑例如 https://github.com/medcl/elasticsearch-analysis-ik/issues/584
dyllen
2020-05-21 09:52:25 +08:00
@misaka19000 我用_analyze 测试了 ik_smart 分词,广州和广州人不会再分,就一个词
dyllen
2020-05-21 09:53:22 +08:00
@CoolSpring 听你这样一说,我好像有点明白了,先去试试先。
dyllen
2020-05-21 10:14:00 +08:00
@fancy967
@dyllen

明白了,还是不太了解 elasticsearch 导致的,设置成了索引时用 ik_max_word,搜索时用 ik_smart 。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/673802

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX