如何使用python Elasticsearch客户端建立父子关系?

时间:2016-06-19 16:11:39

标签: python django elasticsearch parent-child relationships

我正在使用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")

1 个答案:

答案 0 :(得分:0)

在子映射期间:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tblRadiologyData]
(
    [RadiologyID] [int] IDENTITY(1,1) NOT NULL,
    [ExaminationDate] [datetime] NOT NULL,
    [ReferralDate] [datetime] NULL,
    [ReportedDate] [datetime] NULL,
    [AttendanceNumber] [varchar](10) NULL,
    [LocalPatientIdentifier] [varchar](10) NOT NULL,
    [NHSNumber] [varchar](10) NULL,
    [Surname] [varchar](35) NULL,
    [Forenames] [varchar](35) NULL,
    [DateOfBirth] [datetime] NULL,
    [AttendanceStatus] [varchar](20) NULL,
    [AttendancePatientCategory] [varchar](10) NULL,
    [AttendancePatientGroup] [varchar](20) NULL,
    [AttendanceSpecialtyName] [varchar](50) NULL,
    [AttendancePriority] [varchar](10) NULL,
    [AttendanceSiteCode] [varchar](4) NULL,
    [ExamExaminationCode] [varchar](10) NOT NULL,
    [ExamRoomName] [varchar](50) NULL,
    [ExamExaminationName] [varchar](30) NULL,
    [ExamKornerCategory] [varchar](10) NULL,
    [KornerBandName] [varchar](20) NULL,
    [AttendanceSourceName] [varchar](30) NULL,
    [RefDoctor] [varchar](30) NULL,
    [DemogRegisteredGPCode] [varchar](8) NULL,
    [RegPracCode] [varchar](10) NULL,
    [Practice] [varchar](6) NULL,
    [DOHCode] [varchar](8) NULL,
    [PCOCode] [varchar](5) NULL,
    [ExamDuration] [int] NULL,
    [InternalNumber] [varchar](12) NULL,
    [Postcode] [varchar](8) NULL,
    [PCTRes] [varchar](5) NULL,
    [DHA] [varchar](3) NULL,
    [KornerBand] [varchar](2) NULL,
    [OPUnbundled] [bit] NOT NULL,
    [UB_HRG] [varchar](5) NULL,
    [StatusCode] [varchar](2) NULL,
    [LastModified] [datetime] NULL,
    [SpecialtyCode] [varchar](3) NULL,
    [deptcode] [varchar](255) NULL,
    [HRGCode] [varchar](5) NULL,
    [HRGGroup] [varchar](6) NULL,
    [HRGTariff] [decimal](19, 4) NULL,
    [Chargeable] [bit] NOT NULL,
    [HEYActivity] [varchar](10) NULL,
    [InternallyTraded] [varchar](3) NULL,
    [PatientSex] [nchar](10) NULL,
    [EthnicCategory] [nchar](10) NULL,
    [AgeAtExamDate] [int] NULL,
    [HRGCode1516] [varchar](5) NULL,

    CONSTRAINT [PK_tblRadiologyData] 
        PRIMARY KEY NONCLUSTERED ([ExaminationDate] ASC,
                                  [LocalPatientIdentifier] ASC,
                                  [ExamExaminationCode] ASC)
                    WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, 
                          ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[tblRadiologyData] 
    ADD DEFAULT ((0)) FOR [OPUnbundled]
GO

ALTER TABLE [dbo].[tblRadiologyData] 
    ADD DEFAULT ((0)) FOR [Chargeable]
GO
这里的父参数是父文档的ID,因此您不能将其用于您的目的,而是尝试:

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

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

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