ITPub博客

首页 > IT基础架构 > > Elasticsearch常用简单DSL

Elasticsearch常用简单DSL

作者:格伯纳 时间:2020-05-22 15:32:17 0 删除 编辑

1. 简单查询

# 查询所有
GET /tmpl-word-log*/_search
{
  "query": {
    "match_all": {}
  }
}

# 查询固定条数(默认size=10)
GET /tmpl-word-log*/_search
{
  "size": 2,
  "query": {
    "match_all": {}
  }
}

# 查询从10-15条数据(from默认从0开始)
GET /tmpl-word-log*/_search
{
  "query": {
    "match_all": {}
  }
  "form": 9,
  "size": 5
}

# 按某字段(时间)排序,desc降序,asc升序
GET /tmpl-word-log*/_search
{
  "query": {
    "match_all": {}
  }
  , "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ]
}

# 只查询某些字段
GET /tmpl-word-log*/_search
{
  "size": 2,
  "query": {
    "match_all": {}
  }
  , "_source": ["@timestamp", "index"]
}

2. 简单匹配查询(match)

# match是模糊查询,例如match匹配"十月",结果无论是只有一个"十"或者只有一个"月"都能搜所出来
# 匹配index=1的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "match": {
      "index":1
    }
  }
}

# 查询word字段包含KTV关键字的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "match": {
      "word":"KTV"
    }
  }
}

# 查询word字段中含有"快乐"或者"中秋"关键字的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "match": {
      "word": "快乐 中秋"
    }
  }
}

# 查询包含短语"中秋快乐"的数据(match_phrase是match的变种,表示匹配整个短语)
GET /tmpl-word-log*/_search
{
  "query": {
    "match_phrase": {
      "word": "中秋快乐"
    }
  }
}

3. bool查询

# 匹配word包含中秋(两个字中有一个就能匹配)且index=2的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "bool": {
      "must": [
        {"match":{"word": "中秋"}},
        {"match": {"index": 2}}
      ]
    }
  }
}

# 匹配word包含中秋(两个字中有一个就能匹配)或index=2的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "bool": {
      "should": [
        {"match":{"word": "中秋"}},
        {"match": {"index": 2}}
      ]
    }
  }
}

# 匹配word不包含中秋(两个字中有一个就能匹配)且index=2的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match":{"word": "中秋"}},
        {"match": {"index": 2}}
      ]
    }
  }
}

# 在bool中must,must_not,should等关键字可以混用,表示多条件同时满足
# 匹配word包含中秋(两个字中有一个就能匹配)且index!=2的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "bool": {
      "must": [
        {"match":{"word": "中秋"}}
      ]
      , "must_not": [
        {"match": {"index": 2}}
      ]
    }
  }
}

4.范围查询

# 查询索引在11-20之间的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "range": {
      "index": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

5.过滤查询(filters)

# 在Elasticsearch5.x版本中filtered方法弃用改用bool中的filter
# 匹配日期在9-12到9-24日的数据
GET /tmpl-word-log*/_search
{
  "query": {
    "bool":{
        "must": {"match_all": {}}, 
        "filter":{
        "range":{
          "@timestamp":{
          "gte":"2018-09-12T00:00:00.000+0800",
          "lte":"2018-09-24T23:59:59.000+0800"
          }
        }
      }
    }
    }
  }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31509949/viewspace-3157295/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
管理员

注册时间:2018-03-30

  • 博文量
    2383
  • 访问量
    984257