使用elasticsearch_dsl(Python)时如何指定文档类型?同样,如何指定几个索引?

How to specify the document type when using elasticsearch_dsl (Python)? Similarly, how to specify a few indices?

我想创建这个例子

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()

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

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

你可以这样使用。

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

索引参数采用 listtuplestring 类型。

在搜索构造函数中你可以看到

if isinstance(index, (tuple, list)):
    self._index = list(index)
elif index:
    self._index = [index]

我就是这样工作的:

client = connections.create_connection(hosts=['myNode01:9200'])
s = Search().using(client)
      .index('myindex')
      .doc_type('mytype1')
      .doc_type('mytype2')
      .query('match', name='arandomname')