我正在使用 ES 7.2.1 存储大量基于位置的数据并查询附近的位置。对于位置坐标,我使用我的 java 代码库中的 GeoPoint 字段。
ES:7.2.1
Spring Data Elasticsearch:4.0.0.DATAES-690-SNAPSHOT
MVN org.elasticsearch:7.2.1
模板:
curl -X PUT "localhost:9200/_template/store_locator_template?pretty"
{
"order": 1,
"index_patterns": [
"store_locator_*"
],
"settings": {
},
"mappings": {
"properties": {
"esId": {
"type": "keyword"
},
"geoPoint": {
"type": "geo_point"
},
"storeName": {
"type": "keyword"
}
}
}
}
尝试通过 bulkIndex() 插入数据时,出现此错误:
org.springframework.data.elasticsearch.ElasticsearchException:
Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments()
for detailed messages [{QObQeXEBqxAg6uMFyeNZ=ElasticsearchException
[Elasticsearch exception
[type=illegal_argument_exception, reason=
mapper [geoPoint] of different type,
current_type [geo_point], merged_type [ObjectMapper]]]}]
实体:
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "esId", callSuper = false)
@NoArgsConstructor
@Document(indexName = "store_locator_index", replicas = 0, createIndex = false)
public class EsEntity {
@Id
@Field(type = FieldType.Text)
private String esId;
@GeoPointField
private GeoPoint geoPoint;
@Field(type = FieldType.Text)
private String storeName;
}
更新:如果我使用下面的代码,它工作正常。它根据需要放置映射,并且 spring data es 没有抱怨!
//clazz -> entity class with @Document annotation
boolean indexCreated = false;
if (!elasticsearchOperations.indexExists(clazz)) {
indexCreated = elasticsearchOperations.createIndex(clazz);
}
if (indexCreated) {
elasticsearchOperations.refresh(clazz);
elasticsearchOperations.putMapping(clazz); --> Does the MAGIC
}
...从上面的代码生成的映射是:
{
"esentity":{ ---> Why is this here??
"properties":{
"esId":{
"type":"keyword",
"index":true
},
"geoPoint":{
"type":"geo_point"
}
}
}
}
它正在以我的实体类的名称向映射添加一个类型!
=====================
还.....
一切似乎都适用于:
ES:6.4.3
Spring Data Elasticsearch:3.1.X
我能够(通过模板)插入带有 GeoPoint 字段的文档。通过代码插入文档时会自动生成索引。同一组代码工作正常,没有错误!!!!
这是我的模板:
curl -X PUT "localhost:9200/_template/store_locator_template?pretty"
{
"order": 1,
"index_patterns": [
"store_locator_*"
],
"settings": {
},
"mappings": {
"store_locator_index": {
"properties": {
"esId": {
"type": "keyword"
},
"geoPoint": {
"type": "geo_point"
},
"storeName": {
"type": "keyword"
}
}
}
}
}
这是映射:
{
"mapping": {
"properties": {
"esId": {
"type": "keyword"
},
"geoPoint": {
"type": "geo_point"
}
}
}
}