2015-10-06 117 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

相关问题