环境
jdk17
SpringBoot 3.1.0
Elasticsearch 8.7.0
Java的Doc对象中如何正确映射我的经度纬度,进行创建Index和Mapping,以便于后期做坐标geo查询?
@Data
@Accessors(chain = true)
@Schema(name = "帖子文档",description = "")
@Document(indexName = "post_index")
public class PostDoc {
/** 文章id */
@Id
@Schema(description = "文章id")
private Long postId ;
/** 用户id */
@Field(type = FieldType.Integer)
@Schema(description = "用户id")
private Long userId ;
/** 墙类型 */
@Schema(description = "墙类型")
@Field(type = FieldType.Integer)
private Integer wallTypeId ;
/** 内容;针对一些特殊的墙类型存放特定数据用的。例如 @ta的信息 */
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
@Schema(description = "内容")
private String content ;
/** 声音 */
@Field(type = FieldType.Text,index = false)
@Schema(description = "声音")
private String soundUrl ;
/** 图片url */
@Field(type = FieldType.Text,index = false)
@Schema(description = "图片url")
private String imagesUrl ;
/** 其他信息 */
@Schema(description = "其他信息")
@Field(type = FieldType.Text,index = false)
private String other ;
/** 学校id */
@Field(type = FieldType.Integer)
@Schema(description = "学校id")
private Long schoolId ;
/** 省份 */
@Field(type = FieldType.Text)
@Schema(description = "省份")
private String province ;
/** 市 */
@Field(type = FieldType.Text)
@Schema(description = "市")
private String municipal ;
/** 详细地址 */
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
@Schema(description = "详细地址")
private String detailedAddress ;
/** 经度 */
@Schema(description = "经度")
private Double longitude ;
/** 纬度 */
@Schema(description = "纬度")
private Double latitude ;
/** 是否公开地址 */
@Field(type = FieldType.Boolean)
@Schema(description = "是否公开地址")
private Boolean isPublicAddress ;
/** 是否匿名 */
@Field(type = FieldType.Boolean)
@Schema(description = "是否匿名")
private Boolean isAnonymous ;
/** 文章状态;0 待审核 1 通过 2 违规 */
@Field(type = FieldType.Integer)
@Schema(description = "文章状态")
private Integer state ;
}
要在Java的Doc对象中正确映射经度和纬度,以便后续进行坐标地理查询,你可以使用Elasticsearch的地理坐标类型。以下是修改后的代码示例:
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
@Data
@Accessors(chain = true)
@Schema(name = "帖子文档", description = "")
@Document(indexName = "post_index")
public class PostDoc {
// ...
/** 经度和纬度 */
@GeoPointField
@Schema(description = "经度和纬度")
private GeoPoint location;
// ...
public void setLongitude(Double longitude) {
if (location == null) {
location = new GeoPoint();
}
location.setLon(longitude);
}
public void setLatitude(Double latitude) {
if (location == null) {
location = new GeoPoint();
}
location.setLat(latitude);
}
public Double getLongitude() {
return location != null ? location.getLon() : null;
}
public Double getLatitude() {
return location != null ? location.getLat() : null;
}
}
代码中新增了一个GeoPointField
注解,并将longitude
和latitude
属性替换为location
属性,location
使用了GeoPoint
类型。GeoPointField
用于在Elasticsearch中映射地理坐标字段。
这样,当你创建索引和映射时,Elasticsearch将正确地映射经度和纬度,以便进行地理查询。
另外,确保你的Elasticsearch版本(8.7.0)与你使用的Spring Data Elasticsearch版本(3.1.0)兼容。
这里衔接上面的代码
ImmutableOpenMap<String, MappingMetaData> mapping = mappings.get("your index");