繁体   English   中英

如何使用 Python 在 ElasticSearch 中创建嵌套索引?

[英]how to create a nested index in ElasticSearch with Python?

我在elasticsearch-py 中使用批量索引来添加包含字典数组形式的嵌套项的文档(在这种情况下为address ):

{'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)推送helpers.bulk(es, actions)

我想指定address是一个嵌套对象。 我在哪里用elasticsearch-py向 ES 表明这一点?

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

我使用了DSL 库 我创建了一个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(),
        }
    )

然后我包含这个文件并使用include_in_parent将映射放入 elasticsearch 以允许突出显示和其他:

from elasticsearch_dsl import Index
from mytype import MyType

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM