dino
dino

Reputation: 3307

elastic search and date range filters

I'm trying to filter an elastic search query by date using the rawes python bindings. If I drop the database, specify that the datetime field is a date_optional_time, insert a few rows into the elastic search database, and then query it using a range filter on the datetime field, I get no results (see below). Any idea what I am doing wrong?

>>> result = es.delete(root_url)
>>> result = es.put(root_url+"_mapping", data={
    "tweet": {
        "properties": {
            "datetime": {
                "type": "date", 
                "format": "date_optional_time"
            },
        }
    },
})
>>> result = es.post(root_url, data={
    "datetime": "2012-12-20 12:00:00",
    "name": "freddy",
    "text": "hello world",
})
>>> result = es.post(root_url, data={
    "datetime": "2012-11-20 12:00:00",
    "name": "julie",
    "text": "welcome to the wonderful world of cooking",
})
>>> result = es.get(root_url+"_search", data={
    "query": {
        "range": {  # expect this to return the one result on 2012-12-20
            "datetime": {
                "gte":"2012-12-01", # do not append "T00:00:00"; too slow!
                "lte":"2012-12-31", # do not append "T23:59:59"; too slow! 
            }
        }
    }
})
>>> pprint.pprint(result)
{u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
 u'hits': {u'hits': [], u'max_score': None, u'total': 0},
 u'timed_out': False,
 u'took': 4}

Upvotes: 2

Views: 16509

Answers (2)

dino
dino

Reputation: 3307

The problem is apparently with how I was encoding dates as strings (note the "T" in the strings). This works:

>>> result = es.post(root_url, data={
    "datetime": "2012-12-20T12:00:00",
    "name": "freddy",
    "text": "hello world",
})
>>> result = es.post(root_url, data={
    "datetime": "2012-11-20T12:00:00",
    "name": "julie",
    "text": "welcome to the wonderful world of cooking",
})
>>> result = es.get(root_url+"_search", data={
    "query": {
        "range": {  # expect this to return the one result on 2012-12-20
            "datetime": {
                "gte":"2012-12-01", 
                "lte":"2012-12-31",
            }
        }
    }
})

Upvotes: 7

dadoonet
dadoonet

Reputation: 14512

Could you try to launch your request with post instead of get? I don't know how works pyes but in some other client, the payload is not send when using GET instead of POST.

>>> result = es.post(root_url+"_search", data={
    "query": {
        "range": {  # expect this to return the one result on 2012-12-20
            "datetime": {
                "gte":"2012-12-01", 
                "lte":"2012-12-31",
            }
        }
    }
})

Does it work?

Upvotes: 1

Related Questions