Elasticsearch 返回的文档数量相同 API
Same number of documents returned by Elasticsearch API
美好的一天!
Elasticsearch API 出现问题。我写了一个脚本,我在其中执行了几个不同的请求。在这些查询中,我尝试计算最近半小时内来自不同主机的文档数。
在所有请求中我使用“GET /log/_count”
"query": {
"bool": {
"must": [
{
"match": { "host": "xxx" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
"query": {
"bool": {
"must": [
{
"match": { "host" : "yyy" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
"query": {
"bool": {
"must": [
{
"match": { "host" : "zzz" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
以及另一个验证请求:
"query": {
"bool": {
"filter": {
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
}
}
但是当我 运行 脚本时,我得到的文件数量大致相同。即使在最后一个应该显示所有文档总和的请求中,也显示了相同的数量,尽管它只配置了“范围”。
XXX: 15691
YYY: 15689
ZZZ: 15689
ALL: 15689
这是正常行为,还是我做错了什么?
UPD.
"query": {
"bool": {
"must": [
{
"match_phrase": { "host" : "xxx" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
这可能是预期的行为。
原因是您有两个查询可用于 now
。您可能会按时执行 query1 和 query2。 now
时间 1 和时间 2 不同。
同时(time2 - time1),可能会添加一些文档。因此,您看到的差别很小。
要获取所有主机的计数,
GET logs/_search
{
"size": 0,
"aggs": {
"hosts": {
"filters": {
"filters": {
"xxx": {
"match": {
"host": "xxx"
}
},
"yyy": {
"match": {
"host": "yyy"
}
}
}
},
"aggs": {
"range": {
"date_range": {
"field": "@timestamp",
"ranges": [
{
"to": "now-30m"
},
{
"from": "now"
}
]
}
}
}
}
}
}
如果您的值不止一个字,您需要使用 match phrase
而不是 match
。
美好的一天!
Elasticsearch API 出现问题。我写了一个脚本,我在其中执行了几个不同的请求。在这些查询中,我尝试计算最近半小时内来自不同主机的文档数。
在所有请求中我使用“GET /log/_count”
"query": {
"bool": {
"must": [
{
"match": { "host": "xxx" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
"query": {
"bool": {
"must": [
{
"match": { "host" : "yyy" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
"query": {
"bool": {
"must": [
{
"match": { "host" : "zzz" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
以及另一个验证请求:
"query": {
"bool": {
"filter": {
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
}
}
但是当我 运行 脚本时,我得到的文件数量大致相同。即使在最后一个应该显示所有文档总和的请求中,也显示了相同的数量,尽管它只配置了“范围”。
XXX: 15691
YYY: 15689
ZZZ: 15689
ALL: 15689
这是正常行为,还是我做错了什么?
UPD.
"query": {
"bool": {
"must": [
{
"match_phrase": { "host" : "xxx" }
},
{
"range": {
"@timestamp": {
"gte": "now-30m",
"lte": "now"
}
}
}
]
}
}
这可能是预期的行为。
原因是您有两个查询可用于 now
。您可能会按时执行 query1 和 query2。 now
时间 1 和时间 2 不同。
同时(time2 - time1),可能会添加一些文档。因此,您看到的差别很小。
要获取所有主机的计数,
GET logs/_search
{
"size": 0,
"aggs": {
"hosts": {
"filters": {
"filters": {
"xxx": {
"match": {
"host": "xxx"
}
},
"yyy": {
"match": {
"host": "yyy"
}
}
}
},
"aggs": {
"range": {
"date_range": {
"field": "@timestamp",
"ranges": [
{
"to": "now-30m"
},
{
"from": "now"
}
]
}
}
}
}
}
}
如果您的值不止一个字,您需要使用 match phrase
而不是 match
。