NSA*_*NSA 4 elasticsearch kibana
我想在 elasticsearch 中找到所有文档,其中我的“更新”字段存在并且小于某个值,或者文档中根本不存在该字段。我可以看到使用 bool 查询,并且必须和不能使用,但是我如何获得我试图用它们实现的确切场景?
谢谢!
假设updated是一种date字段类型,查询将如下所示:
GET test/_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "updated"
}
},
{
"range": {
"updated": {
"lte": "2019-06-10"
}
}
}
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "updated"
}
}
]
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上解释:
让,
updated应该存在 ===>Aupdated应小于X===>Bupdated根本不应该存在 ===>C所需条件转化为 (A AND B) OR C
让(A AND B)是D
现在就弹性而言,它变成:
should
{
D,
C
}
Run Code Online (Sandbox Code Playgroud)
should
{
must
{
A,
B
},
C
}
Run Code Online (Sandbox Code Playgroud)
在上面的查询中,只有范围查询就足够了,不需要使用存在查询和范围来检查更新字段的存在。
所以查询可以改写为 (B OR C):
GET test/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"updated": {
"lte": "2019-06-10"
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "updated"
}
}
]
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4606 次 |
| 最近记录: |