首页 文章

弹性搜索非法参数异常

提问于
浏览
1

我有弹性搜索索引,里面有不同的类型 . 每种类型都包含一些默认字段和一些基于类型的额外字段 . 每种类型都有位置对象,存储lat和lon . 我的弹性搜索索引中的示例数据是

[{
    "_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
        }
    }
}]

我能够通过“sense”创建具有相同映射的索引,但是从java应用程序中获取错误 .

我使用“org.springframework.data.elasticsearch.core.ElasticsearchTemplate”从我的Java应用程序创建弹性搜索索引 .

我使用相同的ElasticsearchTemplate来保存和查询数据 .

当为具有不同映射文件的所有不同类型创建索引时,它是成功的但是在保存数据时,我收到错误

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

我对所有类型的“位置”都有相同的映射

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

仅供参考 - 这是我的es_index映射

{
"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": {}
}
}

它的原因是什么,我该如何解决?

1 回答

  • 0

    该错误消息表明您尝试在同一索引中以两种不同的方式(在不同的文档类型中)映射相同的字段名称 .

    • 这可能发生,因为您实际上写了这个重复的映射 .

    • 如果打开动态映射并且在更新使用相同名称的另一种类型的映射之前插入了包含该字段名称的文档,也可能发生这种情况 .

    您无法在同一索引中以两种不同的方式映射字段 .

    例如,如果你有......

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

    你以后不能拥有相同的索引......

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

    title字段在索引中只有一个映射定义 .

    Headers 字段为索引中的所有类型共享 .

    Types are not like database tables.

    • 他们共享Lucene索引中的字段 .

    • 它们有点像对大型共享索引的看法 .

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

    • 使索引中所有文档类型的位置字段的映射相同 . (对于像位置这样的东西,这似乎是一个好主意 . )

    • 在不同文档类型中为位置使用不同的字段名称 .

    • 对需要不同位置映射定义的文档类型使用单独的索引 .

    如果这只是动态映射进入并损坏索引的情况,那么请考虑将其关闭 . 想象一下,在 生产环境 中发生这种情况会多么快乐 .

相关问题