2015-11-18 16 views
4

我要創建使用蟒蛇elasticsearch_dsl這個例子如何在使用elasticsearch_dsl(Python)時指定文檔類型?同樣,如何指定幾個索引?

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

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

ok,它指定主機,端口和索引。

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

但我該如何指定文件類型是「產品」?似乎在Search()函數中應該有一個參數。

類似的問題,假設我想運行相同的查詢,但我希望它運行索引「my_store」和「her_store」。我該如何指定?

回答

5

你可以這樣使用。

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

指數PARAM需要listtuplestring類型。

在搜索構造函數中你可以看到

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

所以索引字段會使用字符串元組或只是一個字符串?它會需要一個字符串列表嗎? – travelingbones

+1

您可以使用元組或索引列表。 –

3

我剛剛得到它的工作是這樣的:

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