Query in elasticsearch with multiple ranges on multiple dates

后端 未结 1 536
温柔的废话
温柔的废话 2021-02-13 06:16

The following range_query returns a result as expected:

{\"query\": {
    \"bool\": {
      \"must\": [
        {
          \"range\": {
            \"created_at         


        
相关标签:
1条回答
  • 2021-02-13 06:36

    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"
                            }
                        }
                    }
                ]
            }
        }
    }
    '
    
    0 讨论(0)
提交回复
热议问题