1 | initial version |
You can use the Elasticsearch Python API to filter data based on a particular field within a specific time range using the following steps:
from elasticsearch import Elasticsearch
es = Elasticsearch()
Note: If the Elasticsearch instance runs on a remote server, you can use the hosts
argument to specify the IP address and port number of the server.
query = {
"query": {
"bool": {
"must": [
{
"match": {
"your_field": "your_value"
}
},
{
"range": {
"@timestamp": {
"gte": "2021-01-01T00:00:00",
"lt": "2021-01-31T23:59:59"
}
}
}
]
}
}
}
In this query, replace your_field
with the name of the field you want to filter on and your_value
with the value you want to filter for. Also, replace @timestamp
with the name of the timestamp field in your index.
The range
filter will match documents with a timestamp field that falls within the specified time range.
res = es.search(index="your_index", body=query)
In this query, replace your_index
with the name of the index you want to search in.
for hit in res['hits']['hits']:
print(hit['_source'])
This code will loop through the search results and print the source document for each hit.