Python Elasticsearch : Index mapping inconsistencies between 'es.index' and 'es.indices.create'

I'm trying to implement a custom index mapping (my_mapping) in Python, BUT, I do not get the expected index mapping after the python file is run!

file.py
----------
my_mapping = """
{
    "settings": {
        "number_of_shards": "1",
        "number_of_replicas": "1"
    },
    "mappings": {
        "properties": {
            "site": {
                "type": "completion",
            },
            "geometry": {
                "type": "geo_shape"
            }
        }
    }
}"""
result = es.index(index='my_index', document=my_mapping)

Expected Output:

{
  "mappings": {
    "_doc": {
      "properties": {
        "geometry": {
          "type": "geo_shape"
        },
        "site": {
          "type": "completion",
          "analyzer": "simple",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 200
        }
      }
    }
  }
}

Actual horrible output:

{
  "mappings": {
    "_doc": {
      "properties": {
        "mappings": {
          "properties": {
            "properties": {
              "properties": {
                "geometry": { ...........

BUT! NOTE that when I instead use the following python to create the index, I DO get the expected outcome.

result = es.indices.create(index='my_index', document=my_mapping)

Can someone please explain to me the inconsistency, and HOW to generate the expected outcome USING es.index

I think that es.index is meant to index documents. Not to call the create index API.

2 Likes

@dadoonet is correct

es.indices.create - API Documentation — Elasticsearch 7.15.1 documentation
es.index - API Documentation — Elasticsearch 7.15.1 documentation

1 Like

Thanks all for clarifying. I’ll stick to using es.indices.create

@warkolm - I forgot to mention but I also get a depreciation warning when using es.indices.create()

I’m not sure of how to rectify this

1 Like

Ahh not sure on that, hopefully one of the clients team will drop by and comment :slight_smile:

@warkolm I can answer my own question now. To circumvent the deprecation warning I use the explicit "mappings" parameter as opposed to the more generic "body" parameter. Eg:

es.indices.create(index='my_index', mappings="my_mapping")

No more warnings!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.