当前位置:网站首页>ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型

ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型

2022-06-24 13:04:00 loong_XL

参考:https://blog.csdn.net/winterking3/article/details/108254346
https://blog.csdn.net/tclzsn7456/article/details/79956625

1、ES mapping之keyword

默认mapping结构一般是:
如果不设置mapping,ES默认把字符串设为text类型,并包含一个keyword子类型。
在这里插入图片描述
***这种结构保存字段会存两份索引(个人理解),首先第一个type text这个会进行分词建索引保存,再后面fields keyword会进行保存完整字符串附加

term查询

1)直接term
(结构查不到,这是因为不加任何默认是text类型,这个字段保存是有分词,比如电影分成了电和影两个字保存)

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "channel": "电影"
          }
        }
      ]
    }
  }
}

索引这里改成单字查询可以查询到


{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "channel": "电"
          }
        }
      ]
    }
  }
}
##或

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "channel": "影"
          }
        }
      ]
    }
  }
}

或者用match查询,这会进行分词后再查询


{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "channel": "电影"
          }
        }
      ]
    }
  }
}

2)具体全匹配查询term+keywod
这种查询会依据mapping里子类型keyword字段匹配,必须包含查询字符串不分词,相当于是查询的不分词前的原来字符串

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "channel.keyword": "电影"
          }
        }
      ]
    }
  }
}

3、更改mapping keyword类型

直接去掉text那一级,吧text直接改成keyword,这种皆可以直接进行term查询进行不分词全匹配了,不用再加keyword

#设置channel为keyword
PUT pigg_user
{
  "mappings": {
    "properties": {
      "channel": {
        "type":  "keyword"
      },
      "age": {
        "type": "short"
      }
    }
  }
}

原网站

版权声明
本文为[loong_XL]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42357472/article/details/125439925