ElasticSearch查询常见DSL语句汇总

ES查询之常见查询

term查询

term的查询代表完全匹配,搜索之前不会对搜索的关键词进行分词。对关键字去文档分词库中匹配内容。

基本语法

# term查询
POST scs*/_search
{
  "from": 0,
  "size":5,
  "query": {
    "term": {
      "type": {
        "value": "1"
      }
    }
  }
}

terms查询

terms和term的查询机制是一样的,都不会对搜索的关键词进行分词。对关键字去文档分词库中匹配内容。

POST scs*/_search
{
  "query": {
    "terms": {
      "customerCode.keyword": [
        "ds",
        "fc"
      ]
    }
  }
}

match 查询

match 查询属于高层查询,他会根据你查询的字段类型不一样,采用不同的查询方式。 ·查询的是日期或者是数值的话,他会将你基于的字符串查询内容转换为日期或者数值对待。 ·如果查询的内容是一个不能被分词的内容((keyword ),matchi 查询不会对你指定的查询关键字进行分词。 ·如果查询的内容时一个可以被分词的内容(text),match 会将你指定的查询内容根据一定的方式去分词,去分词库中匹配指定的内容。 matchi 查询,实际底层就是多个term查询,将多个term查询的结果给你封装到了一起。

语法

POST /sms-log-index/sms-log-type/_search
{
    "query":{
        "match":{
            "smsContent":"微信"
        }
    }
}

match_all

查询全部内容,不指定任何查询条件。

语法

POST /sms-log-index/sms-log-type/_search
{
    "query":{
        "match_all":{}
    }
}

布尔match查询

{
    "query": {
        "match": {
            "smsContent": {
                    "query": "中国 健康",
                    "operator": "and"
            }
        }
    }
}

multi-match

match针对一个field做检索,multi-match针对多个field进行检索。

POST scs*/_search
{
  "query": {
   "multi_match": {
     "query": "D",
     "fields": ["stationCode","customerCode.keyword"]
   }
  }
}

ES查询之其他查询

id

  GET scs1/_doc/1005558649YT5749518232238

ids

查询语句

POST scs*/_search
  {
    "query": {
      "ids": {
        "values": ["57111859YT5568068260191","1005558649YT5749518232238"]
      }
    }
  }

prefix

前缀查询

POST scs*/_search
{
  "query": {
    "prefix": {
      "city": {
        "value": "昆山"
      }
    }
  }
}

fuzzy

模糊查询,根据输入的字符大概取匹配结果

POST scs*/_search
{
  "query": {
    "fuzzy": {
      "city": {
        "value": "昆山",
        "prefix_length":2  ## 指定前面结果字符是不允许出现错误的
      }
    }
  }
}

wildcard

通配查询,和mysql的like查询一样,可以在字符串中指定通配符*和占位符?

POST scs*/_search
{
  "query": {
    "wildcard": {
      "city": {
        "value": "昆山*"
      }
    }
  }
}

range

范围查找,gt:大于,lt:小于 e:等于。gte:>= lte:<=

POST scs*/_search
  {
    "query": {
     "range": {
       "fee": {
         "gte": 1,
         "lte": 5
       }
     }
    }
  }

regexp

正则查询,通过你编写的正则表达式去匹配内容。
PS:
prefix、fuzzy、wildcard和regexp查询效率相对比较低,要求要求高时,避免使用。

POST scs*/_search
{
    "query": {
     "regexp": {
       "mobile":"180[0-9]{8}"
     }
    }
}

ES查询之复合查询

bool

复合过滤器,将多个查询条件,以一定的逻辑组合在一起。

  • must:所有的条件,用must组合在一起,表示and的意思。
  • must_not:将must_not中的条件,全部都不能匹配,表示not的意思。
  • should:所有的条件,用should组合在一起,表示or的意思。
POST scs*/_search
{
  "query": {
    "bool": {
      "must": [
       {
         "match": {
           "code": "00001"
         }
       }
      ],
      "must_not": [
        {"term": {
          "deviceType": {
            "value": "ZT"
          }
        }}
      ],
      "should": [
        {"term": {
          "customerCode.keyword": {
            "value": "fc"
          }
        }},
         {"term": {
          "customerCode.keyword": {
            "value": "cn"
          }
        }}
      ]
    }
  }
}

boosting

查询可以帮助取影响查询后的score。

  • positive:只有匹配上positive的查询的内容,才会被放到返回的结果集中。
  • negative:如果匹配上和positive并且也匹配上了negative,就可以降低文档的score。
  • negative_boost:指定系数,必须小于1.0。
    关于查询时,分数是如何计算的:
  • 搜索的关键字在文档中出现的频次越高,分数就越高。
  • 指定的文档内容越短,分数就越高。
  • 在搜索时,指定的关键字也会被分词。这个被分词的内容,被分词库匹配的个数越多,分数越高。
POST scs*/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "customerCode.keyword": "DSD"
        }
      },
      "negative": {
        "match": {
          "stationCode": "A000002498"
        }
      },
      "negative_boost": 0.8
    }
  }
}

ES查询之filter查询

filter查询比较

query:根据查询条件计算文档的匹配度得到一个分数,并且根据分数进行排序,不会做缓存的
filter:根据查询条件查询文档,不计算分数,且filter会对经常被过滤的数据进行缓存。
filter查询语句

POST scs*/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "customerCode.keyword": "cn"
          }
        },
        {
          "range": {
            "opIncomeTime": {
              "gte": 1635632000000,
              "lte": 1648051199999
            }
          }
        }
      ]
    }
  }
}

ES查询之高亮查询

高亮查询是用户输入的关键字,以一定的特殊样式展示给用户。
高亮展现的数据,本身就是文档中的一个field,单独将field以highlight的样式返回给你。
es提供了一个highlight的属性,和query同级别的:
fragment_size:指定高亮数据展示多少字符回来。
pre_tags:指定前缀标签。
post_tags:指定后缀标签。

POST scs*/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "customerCode.keyword": "ds"
          }
        },
        {
          "range": {
            "opIncomeTime": {
              "gte": 1635632000000,
              "lte": 1648051199999
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "customerCode":{}
    },
    "pre_tags": "<font color='red'>",
    "post_tags": "</font>",
    "fragment_size": 10
  }
}

ES查询之聚合查询

ES聚合查询和MySQL的聚合查询类型,ES的聚合查询相比MySQL要强大的多,ES提供的统计数据的方式多种多样。

POST /index/type/_search
{
    "aggs":{
        "名字":{
            "agg_type":{
                "属性":"值"
            }
        }
    }
}

去重计数查询

去重计数,即cardinality,第一步先将返回的文档中的一个指定的field进行去重,统计一共有多少条。

POST scs*/_search
{
  "aggs": {
    "agg": {
      "cardinality": {
        "field": "customerCode"
      }
    }
  }
}

range

范围统计:统计一定范围出现的文档个数,比如针对某个field的值在0,-100,100-200之间分别是多少。
范围统计可以针对普通的数值、时间类型和ip类型等做出相应的统计。

普通的数值的排序
POST scs*/_search
{
  "aggs": {
    "agg": {
      "range": {
        "field": "fee",
        "ranges": [
          {
            "to": 5
          },
          {
            "from": 5,
            "to": 10
          }
        ]
      }
    }
  }
}
日期格式排序
POST scs*/_search
{
  "aggs": {
    "agg": {
      "date_range": {
        "field": "createTime",
        "format": "yyyy", 
        "ranges": [
          {
            "to": 2021
          },
          {
            "from": 2021
          }
        ]
      }
    }
  }
}
ip地址排序
POST scs*/_search
{
  "aggs": {
    "agg": {
      "ip_range": {
        "field": "ipAddr",
        "ranges": [
          {
            "to": "10.7.1.10"
          },
          {
            "from": "10.7.1.10"
          }
        ]
      }
    }
  }
}

统计聚合查询

查询指定field的最大值、最小值、平均值和平方和。
语法:

POST scs*/_search
{
  "aggs": {
    "agg":{
      "extended_stats": {
        "field": "stationCode"
      }
    }
  }
}

ES查询之地图经纬度查询

创建索引

{
    "settings": {
            "number_of_replicas": 3,
            "number_of_shards": 5
    },
    "mappings": {
            "properties": {
                    "name": {
                            "type": "text"
                    },
                    "location": {
                            "type": "geo_point"
                    }
            }
    }
}

添加数据

PUT /map/_doc/1
{
  "name":"天安门",
  "location":
  {
    "lon":116.403981,
    "lat":39.914492
  }
}

PUT /map/_doc/2
{
  "name":"海淀公园",
  "location":
  {
    "lon":116.302509,
    "lat":39.991152
  }
}

PUT /map/_doc/3
{
  "name":"北京动物园",
  "location":
  {
    "lon":116.343184,
    "lat":39.947468
  }
}

geo_distance

geo_distance:直线距离检索,如给定点A,要求返回地图上距离点A三千米的商家(点外卖场景)
查找索引内距离3000米内的点
geo_distance涉及的参数如下

location:确定一个点;
distance:确定一个半径,单位米
distance_type:确定一个图形的类型,一般是圆形,arc

POST /map/_search
{
  "query": {
    "geo_distance":
    {
      "location":
      {
        "lon":116.433733
        ,"lat":39.908404
      },
      "distance":3000,
      "distance_type":"arc"
    }

  }
}

geo_bounding_box

geo_bounding_box:以两个点确定一个矩形,获取在矩形内的全部数据

geo_bounding_box涉及的参数如下
top_left: 左上角的矩形起始点经纬度;
bottom_right: 右下角的矩形结束点经纬度

POST /map/_search
{
  "query": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lon": 116.326943,
          "lat": 39.95499
        },
        "bottom_right": {
          "lon": 116.433446,
          "lat": 39.908737
        }
      }
    }
  }
}

geo_polygon

geo_polygon涉及的参数如下
points:是个数组,存储多变形定点的经纬度,每个点用大括号包起来。

POST /map/_search
{
  "query": {
    "geo_polygon": {
      "location": {
        "points": [
          {
            "lon": 116.29561,
            "lat": 39.976004
          },
          {
            "lon": 116.364528,
            "lat": 39.996348
          },
          {
            "lon": 116.300209,
            "lat": 40.003423
          }
        ]
      }
    }
  }
}
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值