在弹性搜索中查询多个日期的多个范围

时间:2013-12-16 12:29:25

标签: elasticsearch

以下range_query按预期返回结果:

{"query": {
    "bool": {
      "must": [
        {
          "range": {
            "created_at": {
              "gte": "2013-12-09"
            }
          }
        }
      ]
    }
  }
}

但是将几个范围查询放在一起,什么都不返回:

{"query": {
    "bool":{
      "must": [
        {
          "and": [
            {
              "range": {
                "created_at": {
                  "gte": "2013-12-09"
                }
              }
            },
            {
              "range": {
                "happens_on": {
                  "lte": "2013-12-16"
                }
              }
            },
            {
              "range": {
                "created_at": {
                  "lte": "2013-12-14"
                }
              }
            }
          ]
        }
      ]
    }
  }
}

在多个字段上使用多个range_queries的正确方法是什么?

编辑:啊,好的,所以这是我使用range_filter而不是range_query的地方?这听起来很有希望,所以我只使用一个范围过滤器重新编写了我的查询。在这里发布所有内容,以防我在其他地方弄乱查询。我正在执行GET,源键中的所有内容实际上都是JSON,但为了便于阅读,我删除了转义的连字符:

{
  "source": {
    "filtered": {
      "filter": {
        "and": [
          {
            "term": {
              "restricted": false
            }
          },
          {
            "not": {
              "term": {
                "deleted": true
              }
            }
          },
          {
            "range": {
              "happens_on": {
                "lte": "2013-12-16"
              }
            }
          }
        ]
      },
      "query": {
        "bool": {
          "must": [
          ]
        }
      }
    },
    "from": 0,
    "size": 10
  }
}

令人遗憾的是,我的问题仍然存在:我没有受到任何打击。

EDIT2:所以,在Njal建议的必须条款中沿着范围的小巷走下去。这给了我一个像这样的多范围查询:

{
  "source": {
    "filter": {
      "and": [
        {
          "term": {
            "restricted": false
          }
        },
        {
          "not": {
            "term": {
              "deleted": true
            }
          }
        }
      ]
    },
    "from": 0,
    "query": {
      "bool": {
        "must": [
          {
            "range": {
              "happens_on": {
                "gte": "2013-12-06"
              }
            }
          },
          {
            "range": {
              "created_at": {
                "gte": "2013-12-15"
              }
            }
          },
          {
            "range": {
              "happens_on": {
                "lte": "2013-12-17"
              }
            }
          },
          {
            "range": {
              "created_at": {
                "lte": "2013-12-17"
              }
            }
          }
        ]
      }
    },
    "size": 10
  }
}

仍然没有返回任何结果。我在这里犯了什么明显的错误吗?

1 个答案:

答案 0 :(得分:15)

根据您的bool查询'must条款,无需将其包含在and中。没有and个查询,也许您在考虑and filter

为方便起见,

Example runnable play为curl命令:

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "settings": {},
    "mappings": {
        "type": {
            "properties": {
                "created_at": {
                    "type": "date",
                    "format": "dateOptionalTime"
                },
                "name": {
                    "type": "string"
                },
                "happens_on": {
                    "type": "date",
                    "format": "dateOptionalTime"
                }
            }
        }
    }
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"name":"foo","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"}
{"index":{"_index":"play","_type":"type"}}
{"name":"bar","created_at":"2013-12-08T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"}
{"index":{"_index":"play","_type":"type"}}
{"name":"bar","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-17T00:00:00.000Z"}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "created_at": {
                            "gte": "2013-12-09T00:00:00.000Z"
                        }
                    }
                },
                {
                    "range": {
                        "happens_on": {
                            "lte": "2013-12-16T00:00:00.000Z"
                        }
                    }
                }
            ]
        }
    }
}
'