【问题标题】:Elastic search illegal argument exception弹性搜索非法参数异常
【发布时间】:2017-04-18 20:27:02
【问题描述】:

我有不同类型的弹性搜索索引。 并且每种类型都包含一些默认字段和一些基于类型的额外字段。 每种类型都有存储纬度和经度的位置对象。 我在弹性搜索索引中的示例数据是

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

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

【问题讨论】:

  • 你能发布你的索引映射吗? curl localhost:9200/es_index?pretty
  • @fylie:我编辑了问题

标签: java spring elasticsearch


【解决方案1】:

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

  • 这可能是因为您实际上编写了此重复映射。
  • 如果动态映射已打开,并且您在为使用相同名称的其他类型更新映射之前插入了包含该字段名称的文档,也可能发生这种情况。

您不能在同一个索引中以两种不同的方式映射字段。

例如,如果您有...

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

您以后不能在同一个索引中...

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

标题字段在索引中只有一个映射定义。

标题字段为索引中的所有类型共享。

类型不像数据库表。

  • 它们共享 Lucene 索引中的字段。
  • 它们有点像大型共享索引的视图。

如果您遇到这种情况,您有三个选择:

  1. 使位置字段的映射对于索引中的所有文档类型都相同。 (这对于位置之类的东西来说似乎是个好主意。)

  2. 对不同文档类型中的位置使用不同的字段名称。

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

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

【讨论】:

  • 我想这个问题之前已经回答过很多次了。对于刚接触 Elasticsearch 并期望它表现得像典型数据库的人来说,这是第一次粗鲁的觉醒。
  • 在我的情况下,第二和第三选择被排除在外。我对所有类型都有相同的映射。 "location": { "geohash": true, "lat_lon": true, "store": true, "type": "geo_point" } 但我仍然收到此错误,还有其他可能发生这种情况的原因吗?
  • 我也更新了这个问题。当我尝试使用“sense”时,我能够创建具有相同映射的索引。但是java应用程序并没有发生同样的情况。 @joshp
  • @Raghavendra 如果 GET /yourindex/_mapping 检索到的实际映射对于每种类型的字段具有相同的属性,并且您尝试添加的映射也相同,那么我不知道为什么会抱怨。在实践中,动态映射通常是一个问题,有人在应用您的映射之前插入了一个文档。但如果那没有发生,我不知道。
  • 我刚刚修改了问题并添加了我的es索引映射。
猜你喜欢
  • 1970-01-01
  • 2012-08-23
  • 2014-11-13
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2017-03-31
相关资源
最近更新 更多