一个有梦的程序海 2023-06-30 18:15 采纳率: 50%
浏览 12

Java中操作Elasticsearch,如何映射Mapping,用什么注解?

环境
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 ;
}

  • 写回答

2条回答 默认 最新

  • PhoenixRiser 2023-06-30 18:23
    关注

    TechWhizKid参考GPT回答:

    要在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注解,并将longitudelatitude属性替换为location属性,location使用了GeoPoint类型。GeoPointField用于在Elasticsearch中映射地理坐标字段。

    这样,当你创建索引和映射时,Elasticsearch将正确地映射经度和纬度,以便进行地理查询。

    另外,确保你的Elasticsearch版本(8.7.0)与你使用的Spring Data Elasticsearch版本(3.1.0)兼容。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月30日

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了