Search code examples
python-2.7elasticsearchkibana-4elasticsearch-mapping

Elasticsearch : es.index() changes the Mapping when message is pushed


I am trying to push some messages like this to elasticsearch

id=1
list=asd,bcv mnmn,kjkj, pop asd dgf

so, each message has an id field which is a string, and a list field that contains a list of string values

when i push this into elastic and try to create charts in kibana, the default analyzer kicks in and splits my list by the space character. Hence it breaks up my values. I tried to create a mapping for my index as

mapping='''
{ 
"test":
    { 
  "properties": {
        "DocumentID": {
          "type": "string"
        },
        "Tags":{
            "type" : "string",
          "index" : "not_analyzed"
        }
      }
    }
}'''

es = Elasticsearch([{'host': server, 'port': port}])
indexName = "testindex"    
es.indices.create(index=indexName, body=mapping)

so this should create the index with the mapping i defined. Now , i push the messages by simply

es.index(indexName, docType, messageBody)

but even now, Kibana breaks up my values! why was the mapping not applied ?

and when i do

GET /testindex/_mapping/test

i get

{
  "testindex": {
    "mappings": {
      "test": {
        "properties": {
          "DocumentID": {
            "type": "string"
          },
          "Tags": {
            "type": "string"
          }
        }
      }
    }
  }
}

why did the mapping change? How can i specify the mapping type when i do

es.index()

Solution

  • You were very close. You need to provide the root mappings object while creating the index and you dont need it when using _mapping end point and that is the reason put_mapping worked and create did not. You can see that in api.

    mapping = ''' 
    {
        "mappings": {
            "test": {
                "properties": {
                    "DocumentID": {
                        "type": "string"
                    },
                    "Tags": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
    '''
    

    Now this will work as expected

    es.indices.create(index=indexName, body=mapping)
    

    Hope this helps