1
votes

I have elastic search index with different types in it. And every type contains some default fields and some extra fields based on type. Every type will have location object which stores lat and lon. Sample data that I have in my elastic search index is

[{
    "_index": "es_index",
    "_type": "type1",
    "_id": "id1",
    "_source": {
        "name": "name",
        "type1field": "value",
        "location": {
            "lat": 1,
            "lon": 1
        }
    }
}, {
    "_index": "es_index",
    "_type": "type2",
    "_id": "id2",
    "_source": {
        "name": "name",
        "type2field": "value",
        "location": {
            "lat": 1,
            "lon": 1
        }
    }
}]

I was able to create the index with same mappings through "sense" but getting an error from java application.

I am using "org.springframework.data.elasticsearch.core.ElasticsearchTemplate" for creating the elastic search index from my Java application.

I am using the same ElasticsearchTemplate to save and query the data.

When creating index for all the different types with different mapping files, it is successful but while saving the data, I am getting error as

java.lang.IllegalArgumentException: [location] is defined as an object in mapping [esIndex] but this name is already used for a field in other types

And I have same mapping for "location" in all the types

"location": {
                "geohash": true,
                "lat_lon": true,
                "store": true,
                "type": "geo_point"
            }

FYI- Here is my es_index mapping

{
"es_index": {
    "aliases": {
        "es_index1": {}
    },
    "mappings": {

        "type1": {
            "properties": {
                "field1": {
                    "type": "string",
                    "analyzer": "nGram_analyzer",
                    "search_analyzer": "whitespace_analyzer"
                },
                "field2": {
                    "type": "string",
                    "index": "no"
                },
                "field3": {
                    "type": "string",
                    "analyzer": "nGram_analyzer",
                    "search_analyzer": "whitespace_analyzer"
                },
                "field4": {
                    "type": "string",
                    "index": "no"
                },
                "location": {
                    "type": "geo_point",
                    "store": true,
                    "lat_lon": true,
                    "geohash": true
                }
            }
        },

        "type2": {
            "properties": {
                "field1": {
                    "type": "string",
                    "analyzer": "nGram_analyzer",
                    "search_analyzer": "whitespace_analyzer"
                },
                "field5": {
                    "type": "string",
                    "analyzer": "nGram_analyzer",
                    "search_analyzer": "whitespace_analyzer",
                    "include_in_all": true
                },
                "field4": {
                    "type": "string",
                    "index": "no"
                },
                "location": {
                    "type": "geo_point",
                    "store": true,
                    "lat_lon": true,
                    "geohash": true
                },
                "field6": {
                    "type": "string",
                    "index": "no"
                }

            }



        }
    },
    "settings": {
        "index": {
            "creation_date": "1491466792565",
            "include_in_all": "false",
            "uuid": "uuid....",
            "number_of_replicas": "1",
            "analysis": {
                "filter": {
                    "nGram_filter": {
                        "max_gram": "75",
                        "type": "edgeNGram",
                        "min_gram": "2",
                        "token_chars": [
                            "letter",
                            "digit",
                            "punctuation",
                            "symbol"
                        ]
                    }
                },
                "analyzer": {
                    "nGram_analyzer": {
                        "type": "custom",
                        "filter": [
                            "lowercase",
                            "asciifolding",
                            "nGram_filter"
                        ],
                        "tokenizer": "keyword"
                    },
                    "whitespace_analyzer": {
                        "type": "custom",
                        "filter": [
                            "lowercase",
                            "asciifolding"
                        ],
                        "tokenizer": "keyword"
                    }
                }
            },
            "number_of_shards": "8",
            "version": {
                "created": "2040499"
            }
        }
    },
    "warmers": {}
}
}

What is the reason for it and how can I resolve it?

1
Can you post your index mapping? curl localhost:9200/es_index?prettyfylie
@fylie: I edited the questionRaghavendra

1 Answers

0
votes

The error message suggests that you are trying to map the same field name two different ways (in different document types) in the same index.

  • This could happen because you actually wrote this duplicate mapping.
  • It could also happen if dynamic mapping is turned on and you inserted a document containing that field name before updating the mapping for another type that uses the same name.

You cannot map a field two different ways in the same index.

For example, if you have...

  "mappings": {
    "books": {
      "properties":{
        "title":{
          "type":"text"
        },
       ...

You cannot later have in the same index...

    "films": {
      "properties":{
        "title":{
          "type":"keyword"
        }
       }

The title field has only one mapping definition in the index.

The title field is shared for all types in the index.

Types are not like database tables.

  • They share fields in the Lucene index.
  • They are a bit like views into a big shared index.

If this is what you are experiencing you have three choices:

  1. Make the mappings for the location field identical for all document types in the index. (This seems like a good idea for something like location.)

  2. Use different field names for location in the different document types.

  3. Use a separate index for the document types that need a different mapping definition for location.

If this is just a case of dynamic mapping getting in there and spoiling your index, well, consider turning it off. Imagine how joyful it would be to have that happen in production.