5

我想创建这个例子

GET /my_store/products/_search
{
    "query" : {
        "filtered" : { 
            "query" : {
                "match_all" : {} 
            },
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

使用 Python 的 elasticsearch_dsl。

import elasticsearch as ES
import elasticsearch_dsl as dsl
from elasticsearch_dsl import Search

client = ES.Elasticsearch() # i'm using the localhost default client
s = Search(using = client, index = "my_store") 

好的,这指定了主机、端口和索引。

s = s.filter("term", price = 20)
results = s.execute().to_dict()

但是如何指定文档类型是“产品”?似乎 Search() 函数中应该有一个参数。

类似的问题,假设我想运行相同的查询,但我希望它在索引“my_store”和“her_store”上运行。我该如何指定?

4

2 回答 2

5

你可以这样使用。

s = Search(using=client, index=('my_report', 'my_store'), doc_type=('products'))

索引参数采用list, tuple,string类型。

在 Search 构造函数中,您可以看到

if isinstance(index, (tuple, list)):
    self._index = list(index)
elif index:
    self._index = [index]
于 2015-11-18T02:38:43.603 回答
3

我刚刚让它像这样工作:

client = connections.create_connection(hosts=['myNode01:9200'])
s = Search().using(client)
      .index('myindex')
      .doc_type('mytype1')
      .doc_type('mytype2')
      .query('match', name='arandomname')
于 2016-02-16T18:56:38.307 回答