コード例 #1
0
    def index(self,
              index,
              body,
              doc_type='_doc',
              id=None,
              params=None,
              headers=None):
        if index not in self.__documents_dict:
            self.__documents_dict[index] = list()

        version = 1

        if id is None:
            id = get_random_id()
        elif self.exists(index, doc_type, id, params=params):
            doc = self.get(index, id, doc_type, params=params)
            version = doc['_version'] + 1
            self.delete(index, doc_type, id)

        self.__documents_dict[index].append({
            '_type': doc_type,
            '_id': id,
            '_source': body,
            '_index': index,
            '_version': version
        })

        return {
            '_type': doc_type,
            '_id': id,
            'created': True,
            '_version': version,
            '_index': index
        }
コード例 #2
0
    def index(self, index, doc_type, body, id=None, params=None):
        if index not in self.__documents_dict:
            self.__documents_dict[index] = list()

        if id is None:
            id = get_random_id()

        version = 1

        self.__documents_dict[index].append({
            '_type': doc_type,
            '_id': id,
            '_source': body,
            '_index': index,
            '_version': version
        })

        return {
            "_shards" : {
                "total" : 2,
                "failed" : 0,
                "successful" : 2
            },
            "_index" : index,
            "_type" : doc_type,
            "_id" : id,
            "_version" : version,
            "result" : "created"
        }
コード例 #3
0
ファイル: mock.py プロジェクト: Edraak/django-es-utils
    def index(self, index, doc_type, body, id=None, params=None):
        if index not in self.__documents_dict:
            self.__documents_dict[index] = list()

        if id is None:
            id = get_random_id()

        version = 1

        self.__documents_dict[index].append({
            '_type': doc_type,
            '_id': id,
            '_source': body,
            '_index': index,
            '_version': version
        })

        return {
            '_type': doc_type,
            '_id': id,
            'created': True,
            'result': 'created',
            '_version': version,
            '_index': index
        }
コード例 #4
0
    def bulk(self,  body, index=None, doc_type=None, params=None, headers=None):
        version = 1
        items = []

        for line in body.splitlines():
            if len(line.strip()) > 0:
                line = json.loads(line)

                if 'index' in line:
                    index = line['index']['_index']
                    doc_type = line['index']['_type']

                    if index not in self.__documents_dict:
                        self.__documents_dict[index] = list()
                else:
                    document_id = get_random_id()

                    self.__documents_dict[index].append({
                        '_type': doc_type,
                        '_id': document_id,
                        '_source': line,
                        '_index': index,
                        '_version': version
                    })

                    items.append({'index': {
                        '_type': doc_type,
                        '_id': document_id,
                        '_index': index,
                        '_version': version,
                        'result': 'created',
                        'status': 201
                    }})

        return {
            'errors': False,
            'items': items
        }
コード例 #5
0
    def bulk(self, body, index=None, doc_type=None, params=None, headers=None):
        items = []
        errors = False

        for raw_line in body.splitlines():
            if len(raw_line.strip()) > 0:
                line = json.loads(raw_line)

                if any(action in line
                       for action in ['index', 'create', 'update', 'delete']):
                    action = next(iter(line.keys()))

                    version = 1
                    index = line[action].get('_index') or index
                    doc_type = line[action].get(
                        '_type', "_doc")  # _type is deprecated in 7.x

                    if action in ['delete', 'update'
                                  ] and not line[action].get("_id"):
                        raise RequestError(
                            400, 'action_request_validation_exception',
                            'missing id')

                    document_id = line[action].get('_id', get_random_id())

                    if action == 'delete':
                        status, result, error = self._validate_action(
                            action,
                            index,
                            document_id,
                            doc_type,
                            params=params)
                        item = {
                            action: {
                                '_type': doc_type,
                                '_id': document_id,
                                '_index': index,
                                '_version': version,
                                'status': status,
                            }
                        }
                        if error:
                            errors = True
                            item[action]["error"] = result
                        else:
                            self.delete(index,
                                        document_id,
                                        doc_type=doc_type,
                                        params=params)
                            item[action]["result"] = result
                        items.append(item)

                    if index not in self.__documents_dict:
                        self.__documents_dict[index] = list()
                else:
                    if 'doc' in line and action == 'update':
                        source = line['doc']
                    else:
                        source = line
                    status, result, error = self._validate_action(
                        action, index, document_id, doc_type, params=params)
                    item = {
                        action: {
                            '_type': doc_type,
                            '_id': document_id,
                            '_index': index,
                            '_version': version,
                            'status': status,
                        }
                    }
                    if not error:
                        item[action]["result"] = result
                        if self.exists(index,
                                       document_id,
                                       doc_type=doc_type,
                                       params=params):
                            doc = self.get(index,
                                           document_id,
                                           doc_type=doc_type,
                                           params=params)
                            version = doc['_version'] + 1
                            self.delete(index,
                                        document_id,
                                        doc_type=doc_type,
                                        params=params)

                        self.__documents_dict[index].append({
                            '_type': doc_type,
                            '_id': document_id,
                            '_source': source,
                            '_index': index,
                            '_version': version
                        })
                    else:
                        errors = True
                        item[action]["error"] = result
                    items.append(item)
        return {'errors': errors, 'items': items}