2015-10-06 64 views
1

我正在使用elasticsearch-py散貨指數添加包含嵌套項在字典中的數組形式(address在這種情況下)文件:如何在Python中使用ElasticSearch創建嵌套索引?

{'Name': 'Smith, John', 
'ID': '3327', 
'Nationality': 'English', 
'address': [ 
     { 
     'ID': '5', 
     'City': 'Milwaukee', 
     'Latlon': '43.0526,-87.9199', 
     'State': 'WI' 
     }, 
    ... 
    ] 
} 

我創建的動作列表,一個像這樣每個文檔:

{ 
    "_index": "myindex", 
    "_type": "mytype", 
    "_source": the_doc 
} 

,然後通過helpers.bulk(es, actions)

我想指定address是一個嵌套的對象推的動作。我在哪裏向ES註明elasticsearch-py

回答

1

看到:https://github.com/elastic/elasticsearch-py/issues/278#issuecomment-145923743

我用DSL library。我創建了一個mytype.py文件:

from elasticsearch_dsl import DocType, Nested, String 

class MyType(DocType): 
    name = String() 
    nationality = String() 

    address = Nested(
     include_in_parent=True, 
     properties={ 
      'location': String(), 
      'state': String(), 
      'city': String(), 
     } 
    ) 

然後,我有這個文件,並把映射到elasticsearch與include_in_parent允許突出及其他:

from elasticsearch_dsl import Index 
from mytype import MyType 

myindex = Index('myindex') 
myindex.doc_type(MyType) 
myindex.create() 
+0

既然你指定的文檔類型索引的一部分,你是怎麼繼續使用helpers.bulk的? – dter

+1

@dter我在兩個階段做到了這一點:首先我按照描述創建索引本身,然後用適當的結構構建動作數組(我的動作不使用自定義類,但使用字典),並通過'helpers.bulk' 。自定義類可確保操作在索引中正確映射。不知道這是否是正確的做法,但它適用於我 – mga

相關問題