ElasticSearch学习笔记 | Match和Match_phrase匹配搜索
发布日期:2021-07-28 08:29:50 浏览次数:2 分类:技术文章

本文共 2791 字,大约阅读时间需要 9 分钟。

本文测试数据为官方提供的测试数据,导入方法在学习笔记本章节第一篇中:https://blog.csdn.net/qq_20051535/article/details/113242821

一、基本类型(非字符串),精确匹配

查询 account_number 是 20 的所有结果:

GET bank/_search

{
  "query": {
    "match": {
      "account_number": 20
    }
  }
}
返回内容:

{

  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "20",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 20,
          "balance" : 16418,
          "firstname" : "Elinor",
          "lastname" : "Ratliff",
          "age" : 36,
          "gender" : "M",
          "address" : "282 Kings Place",
          "employer" : "Scentric",
          "email" : "elinorratliff@scentric.com",
          "city" : "Ribera",
          "state" : "WA"
        }
      }
    ]
  }
}
二、字符串模糊匹配查询
比如我们希望查询所有 address 中包含 Kings 的数据:

GET bank/_search

{
  "query": {
    "match": {
      "address": "Kings"
    }
  }
}
返回结果:

可以看到 “282 Kings Place” 和 “305 Kings Hwy” 两条记录都返回了

全文检索会按照评分进行排序,会对检索条件进行分词匹配。

{

  "took" : 157,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 5.990829,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "20",
        "_score" : 5.990829,
        "_source" : {
          "account_number" : 20,
          "balance" : 16418,
          "firstname" : "Elinor",
          "lastname" : "Ratliff",
          "age" : 36,
          "gender" : "M",
          "address" : "282 Kings Place",
          "employer" : "Scentric",
          "email" : "elinorratliff@scentric.com",
          "city" : "Ribera",
          "state" : "WA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "722",
        "_score" : 5.990829,
        "_source" : {
          "account_number" : 722,
          "balance" : 27256,
          "firstname" : "Roberts",
          "lastname" : "Beasley",
          "age" : 34,
          "gender" : "F",
          "address" : "305 Kings Hwy",
          "employer" : "Quintity",
          "email" : "robertsbeasley@quintity.com",
          "city" : "Hayden",
          "state" : "PA"
        }
      }
    ]
  }
}
三、Match_phrase 短语匹配
默认的match搜索会对搜索内容进行分词,比如:mill lane 会分成 mill 和 lane 之后搜索的结果可能包含仅有其中一项的结果,但是此类结果分数较低。

如果不希望被分词可以使用 match_phrase 进行搜索

例子:

查询 地址 包含 mill lane 的结果:

GET bank/_search

{
  "query": {
    "match_phrase": {
      "address": "Mill Lane"
    }
  }
}
返回结果:

{

  "took" : 439,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 9.507477,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 9.507477,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland#neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      }
    ]
  }
}
 
————————————————
版权声明:本文为CSDN博主「北鹤M」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_20051535/article/details/113244550

转载地址:https://blog.csdn.net/sinat_17775997/article/details/116132502 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ElasticSearch学习笔记 | Multi_match多字段匹配、bool复合查询
下一篇:ElasticSearch 官网测试数据

发表评论

最新留言

很好
[***.229.124.182]2024年04月08日 23时55分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章