端口查询:elasticsearch嵌套范围查询

假设我想要这个结构的文档:

{
  "hours": {
    "open": [
      {
        "start": 10,
        "end": 19
      },
      {
        "start": 21,
        "end": 29
      }
      ...
    ],
    "closed": [
      {
        "start": 100,
        "end": 199
      },
      {
        "start": 201,
        "end": 299
      }
      ...
    ]
  }
}

其索引具有此映射:

{
  "mappings": {
    "_doc": {
      "properties": {
        "hours": {
          "properties": {
            "open": {
              "type": "nested",
              "properties": {
                "start": { "type": "integer" },
                "end": { "type": "integer" }
              }
            },
            "closed": {
              "type": "nested",
              "properties": {
                "start": { "type": "integer" },
                "end": { "type": "integer" }
              }
            }
          }
        }
      }
    }
  }
}

在 Elasticsearch 查询 DSL 中,如何找到20位于开放段内而不是封闭段内的所有文档。我尝试的查询不正确。

查询失败

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "hours.open",
                  "query": {
                    "range": {
                      "hours.open.start": { "lte": 20 }
                    }
                  }
                }
              },
              {
                "nested": {
                  "path": "hours.open",
                  "query": {
                    "range": {
                      "hours.open.end": { "gte": 20 }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "bool": {
                  "must": [
                    {
                      "nested": {
                        "path": "hours.closed",
                        "query": {
                          "range": {
                            "hours.closed.start": { "lte": 20 }
                          }
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "hours.closed",
                        "query": {
                          "range": {
                            "hours.closed.end": { "gte": 20 }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我的查询有什么问题?它返回这个文档,这不是我想要的。20 不在一个开放的段内。

8

我终于得到它的工作。以下是正确的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "hours.open",
            "query": {
              "bool": {
                "must": [
                  { "range": { "hours.open.start": { "lte": 20 } } },
                  { "range": { "hours.open.end": { "gte": 20 } } }
                ]
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "nested": {
            "path": "hours.closed",
            "query": {
              "bool": {
                "must": [
                  { "range": { "hours.closed.start": { "lte": 20 } } },
                  { "range": { "hours.closed.end": { "gte": 20 } } }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

话虽如此,它看起来像我原来的尝试是错误的,因为有两个不同的hours.open嵌套路径查询和同样的两个不同的hours.closed嵌套路径查询。

0

似乎你需要交换 lte 和 gte:

"hours.open.start": { "gte": 20 }
"hours.open.end": { "lte": 20 }

和相同的关闭时间:

"hours.closed.start": { "gte": 20 }
"hours.closed.end": { "lte": 20 }

编辑:must 和 must_not 需要是同一 bool 查询的一部分:

{
    "query": {
        "bool": {
            "must": [{
                    "nested": {
                        "path": "hours.open",
                        "query": {
                            "range": {
                                "hours.open.start": {
                                    "gte": 20
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "hours.open",
                        "query": {
                            "range": {
                                "hours.open.end": {
                                    "lte": 20
                                }
                            }
                        }
                    }
                }
            ],
            "must_not": [{
                "bool": {
                    "must": [{
                            "nested": {
                                "path": "hours.closed",
                                "query": {
                                    "range": {
                                        "hours.closed.start": {
                                            "gte": 20
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "nested": {
                                "path": "hours.closed",
                                "query": {
                                    "range": {
                                        "hours.closed.end": {
                                            "lte": 20
                                        }
                                    }
                                }
                            }
                        }
                    ]
                }
            }]
        }
    }
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(977)
单片机蜂鸣器代码:单片机和蓝牙的电路编程(bluetooth microcontroller)
上一篇
Cd功放一体机:LaTeX多重方程式参考于一体(latex subequations)
下一篇

相关推荐

  • comeandgetyourlove音乐爱就在你身边

    Come and Get Your Love是一首热门的歌曲,由美国摇滚乐队Redbone演唱。这首歌曲于1974年发行,被收录在他们的专辑《Wovoka》中。歌曲以放克曲风为主,旋律活泼,曲调悠扬,歌词朗朗上口,深受歌迷喜爱。…

    2023-06-29 07:47:31
    0 59 64
  • css预编译器: center;}

    CSS预编译器是一种用于构建CSS的工具,它可以将CSS代码转换为更易于管理和维护的格式。它们可以使CSS代码更加灵活,更易于重用,并且可以帮助开发人员更轻松地组织和管理CSS代码。…

    2023-04-30 05:19:08
    0 75 10
  • python中predict函数参数:如何使用Python的predict函数进行机器学习预测

    示例示例predict函数是scikit-learn中的一个函数,用于预测新样本的输出结果。参数:…

    2023-03-30 08:03:12
    0 54 51
  • canvas 官网Bring Your Ideas to Life with Creative Artwork

    Canvas 官网是一个用于创建图形的 HTML5 API,它可以在浏览器中使用 JavaScript 来绘制 2D 图形。它提供了一个可以在网页上绘制图形的强大工具,可以用来创建动画、游戏、数据可视化等。…

    2023-02-28 09:52:08
    0 52 44
  • qt creator快速入门 第3版 pdf从零开始

    Qt Creator快速入门第3版是一本关于Qt Creator的教程书,旨在帮助读者快速掌握Qt Creator的使用。书中介绍了Qt Creator的基本功能,如如何创建项目、编辑代码、调试代码以及创建应用程序等等。书中还提供了一些实例代码,帮助读者更好地理解Qt Creator的用法。…

    2023-05-16 03:03:33
    0 32 39
  • cherry键盘win键不能用:解决Cherry键盘Win键无法使用的措施

    如果您的cherry键盘win键不能用,可能是由于系统设置问题导致的。下面提供一些代码,可以帮助您解决这个问题:打开“控制面板”,然后点击“硬件和声音”,打开“键盘”选项卡。…

    2023-08-27 03:36:33
    0 81 26
  • certificate意思一步一步指南

    示例示例是一种用于证明某个人或机构拥有某种资格或资质的文件。它可以是一种认证,也可以是一种奖励或认可。代码示例:…

    2023-09-14 15:01:58
    0 34 29
  • win10系统ctrl加c不能复制:解决win10系统下Ctrl+C不能复制的问题

    解决方案解决方案答:可能是由于系统快捷键被修改所导致的,可以尝试恢复系统默认快捷键;…

    2023-04-15 00:45:32
    0 34 53

发表评论

登录 后才能评论

评论列表(66条)