elasticearch 多种查询参数用法:

 

range过滤器查询范围

gt: > 大于

lt: < 小于

gte: >= 大于或等于

lte: <= 小于或等于

 

多个range

'query': {
    "bool": {
        "filter": [
            {"range": {
                "ymd": {
                    "gte": 20181215,#大于等于
                    "lt": 20181216
                    # "lte": 20181225
                }}
            },{"range": {
                'capture_num':{
                    "gte": 1
                }}
            }, {"term": {
                "camera.deviceId": param
            }}]
    }
}

    "query_string": {"query": "backend_name:baidu.com"}

from elasticsearch import Elasticsearch

es = Elasticsearch(['192.168.55.90:9200'])

res = es.search(index="ccat1", body={"query":{"match_all":{}}})
# query = {'query': {'match_all': {}}}# 查找所有文档
# query= es.get(index='ccat1', doc_type='demo0',id="deea102c62fc4b898730cfe1d3751f75-1530117249291")
print(res)

 

res = es.search(index="ccat1", body={"query": {'match':{'fileName.keyword':'0719_020314_631578.jpg'}}})

print(res)

 

range过滤器查询范围

gt: > 大于

lt: < 小于

gte: >= 大于或等于

lte: <= 小于或等于

 

来源: https://blog.csdn.net/feixuedongji/article/details/50666899

bool组合过滤器

must:所有分句都必须匹配,与 AND 相同。

must_not:所有分句都必须不匹配,与 NOT 相同。

should:至少有一个分句匹配,与 OR 相同。

 

{
    "bool":{
      "must":[],
      "should":[],
      "must_not":[],
    }
}

term过滤器

  • term单过滤
{
    "terms":{
      "money":20
    }
}
  • terms复数版本,允许多个匹配条件
{
    "terms":{
      "money": [20,30]
    }
}

match查询

  • match 精确匹配
{
    "match":{
      "email":"123456@qq.com"
    }
}
  • multi_match 多字段搜索

 

{
    "multi_match":{
      "query":"11",
      "fields":["Tr","Tq"]
    }
}

demo

  • 获取最近一小时的数据

 

{'query':
    {'filtered':
        {'filter':
            {'range':
                {'@timestamp':{'gt':'now-1h'}}
            }
        }
    }
}

 

  1.  

 

  • 条件过滤查询

 

{
    "query":{
        "filtered":{
            "query":{"match":{"http_status_code":500}},
            "filter":{"term":{"server_name":"vip03"}}
        }
    }
}
  • Terms Facet 单字段统计

 

{'facets':
  {'stat':
    {'terms':
      {'field':'http_status_code',
        'order':'count',
    'size':50}
    }
  },
  'size':0
}
  • 一次统计多个字段

 

{'facets':
  {'cip':
    {'terms':
      {'fields':['client_ip']}},
        'status_facets':{'terms':{'fields':['http_status_code'],
        'order':'term',
        'size':50}}},
    'query':{'query_string':{'query':'*'}},
  'size':0
}
  • 多个字段一起统计

 

{'facets':
  {'tag':
    {'terms':
      {'fields':['http_status_code','client_ip'],
        'size':10
       }
    }
  },
  'query':
    {'match_all':{}},
  'size':0
}

数据组装

以下是kibana首页的demo,用来统计一段时间内的日志数量

 

{
  "facets": {
    "0": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "5m"
      },
      "facet_filter": {
        "fquery": {
          "query": {
            "filtered": {
              "query": {
                "query_string": {
                  "query": "*"
                }
              },
              "filter": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "@timestamp": {
                          'gt': 'now-1h'
                        }
                      }
                    },
                    {
                      "exists": {
                        "field": "http_status_code.raw"
                      }
                    },
                    # --------------- -------
                    # 此处加匹配条件
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

如果想添加匹配条件,在以上代码标识部分加上过滤条件,按照以下代码格式即可

 

{
"query": {
    "query_string": {"query": "backend_name:baidu.com"}
    }
},

先介绍到这里,后续会有Query DSL API介绍。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值