【问题标题】:how to make a parent child relationship using python Elasticsearch client?如何使用 python Elasticsearch 客户端建立父子关系?
【发布时间】:2016-06-19 16:11:38
【问题描述】:

我正在使用 python 的 elasticsearch 客户端来制作可搜索的 pdf。一组pdf称为调查。我想建立一个父子关系,其中父级由 pdf 组组成,子索引将是组内的文件名。但是,我不断收到错误。我的代码如下:

在 settings.py 中:

import elasticsearch
from elasticsearch import Elasticsearch, RequestsHttpConnection

ES_CLIENT = Elasticsearch(
    ['http://127.0.0.1:9200/'], #could be 9201,9300,9301
    connection_class=RequestsHttpConnection
)

在我的 command.py 中:

from elasticsearch import Elasticsearch
from django.conf import settings
self.indices_client = settings.ES_CLIENT
 print "create parent"
        self.indices_client.index(
            #   op_type='create',
                id='surveys',
                doc_type='parent',
                body={ "properties": { 'title': {'type': 'string', 'index': 'not_analyzed'}}},
                index="surveys" 
            )
        # create child index file_name with parent index surveys
        # self.indices_client.create(index=child_index)
        print 'create child'
        self.indices_client.index(
            doc_type='child',
            body= upload_models.Survey._meta.es_mapping,
            index=child_index,
            parent='surveys'

        )
        print 'post child'

我不断收到此错误:

raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u"Can't specify parent if no parent field has been configured")

【问题讨论】:

  • 您是如何创建索引的,您的映射是什么样的?你能用curl -XGET http://127.0.0.1:9200/surveys 的回复更新你的问题吗?

标签: python django elasticsearch parent-child relationships


【解决方案1】:

在子映射期间:

self.indices_client.index(
    doc_type='child',
    body= upload_models.Survey._meta.es_mapping,
    index=child_index,
    parent='surveys'
)

这里的parent参数是父文档的ID,所以你不能将它用于你的目的,而是尝试:

self.indices_client.index(
    doc_type='child',
    body= {
           doc_type: {
              '_parent': {"type": "surveys"},
              'properties': upload_models.Survey._meta.es_mapping
             }
           }
    index=child_index
)

或尝试其他功能 - put_mapping(*args, **kwargs):

self.indices_client.indices.put_mapping(
    doc_type='child',
    index=child_index,
    body= {
             doc_type: {
               '_parent': {"type": "surveys"},
               'properties': upload_models.Survey._meta.es_mapping
             }
          }
    index=child_index
   )

【讨论】:

    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 2016-01-18
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2016-11-21
    • 2013-02-20
    相关资源
    最近更新 更多