• ElasticSearch


    match vs term

    这个问题来自stackoverflow

    https://stackoverflow.com/questions/23150670/elasticsearch-match-vs-term-query

    首先还原一下这个场景

    创建索引test,含有一个_doc类型

    PUT /test
    {
        "mappings" : {
            "_doc" : {
                "properties" : {
                    "field1" : {
                        "type" : "text",
                        "analyzer" : "standard"
                    }
                }
            }
        }
    }
    

    索引一个_doc类型的文档到test

    POST /test/_doc
    {
        "field1": "GET"
    }
    

    通过match查找GET,可以找到结果

    GET /test/_doc/_search
    {
        "query": {
            "bool": {
                "must": [
                    {"match": {"field1": "GET"}}
                ]
            }
        }
    }
    

    通过term查找GET,找不到结果

    GET /test/_doc/_search
    {
        "query": {
            "bool": {
                "must": [
                    {"term": {"field1": "GET"}}
                ]
            }
        }
    }
    

    Question

    使用match查询request.method:GET

    {
      "query": {
        "filtered": {
          "query": {
            "match": {
              "request.method": "GET"
            }
          },
          "filter": {
            "bool": {
              "must": [
    ...
    

    match查询可以拿到结果,但问题是当使用term来进行查询时没有任何结果

    {
      "query": {
        "filtered": {
          "query": {
            "term": {
              "request.method": "GET"
            }
          },
          "filter": {
            "bool": {
              "must": [
    ...
    

    ANSWER

    可能使用了Standard Analyzer,在对文档进行索引的时候GET变成了get,而文档的_source依然是GET

    GET /_analyze 
    {
      "analyzer": "standard", 
      "text": "GET"
    }
    
    # response
    # 可以看到token是get
    {
      "tokens": [
        {
          "token": "get",
          "start_offset": 0,
          "end_offset": 3,
          "type": "<ALPHANUM>",
          "position": 0
        }
      ]
    }
    

    match查询将会对搜索的句子应用Standard Analyzer,即搜索中的GET会变成get,那么就会命中文档。而term查询并不会对搜索的内容进行分析,因此会直接查找get,那么就找不到该文档。

    如果想要term查询可以生效,那么可以:

    • 将搜索中的GET变为小写的get
    • 修改request.method字段的类型为not_analyzed
    • 修改request.method字段的类型为keyword

    官方文档的一些说明

    ElasticSearch官方文档对matchterm的说明

    match 接受文本/数字/日期,并分析它们

    term 根据提供的确切值查找文本


  • 相关阅读:
    ifcfgens33 xsync async
    MySQL 忘记 root密码 两种 处理方法
    MySQL 用户 权限管理 按需分配 mysql 权限
    ip
    Mac设置su root密码
    odbc 通过 sql server 客户端 连接 mysql
    MySQL 忘记 root密码 处理方法
    mysql 查询缓存 相关变量 参数
    windows 如何 远程桌面 连接 mac
    图像清晰度的评价指标
  • 原文地址:https://www.cnblogs.com/shiyu404/p/10570653.html
Copyright © 2020-2023  润新知