Query in elasticsearch with multiple ranges on multiple dates

thomax

The following range_query returns a result as expected:

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

But and-ing together several range queries, returns nothing:

{"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"
                }
              }
            }
          ]
        }
      ]
    }
  }
}

What is the correct way to use multiple range_queries on multiple fields?

EDIT: Ah, ok, so this is where I use a range_filter as opposed to range_query? This sounded promising, so I re-wrote my query using only a single range filter. Posting all of it here, in case I'm messing up the query someplace else. I'm performing a GET, and everything inside the source key is actually JSON, but I removed the escaped the hyphens for readability:

{
  "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
  }
}

Regrettably, my problem is still the same: I'm not getting any hits.

EDIT2: So, going down the alley of ranges inside a must clause like Njal suggested. This gives me a multi-range query like this:

{
  "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
  }
}

Still no results returned. Am I doing any obvious mistakes here?

Njal Karevoll

Under your bool queries' must clause there's no need to wrap it in an and. There is no and query, maybe you were thinking of the and filter?

Example runnable play as curl commands for convenience:

#!/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"
                        }
                    }
                }
            ]
        }
    }
}
'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Query multiple date ranges in mongoose

From Dev

Elasticsearch multiple fields OR query

From Dev

django query latest of multiple dates

From Dev

django query latest of multiple dates

From Dev

SQL Query with group by for multiple date ranges

From Dev

Mysql query to select from multiple ranges

From Dev

Elasticsearch Aggregation Query with multiple excludes

From Dev

ElasticSearch : Query multiple value in array

From Dev

Elasticsearch bool query with multiple and/or flows

From Dev

ElasticSearch : Query multiple value in array

From Dev

Elasticsearch bool query with multiple and/or flows

From Dev

Elasticsearch Search query with multiple filters

From Dev

Elasticsearch Multiple Prefix query OR Matching

From Dev

single query to get data for multiple dates

From Dev

SQL Query Multiple dates to one date

From Dev

MySQL query for multiple counts returned for variable date ranges

From Dev

ElasticSearch NEST query across multiple types

From Dev

Elasticsearch phrase prefix query on multiple fields

From Dev

Multiple values in QueryString query using elasticsearch NEST

From Dev

How to update multiple documents that match a query in elasticsearch

From Dev

Multiple values in QueryString query using elasticsearch NEST

From Dev

Using elasticsearch dsl query with multiple indices

From Dev

Lucen/Elasticsearch: a query for a filed with multiple values

From Dev

How to write elasticsearch multiple query condition?

From Dev

ElasticSearch - filtered query with multiple clauses ES 5

From Dev

Elasticsearch - How to use multiple analyzers in a query

From Dev

Multiple bool clauses in elasticsearch filter/query

From Dev

Replace multiple ranges in a string

From Java

Concatenate multiple ranges in a list

Related Related

  1. 1

    Query multiple date ranges in mongoose

  2. 2

    Elasticsearch multiple fields OR query

  3. 3

    django query latest of multiple dates

  4. 4

    django query latest of multiple dates

  5. 5

    SQL Query with group by for multiple date ranges

  6. 6

    Mysql query to select from multiple ranges

  7. 7

    Elasticsearch Aggregation Query with multiple excludes

  8. 8

    ElasticSearch : Query multiple value in array

  9. 9

    Elasticsearch bool query with multiple and/or flows

  10. 10

    ElasticSearch : Query multiple value in array

  11. 11

    Elasticsearch bool query with multiple and/or flows

  12. 12

    Elasticsearch Search query with multiple filters

  13. 13

    Elasticsearch Multiple Prefix query OR Matching

  14. 14

    single query to get data for multiple dates

  15. 15

    SQL Query Multiple dates to one date

  16. 16

    MySQL query for multiple counts returned for variable date ranges

  17. 17

    ElasticSearch NEST query across multiple types

  18. 18

    Elasticsearch phrase prefix query on multiple fields

  19. 19

    Multiple values in QueryString query using elasticsearch NEST

  20. 20

    How to update multiple documents that match a query in elasticsearch

  21. 21

    Multiple values in QueryString query using elasticsearch NEST

  22. 22

    Using elasticsearch dsl query with multiple indices

  23. 23

    Lucen/Elasticsearch: a query for a filed with multiple values

  24. 24

    How to write elasticsearch multiple query condition?

  25. 25

    ElasticSearch - filtered query with multiple clauses ES 5

  26. 26

    Elasticsearch - How to use multiple analyzers in a query

  27. 27

    Multiple bool clauses in elasticsearch filter/query

  28. 28

    Replace multiple ranges in a string

  29. 29

    Concatenate multiple ranges in a list

HotTag

Archive