http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 11917|回复: 0

[Elasticsearch] Elasticsearch-查询(二)match 与 match_phrase 查询

[复制链接]
发表于 2021-6-17 18:38:09 | 显示全部楼层 |阅读模式
在Elasticsearch查询中, 经常用到 match 与 match_phrase 查询, 下面我们说下两个查询方式的用法
在我们的索引用,有一个test, 里面存储了部分学生信息, 下面我们用关键字“He is”进行检索

POST /test/_doc/_search
[C#] 纯文本查看 复制代码
{
  "query": {
    "match": {
      "description": "He is"
    }
  }


执行查询,得到结果如下, 得到4条数据:
[C#] 纯文本查看 复制代码
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 0.2169777,
      "hits": [
         {
            "_index": "test",
            "_type": "student",
            "_id": "2",
            "_score": 0.2169777,
            "_source": {
               "name": "februus",
               "sex": "male",
               "age": 24,
               "description": "He is passionate.",
               "interests": "reading, programing"
            }
         },
         {
            "_index": "test",
            "_type": "student",
            "_id": "1",
            "_score": 0.16273327,
            "_source": {
               "name": "leotse",
               "sex": "male",
               "age": 25,
               "description": "He is a big data engineer.",
               "interests": "reading, swiming, hiking"
            }
         },
         {
            "_index": "test",
            "_type": "student",
            "_id": "4",
            "_score": 0.01989093,
            "_source": {
               "name": "pascal",
               "sex": "male",
               "age": 25,
               "description": "He works very hard because he wanna go to Canada.",
               "interests": "programing, reading"
            }
         },
         {
            "_index": "test",
            "_type": "student",
            "_id": "3",
            "_score": 0.016878016,
            "_source": {
               "name": "yolovon",
               "sex": "female",
               "age": 24,
               "description": "She is so charming and beautiful.",
               "interests": "reading, shopping"
            }
         }
      ]
   }
}


实用match_phrase 进行检索:
POST /test/_doc/_search
[C#] 纯文本查看 复制代码
{
  "query": {
    "match_phrase": {
      "description": "He is"
    }
  }
}

得到如下2条结果:
[C#] 纯文本查看 复制代码
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.30685282,
      "hits": [
         {
            "_index": "test",
            "_type": "student",
            "_id": "2",
            "_score": 0.30685282,
            "_source": {
               "name": "februus",
               "sex": "male",
               "age": 24,
               "description": "He is passionate.",
               "interests": "reading, programing"
            }
         },
         {
            "_index": "test",
            "_type": "student",
            "_id": "1",
            "_score": 0.23013961,
            "_source": {
               "name": "leotse",
               "sex": "male",
               "age": 25,
               "description": "He is a big data engineer.",
               "interests": "reading, swiming, hiking"
            }
         }
      ]
   }
}


通过上面的查询条件, 可以清晰的看到, 两则得到的数据结果是不一样的, 分析得到:
1. 对于match的结果,我们可以可以看到,结果的Document中description这个field可以包含“He is”,“He”或者“is”;
2. match_phrased的结果中的description字段,必须包含“He is”这一个词组;

通过上面分析就可以看到, match查询,会将词进行拆分, 只要包含相关Document, 都会出现在最终的结果集中

那么,如果我们不想将我们的查询条件拆分,应该怎么办呢?这时候我们就可以使用match_phrase

match_phrase是短语搜索,亦即它会将给定的短语(phrase)当成一个完整的查询条件。当使用match_phrase进行搜索的时候,你的结果集中,所有的Document都必须包含你指定的查询词组,在这里是“He is”。这看起来有点像关系型数据库的like查询操作。


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 06:00

© 2014-2021

快速回复 返回顶部 返回列表