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'}}
}
}
}
}
- 条件过滤查询
{
"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介绍。