elasticsearch-py icon indicating copy to clipboard operation
elasticsearch-py copied to clipboard

Implementation of custom headers for attachment processor with CBOR

Open SkowRon96 opened this issue 11 months ago • 1 comments

Referring to the elasticsearch (https://www.elastic.co/guide/en/elasticsearch/reference/current/attachment.html#attachment-cbor) documentation, it is possible to create documents with attachments based on CBOR data. However, it is necessary to pass the header {'content-type': 'application/cbor'} - as in the example from the documentation:

import cbor2
import requests

file = 'my-file'
**headers = {'content-type': 'application/cbor'}**

with open(file, 'rb') as f:
  doc = {
    'data': f.read()
  }
  requests.put(
    'http://localhost:9200/my-index-000001/_doc/my_id?pipeline=cbor-attachment',
    data=cbor2.dumps(doc),
    **headers=headers**
  )

However, the Python elasticsearch client's methods for indexing documents have pre-defined headers (code snippet from the index method below), which makes it impossible to transfer our own headers to those necessary for CBOR. I would like to mention that in python elasticsearch 7 versions it was possible to pass this header.

    def index(
        ...
        __body = document if document is not None else body
        **__headers = {"accept": "application/json", "content-type": "application/json"}**
        return self.perform_request(  # type: ignore[return-value]
            __method,
            __path,
            params=__query,
            **headers=__headers,**
            body=__body,
            endpoint_id="index",
            path_parts=__path_parts,
        )

Python 3.13.1 elasticsearch 8.17.1

Is it possible to restore custom headers in the current version? However, if this is not possible, I would like to ask for a solution that allows the execution of the given action using the elasticsearch python client.

SkowRon96 avatar Jan 29 '25 09:01 SkowRon96

Thanks, I would like to add back support for custom headers. Usually the workaround for this is to use perform_request, but even that does not work because CBOR is a unknown serialization mechanism in the library.

pquentin avatar Feb 15 '25 04:02 pquentin