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

标签 python elasticsearch

我在 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)推送 Action

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

最佳答案

参见: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(),
        }
    )

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

from elasticsearch_dsl import Index
from mytype import MyType

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

关于python - 如何使用 Python 在 ElasticSearch 中创建嵌套索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975217/

相关文章:

elasticsearch - logstash Elasticsearch 日期输出不同

python - 使用 Django 项目在 Docker 中创建 postgresql 数据库

python - 计算图像在屏幕上出现的次数

c# - 将对象序列化为 JSON,然后使用该对象在使用 NEST 的 Elasticsearch 中发送查询

java - 无法在elasticsearch 6.1.2的映射中插入 "index":"not_analyzed"

elasticsearch - 如何使用 NEST Bulk Api 将文档添加到 elasticsearch

python - 为什么这个多处理代码会失败?

python - 如何用渐变填充 matplotlib 条形图?

python - Python 中的 sqlite3 详细日志记录

elasticsearch - 适用于英国邮政编码的Elasticsearch映射,能够处理间距和大小写