• ES 关于text和keyword两种类型数据搜索区别


    ElasticSearch 5.0以后,string类型有重大变更,移除了string类型,

    string字段被拆分成两种新的数据类型: text用于全文搜索的,而keyword用于关键词搜索。

     

     1、ES 关于text和keyword两种类型

    ElasticSearch字符串将默认被同时映射成text和keyword类型,将会自动创建下面的动态映射。

    通过 GET /dist_test/_mapping/field/weixin_number 命令查看 weixin_number 字段数据类型,可以看到 mapping 下除了 text 还有 keyword。两者有什么区别呢? 

    1. {
    2. "dist_test" : {
    3. "mappings" : {
    4. "weixin" : {
    5. "weixin_number" : {
    6. "full_name" : "weixin_number",
    7. "mapping" : {
    8. "weixin_number" : {
    9. "type" : "text",
    10. "fields" : {
    11. "keyword" : {
    12. "type" : "keyword",
    13. "ignore_above" : 256
    14. }
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }
    22. }

     1.1 text:

    1. 会分词,然后进行索引
    2. 支持模糊、精确查询
    3. 不支持聚合
    4. 分词器默认standard ,对于中文来说就是按字分词
    5. 支持fields属性,可以在fields中添加keyword子类型,以实现精确检索

    1.2 keyword:

    1. 不进行分词,直接索引
    2. 支持模糊、精确查询
    3. 支持聚合
    4. 支持按字数建立索引,以便节约索引空间
    5. 看下text分词规律。

    1.2.1、纯小写字符串

    1. GET /dist_test/_analyze
    2. {
    3. "field": "weixin_number",
    4. "text": "vicky1176320626"
    5. }

    1.2.2、包含大写字母字符串会全部转换为小写。

    1. GET /dist_test/_analyze
    2. {
    3. "field": "weixin_number",
    4. "text": "XP3412"
    5. }

    1.2.3、包含特殊符号,如空格、-,则会分词为多个字符串。

    1. GET /dist_test/_analyze
    2. {
    3. "field": "weixin_number",
    4. "text": "AAAA-95533"
    5. }

    这也是某些情况下,term 查询后查询不到数据的原因。解决方案也很简单,使用 keyword 精确查询即可。

    如例:针对Text类型的数据,要使用term查询

    1. {
    2. "from": 0,
    3. "size": 10,
    4. "query": {
    5. "bool": {
    6. "must": [
    7. {
    8. "term": {
    9. "city.keyword": {
    10. "value": "湖南省长沙市",
    11. "boost": 1
    12. }
    13. }
    14. }
    15. ],
    16. "adjust_pure_negative": true,
    17. "boost": 1
    18. }
    19. }
    20. }

  • 相关阅读:
    CS50_AI_2_Uncertainty 概率 (Python实现 - 英文注释)
    熊猫烧香病毒,当年究竟有多可怕?
    行人属性识别二:添加新网络训练和自定义数据集训练
    LeetCode刷题---无重复字符的最长子串
    Oracle.xs.dll‘ for module DBD::Oracle: load_file:找不到指定的模块
    华为OD 社招(Java后端)一面
    小白入门大模型的第一课
    【小程序源码】经典语录大全多种分类语录
    win操作系统切换窗口
    k8s部署-kuboard安装(工具kuboard-spary)
  • 原文地址:https://blog.csdn.net/yexiaomodemo/article/details/126941437