ElasticSearch关于term&terms搜索大小写问题

最近在es使用term查询是,发现查询结果一直为空

GET /movies/_doc/100

结果:
{
  "_index" : "movies",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 1,
  "_seq_no" : 237,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "City Hall",
    "@version" : "1",
    "genre" : [
      "Drama",
      "Thriller"
    ],
    "year" : 1996,
    "id" : "100"
  }

}

然后在使用term进行查询时

GET /movies/_search
{
  "query": {
    "term": {
      "title": {
        "value": "City"
      }
    }
  }
}

结果:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

What?明明有包含city单词的title,为什么查询结果为null?(title是text类型)
然后去查询了官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.8/query-dsl-term-query.html#query-dsl-term-query

文档开头直接给出warnning:

  • 1.避免使用term搜索text类型字段
  • 2.es会改变text字段的分词结果
  • 3.使用match替代term
    接着往下看:

    默认情况下,es使用standard analyzertext字段进行处理
  • 移除标点符号
  • 将内容拆分为单词,称为token
  • token转小写
    那么上面使用term搜索City结果为空的原因找到了。如果把term搜索内容转为小写,就可以搜索到内容了
GET /movies/_search
{
  "query": {
    "term": {
      "title": {
        "value": "city"
      }
    }
  }
}

posted @ 2022-11-21 09:44  大数据小码农  阅读(662)  评论(0编辑  收藏  举报