【Elasticsearch】Elasticsearch检索方式全剖析:从基础到实战(一) ...

打印 上一主题 下一主题

主题 912|帖子 912|积分 2740

引言

在大数据时代,高效的数据检索能力是众多应用体系的关键需求。Elasticsearch作为一款强盛的开源分布式搜刮和分析引擎,提供了两种根本的检索方式,帮助开发者从海量数据中精准获取所需信息。这两种检索方式各有特点,适用于差别的业务场景和查询需求。本文将深入探讨这两种检索方式,并通过丰富的示例和详细的表明,帮助读者全面掌握Elasticsearch的检索技巧。
数据准备:数据JSON
Elasticsearch检索方式概述

两种检索方式先容

Elasticsearch支持通过REST request uri发送搜刮参数和通过REST request body发送搜刮参数这两种根本检索方式。明白这两种方式的差别和适用场景,是高效使用Elasticsearch的基础。
方式一:通过REST request uri发送搜刮参数


  • 原理:将搜刮参数以查询字符串的形式直接附加在URI后面,传递给Elasticsearch服务器。这种方式简朴直观,适用于简朴的搜刮场景。
  • 示例
  1. GET bank/_search?q=*&sort=account_number:asc
复制代码

  • 参数表明

    • q=*:q代表查询条件,*是通配符,表示查询所有文档。
    • sort=account_number:asc:sort用于指定排序规则,这里表示按照account_number字段进行升序分列,asc表示升序,desc表示降序。

  • 返回结果分析
  1. {
  2.   "took" : 235,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1000,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : null,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "0",
  21.         "_score" : null,
  22.         "_source" : {
  23.           "account_number" : 0,
  24.           "balance" : 16623,
  25.           "firstname" : "Bradshaw",
  26.           "lastname" : "Mckenzie",
  27.           "age" : 29,
  28.           "gender" : "F",
  29.           "address" : "244 Columbus Place",
  30.           "employer" : "Euron",
  31.           "email" : "bradshawmckenzie@euron.com",
  32.           "city" : "Hobucken",
  33.           "state" : "CO"
  34.         },
  35.         "sort" : [
  36.           0
  37.         ]
  38.       },
  39.       // 此处省略其他文档数据
  40.     ]
  41.   }
  42. }
复制代码
  1. - `took`:表示Elasticsearch执行查询所花费的时间,单位为毫秒,这里是235毫秒,反映了查询的执行效率。
  2. - `timed_out`:表示搜索请求是否超时,`false`表示未超时,说明查询在规定时间内顺利完成。
  3. - `_shards`:包含搜索的分片信息,`total`表示总分片数,`successful`表示成功搜索的分片数,`skipped`表示跳过的分片数,`failed`表示搜索失败的分片数。这里总分片数为1,且成功搜索了1个分片,说明搜索过程顺利。
  4. - `hits.total.value`:表示找到的匹配文档数量,这里是1000,说明在`bank`索引中共有1000个文档符合查询条件(因为这里是查询所有文档)。
  5. - `max_score`:表示文档的最高相关性得分,由于使用`match_all`查询所有文档,没有相关性得分的概念,所以为`null`。
  6. - `hits.sort`:表示文档的排序位置(当不按相关性得分排序时),这里按照`account_number`升序排列,所以每个文档的`sort`值就是其`account_number`的值。
  7. - `hits._score`:表示文档的相关性得分(使用`match_all`时不适用),这里为`null`。
复制代码
方式二:通过REST request body发送搜刮参数

原理:将搜刮参数放在HTTP请求的消息体中发送给Elasticsearch服务器,使用的是一种领域对象语言(DSL),以JSON格式来定义复杂的查询条件、排序规则、分页设置等。这种方式灵活性高,可以或许满足复杂的搜刮需求。
(1)根本语法格式

Elasticsearch提供了一个可以实验查询的Json风格的DSL。这个被称为Query DSL,该查询语言非常全面。
一个查询语句的典型布局
  1. QUERY_NAME:{
  2.    ARGUMENT:VALUE,
  3.    ARGUMENT:VALUE,...
  4. }
复制代码
如果针对于某个字段,那么它的布局如下:
  1. {
  2.   QUERY_NAME:{
  3.      FIELD_NAME:{
  4.        ARGUMENT:VALUE,
  5.        ARGUMENT:VALUE,...
  6.       }   
  7.    }
  8. }
复制代码
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_all": {}
  5.   },
  6.   "from": 0,
  7.   "size": 5,
  8.   "sort": [
  9.     {
  10.       "account_number": {
  11.         "order": "desc"
  12.       }
  13.     }
  14.   ]
  15. }
  16. //match_al查询所有,从第0个数据拿5个数据
复制代码
query定义如何查询;


  • match_all查询类型【代表查询所有的所有】,es中可以在query中组合非常多的查询类型完成复杂查询;
  • 除了query参数之外,我们可也传递其他的参数以改变查询结果,如sort,size;
  • from+size限定,完身分页功能;
  • sort排序,多字段排序,会在前序字段相等时后续字段内部排序,否则以前序为准;
(2)返回部分字段

  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_all": {}
  5.   },
  6.   "from": 0,
  7.   "size": 5,
  8.   "sort": [
  9.     {
  10.       "account_number": {
  11.         "order": "desc"
  12.       }
  13.     }
  14.   ],
  15.   "_source": ["balance","firstname"]
  16.   
  17. }
复制代码
查询结果:
  1. {
  2.   "took" : 18,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1000,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : null,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "999",
  21.         "_score" : null,
  22.         "_source" : {
  23.           "firstname" : "Dorothy",
  24.           "balance" : 6087
  25.         },
  26.         "sort" : [
  27.           999
  28.         ]
  29.       },
  30.       {
  31.         "_index" : "bank",
  32.         "_type" : "account",
  33.         "_id" : "998",
  34.         "_score" : null,
  35.         "_source" : {
  36.           "firstname" : "Letha",
  37.           "balance" : 16869
  38.         },
  39.         "sort" : [
  40.           998
  41.         ]
  42.       },
  43.       {
  44.         "_index" : "bank",
  45.         "_type" : "account",
  46.         "_id" : "997",
  47.         "_score" : null,
  48.         "_source" : {
  49.           "firstname" : "Combs",
  50.           "balance" : 25311
  51.         },
  52.         "sort" : [
  53.           997
  54.         ]
  55.       },
  56.       {
  57.         "_index" : "bank",
  58.         "_type" : "account",
  59.         "_id" : "996",
  60.         "_score" : null,
  61.         "_source" : {
  62.           "firstname" : "Andrews",
  63.           "balance" : 17541
  64.         },
  65.         "sort" : [
  66.           996
  67.         ]
  68.       },
  69.       {
  70.         "_index" : "bank",
  71.         "_type" : "account",
  72.         "_id" : "995",
  73.         "_score" : null,
  74.         "_source" : {
  75.           "firstname" : "Phelps",
  76.           "balance" : 21153
  77.         },
  78.         "sort" : [
  79.           995
  80.         ]
  81.       }
  82.     ]
  83.   }
  84. }
复制代码
(3)match匹配查询



  • 根本类型(非字符串),“account_number”: 20 可加可不加“ ” 不加就是精确匹配
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "account_number": "20"
  6.     }
  7.   }
  8. }
复制代码
match返回account_number=20的数据。
查询结果:
  1. {
  2.   "took" : 1,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 1.0,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "20",
  21.         "_score" : 1.0,
  22.         "_source" : {
  23.           "account_number" : 20,
  24.           "balance" : 16418,
  25.           "firstname" : "Elinor",
  26.           "lastname" : "Ratliff",
  27.           "age" : 36,
  28.           "gender" : "M",
  29.           "address" : "282 Kings Place",
  30.           "employer" : "Scentric",
  31.           "email" : "elinorratliff@scentric.com",
  32.           "city" : "Ribera",
  33.           "state" : "WA"
  34.         }
  35.       }
  36.     ]
  37.   }
  38. }
复制代码


  • 字符串,全文检索“ ” 含糊查询
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address": "kings"
  6.     }
  7.   }
  8. }
复制代码
全文检索,最终会按照评分进行排序,会对检索条件进行分词匹配。
查询结果:
  1. {
  2.   "took" : 30,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 2,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 5.990829,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "20",
  21.         "_score" : 5.990829,
  22.         "_source" : {
  23.           "account_number" : 20,
  24.           "balance" : 16418,
  25.           "firstname" : "Elinor",
  26.           "lastname" : "Ratliff",
  27.           "age" : 36,
  28.           "gender" : "M",
  29.           "address" : "282 Kings Place",
  30.           "employer" : "Scentric",
  31.           "email" : "elinorratliff@scentric.com",
  32.           "city" : "Ribera",
  33.           "state" : "WA"
  34.         }
  35.       },
  36.       {
  37.         "_index" : "bank",
  38.         "_type" : "account",
  39.         "_id" : "722",
  40.         "_score" : 5.990829,
  41.         "_source" : {
  42.           "account_number" : 722,
  43.           "balance" : 27256,
  44.           "firstname" : "Roberts",
  45.           "lastname" : "Beasley",
  46.           "age" : 34,
  47.           "gender" : "F",
  48.           "address" : "305 Kings Hwy",
  49.           "employer" : "Quintity",
  50.           "email" : "robertsbeasley@quintity.com",
  51.           "city" : "Hayden",
  52.           "state" : "PA"
  53.         }
  54.       }
  55.     ]
  56.   }
  57. }
复制代码
(4) match_phrase [短句匹配]

将必要匹配的值当成一整个单词(不分词)进行检索
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_phrase": {
  5.       "address": "mill road"
  6.     }
  7.   }
  8. }
复制代码
查处address中包罗mill_road的所有记录,并给出相干性得分
检察结果:
  1. {
  2.   "took" : 32,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 8.926605,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "970",
  21.         "_score" : 8.926605,
  22.         "_source" : {
  23.           "account_number" : 970,
  24.           "balance" : 19648,
  25.           "firstname" : "Forbes",
  26.           "lastname" : "Wallace",
  27.           "age" : 28,
  28.           "gender" : "M",
  29.           "address" : "990 Mill Road",
  30.           "employer" : "Pheast",
  31.           "email" : "forbeswallace@pheast.com",
  32.           "city" : "Lopezo",
  33.           "state" : "AK"
  34.         }
  35.       }
  36.     ]
  37.   }
  38. }
复制代码
match_phrase和match的区别,观察如下实例:
match_phrase是做短语匹配
match是分词匹配,例如990 Mill匹配含有990大概Mill的结果
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match_phrase": {
  5.       "address": "990 Mill"
  6.     }
  7.   }
  8. }
复制代码
查询结果:
  1. {
  2.   "took" : 0,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 10.806405,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "970",
  21.         "_score" : 10.806405,
  22.         "_source" : {
  23.           "account_number" : 970,
  24.           "balance" : 19648,
  25.           "firstname" : "Forbes",
  26.           "lastname" : "Wallace",
  27.           "age" : 28,
  28.           "gender" : "M",
  29.           "address" : "990 Mill Road",
  30.           "employer" : "Pheast",
  31.           "email" : "forbeswallace@pheast.com",
  32.           "city" : "Lopezo",
  33.           "state" : "AK"
  34.         }
  35.       }
  36.     ]
  37.   }
  38. }
复制代码
使用match的keyword
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address.keyword": "990 Mill"
  6.     }
  7.   }
  8. }
复制代码
查询结果,一条也未匹配到
  1. {
  2.   "took" : 0,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 0,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : null,
  16.     "hits" : [ ]
  17.   }
  18. }
复制代码
修改匹配条件为“990 Mill Road”
  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address.keyword": "990 Mill Road"
  6.     }
  7.   }
  8. }
复制代码
查询出一条数据
  1. {
  2.   "took" : 1,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 1,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 6.5032897,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "970",
  21.         "_score" : 6.5032897,
  22.         "_source" : {
  23.           "account_number" : 970,
  24.           "balance" : 19648,
  25.           "firstname" : "Forbes",
  26.           "lastname" : "Wallace",
  27.           "age" : 28,
  28.           "gender" : "M",
  29.           "address" : "990 Mill Road",
  30.           "employer" : "Pheast",
  31.           "email" : "forbeswallace@pheast.com",
  32.           "city" : "Lopezo",
  33.           "state" : "AK"
  34.         }
  35.       }
  36.     ]
  37.   }
  38. }
复制代码
文本字段的匹配,使用keyword,匹配的条件就是要表现字段的全部值,要进行精确匹配的。
match_phrase是做短语匹配,只要文本中包罗匹配条件既包罗这个短语,就能匹配到。
(5)multi_math【多字段匹配】

  1. GET bank/_search
  2. {
  3.   "query": {
  4.     "multi_match": {
  5.       "query": "mill",
  6.       "fields": [
  7.         "state",
  8.         "address"
  9.       ]
  10.     }
  11.   }
  12. }
复制代码
state大概address中包罗mill,而且在查询过程中,会对于查询条件进行分词。
查询结果:
  1. {
  2.   "took" : 28,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 4,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 5.4032025,
  16.     "hits" : [
  17.       {
  18.         "_index" : "bank",
  19.         "_type" : "account",
  20.         "_id" : "970",
  21.         "_score" : 5.4032025,
  22.         "_source" : {
  23.           "account_number" : 970,
  24.           "balance" : 19648,
  25.           "firstname" : "Forbes",
  26.           "lastname" : "Wallace",
  27.           "age" : 28,
  28.           "gender" : "M",
  29.           "address" : "990 Mill Road",
  30.           "employer" : "Pheast",
  31.           "email" : "forbeswallace@pheast.com",
  32.           "city" : "Lopezo",
  33.           "state" : "AK"
  34.         }
  35.       },
  36.       {
  37.         "_index" : "bank",
  38.         "_type" : "account",
  39.         "_id" : "136",
  40.         "_score" : 5.4032025,
  41.         "_source" : {
  42.           "account_number" : 136,
  43.           "balance" : 45801,
  44.           "firstname" : "Winnie",
  45.           "lastname" : "Holland",
  46.           "age" : 38,
  47.           "gender" : "M",
  48.           "address" : "198 Mill Lane",
  49.           "employer" : "Neteria",
  50.           "email" : "winnieholland@neteria.com",
  51.           "city" : "Urie",
  52.           "state" : "IL"
  53.         }
  54.       },
  55.       {
  56.         "_index" : "bank",
  57.         "_type" : "account",
  58.         "_id" : "345",
  59.         "_score" : 5.4032025,
  60.         "_source" : {
  61.           "account_number" : 345,
  62.           "balance" : 9812,
  63.           "firstname" : "Parker",
  64.           "lastname" : "Hines",
  65.           "age" : 38,
  66.           "gender" : "M",
  67.           "address" : "715 Mill Avenue",
  68.           "employer" : "Baluba",
  69.           "email" : "parkerhines@baluba.com",
  70.           "city" : "Blackgum",
  71.           "state" : "KY"
  72.         }
  73.       },
  74.       {
  75.         "_index" : "bank",
  76.         "_type" : "account",
  77.         "_id" : "472",
  78.         "_score" : 5.4032025,
  79.         "_source" : {
  80.           "account_number" : 472,
  81.           "balance" : 25571,
  82.           "firstname" : "Lee",
  83.           "lastname" : "Long",
  84.           "age" : 32,
  85.           "gender" : "F",
  86.           "address" : "288 Mill Street",
  87.           "employer" : "Comverges",
  88.           "email" : "leelong@comverges.com",
  89.           "city" : "Movico",
  90.           "state" : "MT"
  91.         }
  92.       }
  93.     ]
  94.   }
  95. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王國慶

金牌会员
这个人很懒什么都没写!

标签云

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