• 【ElasticSearch(九)进阶】Term精确数值查询,match keyword精确文本查询


    【ElasticSearch(九)进阶】Term精确数值查询,match keyword精确文本查询


    一、Term精确数值查询

    • term查询,会返回那些 在提供的字段中包含确切信息 的文档内容。

    • 查询text字段值,使用match。查询精确数值,使用term。

    • 为什么避免使用term对text字段进行查询?

      默认情况下,ES更改text字段的值作为词法分析的一部分。这会使查找text字段值的精确匹配变得困难。


    查询年龄是33岁的数据:

    GET bank/_search
    {
      "query":{
        "term":{
          "age": 33
        }
      }
    }
    

    返回结果:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 50,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "18",
            "_score" : 1.0,
            "_source" : {
              "account_number" : 18,
              "balance" : 4180,
              "firstname" : "Dale",
              "lastname" : "Adams",
              "age" : 33,
              "gender" : "M",
              "address" : "467 Hutchinson Court",
              "employer" : "Boink",
              "email" : "daleadams@boink.com",
              "city" : "Orick",
              "state" : "MD"
            }
          },
            。。。
        ]
      }
    }
    

    二、match keyword精确文本查询

    下面对比下matchmatch_phrasematch中的属性加.keyword的区别


    1.match 模糊查询文本

    会将address的文本拆分成词,只要结果中包含有任意词的文档,都可以被筛选出来。

    GET bank/_search
    {
      "query":{
        "match":{
          "address": "467 Hutchinson Court"
        }
      }
    }
    

    返回结果:

    {
      "took" : 17,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 120,
          "relation" : "eq"
        },
        "max_score" : 14.617203,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "18",
            "_score" : 14.617203,
            "_source" : {
              "account_number" : 18,
              "balance" : 4180,
              "firstname" : "Dale",
              "lastname" : "Adams",
              "age" : 33,
              "gender" : "M",
              "address" : "467 Hutchinson Court",
              "employer" : "Boink",
              "email" : "daleadams@boink.com",
              "city" : "Orick",
              "state" : "MD"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "53",
            "_score" : 5.990829,
            "_source" : {
              "account_number" : 53,
              "balance" : 28101,
              "firstname" : "Kathryn",
              "lastname" : "Payne",
              "age" : 29,
              "gender" : "F",
              "address" : "467 Louis Place",
              "employer" : "Katakana",
              "email" : "kathrynpayne@katakana.com",
              "city" : "Harviell",
              "state" : "SD"
            }
          },
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "56",
            "_score" : 2.1248586,
            "_source" : {
              "account_number" : 56,
              "balance" : 14992,
              "firstname" : "Josie",
              "lastname" : "Nelson",
              "age" : 32,
              "gender" : "M",
              "address" : "857 Tabor Court",
              "employer" : "Emtrac",
              "email" : "josienelson@emtrac.com",
              "city" : "Sunnyside",
              "state" : "UT"
            }
          },
         。。。
        ]
      }
    }
    

    2.match_phrase 短语查询

    address的文本视为一个短语,不进行文本拆分,只要结果中包含这个短语的文档,都能被筛选出来。

    GET bank/_search
    {
      "query":{
        "match_phrase":{
          "address": "467 Hutchinson"
        }
      }
    }
    

    返回结果:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 12.492344,
        "hits" : [
          {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "18",
            "_score" : 12.492344,
            "_source" : {
              "account_number" : 18,
              "balance" : 4180,
              "firstname" : "Dale",
              "lastname" : "Adams",
              "age" : 33,
              "gender" : "M",
              "address" : "467 Hutchinson Court",
              "employer" : "Boink",
              "email" : "daleadams@boink.com",
              "city" : "Orick",
              "state" : "MD"
            }
          }
        ]
      }
    }
    
    

    3.match中的属性加.keyword

    keyword精确查询,只有结果中address属性和address.keyword的值完全一致的文档,才能被筛选出来。

    结合2和3,可以发现同样的值,match_phrase 和 keyword的区别。

    GET bank/_search
    {
      "query":{
        "match":{
          "address.keyword": "467 Hutchinson"
        }
      }
    }
    

    返回结果:

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

    *我们一般规定:全文精确检索字段用 match加.keyword,其他非text字段匹配用term

  • 相关阅读:
    H5应用加固防破解-js虚拟机保护方案浅谈
    Hijack chrome browser
    端口复用正向后门
    Django框架的一些漏洞
    07_简单的LISP加减乘除(基本计算器)
    git error:invalid path问题解决(win下)
    配置win10支持文件夹内区分大小写
    win10启用自带ubuntu虚拟机并升级至wsl2
    【进程调度】关于CPU的sockets、dies、cores、threads含义理解
    06_最长回文子串长度
  • 原文地址:https://www.cnblogs.com/musecho/p/15179997.html
Copyright © 2020-2023  润新知