Python 3.x Elasticsearch:
- elasticsearch==6.1.1
- elasticsearch-dsl==6.1.0
Elasticsearch model:
class VendCompanyData(DocType):
data = Nested(
properties={
"machine_id": Integer(required=True),
"product_id": Integer(required=True),
"quantity": Integer(),
"insert_time": Date()
}
)
last_export = Date()
created_at = Date()
def save(self, **kwargs):
self.created_at = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
return super().save(**kwargs)
Some data for nested filed:
[
{
"machine_id" : 1229,
"product_id" : 3323,
"quantity" : 1,
"insert_time" : "2017-03-13T12:48:10.156026"
},
{
"machine_id" : 1593,
"product_id" : 3998,
"quantity" : 1,
"insert_time" : "2017-03-13T12:48:23.296982"
},
{
"machine_id" : 1485,
"product_id" : 3979,
"quantity" : 1,
"insert_time" : "2017-03-13T12:48:44.989393"
},
{
"machine_id" : 1476,
"product_id" : 8212,
"quantity" : 1,
"insert_time" : "2017-03-13T12:48:54.282708"
}]
And now elasticsearch-dsl query for query only machine_id, and logic returns only that machine/s from the array.
Query:
get_last_machine= (
Search(using=cls.client, index=cls.vend_index_today)
.filter("nested", path="data", query=Q("term", data__machine_id=machine_id))
.source(['data.machine_id'])
)
What query returns is all data, not only one what I've asking in the query.
inner_hitsinnestedquery. Refer: stackoverflow.com/questions/31719968/…