将Python elastisearch_dsl与嵌套对象

时间:2017-03-24 00:08:36

标签: python elasticsearch-dsl

我想尝试将elasticsearch_dsl与python一起用于以下

import elasticsearch

es_server = 'my_server_name'
es_port = '9200'
es_index_name = 'my_index_name'
es_connection = Elasticsearch([{'host': es_server, 'port': es_port}])

es_query = '{"query":{"bool":{"must":[{"term":{"data.party.fullName":"john do"}}],"must_not":[],"should":[]}},"from":0,"size":1,"sort":[],"facets":{}}'
my_results = es_connection.search(index=es_index_name, body=es_query)
print  my_results

es_query ='{"query": {"nested" : {"filter" : {"term" : {"party.phoneList.phoneFullNumber" : "4081234567"}},"path" : "party.phoneList"}},"from" :0,"size" : 1}';
my_results = es_connection.search(index=es_index_name, body=es_query)
print  my_results

我能够获得第一个查询但不确定第二个查询

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q

client = Elasticsearch('my_server:9200')
s = Search(using=client, index = "my_index").query("term",fullName="john do ")
response = s.execute()
print response

不确定如何使用DSL进行嵌套对象party.phoneList.phoneFullNumber

的查询

ES的新手,因此无法弄清楚如何执行嵌套对象。 我看了https://github.com/elastic/elasticsearch-dsl-py/issues/28并且无法理解。

谢谢!

1 个答案:

答案 0 :(得分:1)

只需使用__代替.来解决python的限制和nested查询:

s = Search(using=client, index = "my_index")
s = s.query("nested",
    path="party.phoneList",
    query=Q("term", party__phoneList__phoneFullNumber="4081234567")
)