商城项目17_es的DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示

Posted 所得皆惊喜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了商城项目17_es的DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示相关的知识,希望对你有一定的参考价值。

文章目录

①. DSL领域特定语言

  • ①. Elasticsearch提供了一个可以执行查询的Json风格的DSL(domain-specific language领域特定语言)。这个被称为Query DSL,该查询语言非常全面

  • ②. 基本语法格式(一个查询语句的典型结构)

如果针对于某个字段,那么它的结构如下:

  QUERY_NAME:   # 使用的功能
     FIELD_NAME:  #  功能参数
       ARGUMENT:VALUE,
       ARGUMENT:VALUE,...
         
   

示例  使用时不要加#注释内容
GET bank/_search

  "query":   #  查询的字段
    "match_all": 
  ,
  "from": 0,  # 从第几条文档开始查
  "size": 5,
  "_source":["balance"],
  "sort": [
    
      "account_number":   # 返回结果按哪个列排序
        "order": "desc"  # 降序
      
    
  ]

_source为要返回的字段
GET bank/_search

  "query": 
    "match_all": 
  ,
  "from": 0,
  "size": 5,
  "sort": [
    
      "account_number": 
        "order": "desc"
      
    
  ],
  "_source": ["balance","firstname"]
  


  "took" : 18,  #   花了18ms
  "timed_out" : false,  # 没有超时
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1000,  # 命令1000
      "relation" : "eq"   
    ,
    "max_score" : null,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "999",  # 第一条数据id是999
        "_score" : null,  # 得分信息
        "_source" : 
          "firstname" : "Dorothy",
          "balance" : 6087
        ,
        "sort" : [  #  排序字段的值
          999
        ]
      ,
      省略。。。

②. match、match_phrase、multi_math查询

  • ①. match:如果是非字符串,会进行精确匹配
    全文检索按照评分进行排序
基本类型(非字符串),精确控制
GET bank/_search

  "query": 
    "match": 
      "account_number": "20"
    
  

match返回account_number=20的数据
查询结果:

  "took" : 1,
  "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"
        
      
    ]
  

  • ②. match:如果是字符串,会进行全文检索
字符串,全文检索
GET bank/_search

  "query": 
    "match": 
      "address": "kings"
    
  

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

  "took" : 30,
  "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(不拆分匹配):将需要匹配的值当成一整个单词(不分词)进行检索(短语匹配)
  1. match_phrase:不拆分字符串进行检索
  2. 字段.keyword:必须全匹配上才检索成功(精确匹配)
# 前面的是包含mill或road就查出来,我们现在要都包含才查出
GET bank/_search

  "query": 
    "match_phrase": 
      "address": "mill road"   #  就是说不要匹配只有mill或只有road的,要匹配mill road一整个子串
    
  

# 查处address中包含mill road的所有记录,并给出相关性得分

  "took" : 32,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 8.926605,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 8.926605,
        "_source" : 
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road", # "mill road"
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        
      
    ]
  

GET bank/_search

  "query": 
    "match_phrase": 
      "address.keyword": "mill road"  
    
  


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

  • ④. query/multi_match(多字段匹配)
    state或者address中包含mill,并且在查询过程中,会对于查询条件。进行分词处理
GET bank/_search

  "query": 
    "multi_match":   # 前面的match仅指定了一个字段。
      "query": "Mill",
      "fields": [ # state和address有mill子串  不要求都有
        "state",
        "address"
      ]
    
  

查询结果:

  "took" : 28,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 4,
      "relation" : "eq"
    ,
    "max_score" : 5.4032025,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 5.4032025,
        "_source" : 
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",  # 有mill
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"  # 没有mill
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 5.4032025,
        "_source" : 
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane", # mill
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"  # 没有mill
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 5.4032025,
        "_source" : 
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",  # 
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"  # 没有mill
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "472",
        "_score" : 5.4032025,
        "_source" : 
          "account_number" : 472,
          "balance" : 25571,
          "firstname" : "Lee",
          "lastname" : "Long",
          "age" : 32,
          "gender" : "F",
          "address" : "288 Mill Street", #
          "employer" : "Comverges",
          "email" : "leelong@comverges.com",
          "city" : "Movico",
          "state" : "MT" # 没有mill
        
      
    ]
  

③. query→bool→must复合查询

  • ①. 复合语句可以合并,任何其他查询语句,包括符合语句。这也就意味着,复合语句之间可以互相嵌套,可以表达非常复杂的逻辑

  • ②. must:必须达到must所列举的所有条件

实例:查询gender=m,并且address=mill的数据
GET bank/_search

   "query":
        "bool":  # 
             "must":[ # 必须有这些字段
              "match":"address":"mill",
              "match":"gender":"M"
             ]
         
    

查询结果

  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 3,
      "relation" : "eq"
    ,
    "max_score" : 6.0824604,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 6.0824604,
        "_source" : 
          "account_number" : 970,
          "balan

以上是关于商城项目17_es的DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch02_DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示

ElasticSearch02_DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示

ElasticSearch02_DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询

ElasticSearch02_DSL特定语言matchbooltermtermsaggsfromsizerangesort排序查询高亮显示

商城项目05_ES6 - varlet const解析表达式模板字符串箭头函数map reduceObject优化

商城项目05_ES6 - varlet const解析表达式模板字符串箭头函数map reduceObject优化