请教一个ES查询的写法?

作者站长头像
站长
· 阅读数 10

我的搜索条件:

item_code = "ZJDL_013" 
&& "effective_flag" = 1 
&& "delete_flag" = 0 
&& (start_time == null || start_time <= '2023-02-08') 
&& (end_time == null || end_time <= '2023-02-08')

我的ES查询条件

GET index_three_catalogues/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "item_code.keyword": "ZJDL_013"
          }
        },
        {
          "match": {
            "effective_flag": 1
          }
        },
        {
          "match": {
            "delete_flag": 0
          }
        },
        {
          "bool": {
            "should": [
              {
                "exists": {
                  "field": "start_time"
                }, 
                
                "range": {
                  "start_time": {
                    "lte": "2023-02-08",
                    "format": "yyyy-MM-dd"
                  }
                }
              },
              {
                "exists": {
                  "field": "start_time"
                }, 
                "range": {
                  "start_time": {
                    "gte": "2023-02-08",
                    "format": "yyyy-MM-dd"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

但是一直报错,好像是语法组织得不对,请教这块带的逻辑条件怎么写。

回复
1个回答
avatar
test
2024-07-10

仔细一些,你的 rangeexists 成组合了。

下面就是你要的组合方式。

GET index_three_catalogues/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "item_code.keyword": "ZJDL_013"
          }
        },
        {
          "match": {
            "effective_flag": 1
          }
        },
        {
          "match": {
            "delete_flag": 0
          }
        },
        {
          "bool": {
            "should": [
              {
                "exists": {
                  "field": "start_time"
                }
              },
              {
                "range": {
                  "start_time": {
                    "lte": "2023-02-08",
                    "format": "yyyy-MM-dd"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "exists": {
                  "field": "start_time"
                }
              },
              {
                "range": {
                  "start_time": {
                    "gte": "2023-02-08",
                    "format": "yyyy-MM-dd"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容