【问题标题】:Spring Data Elasticsearch Geo Shape Type for Multipoint useSpring Data Elasticsearch Geo Shape Type for Multipoint 使用
【发布时间】:2014-10-24 19:32:19
【问题描述】:

我们目前正在使用 spring data 弹性搜索。我想创建一个 geo_shape 类型的字段,这样我就可以创建一个具有多个坐标 geoJson 点的多点字段。我看到该项目支持 GeoPointFields 但没有看到 Geo Shapes。

有没有办法指定geo_shape?

如果没有,我看到有一个自定义映射对象。我们可以在模板/映射中指定地理形状并使用一些自定义实体来映射我们需要的东西吗?

【问题讨论】:

  • 你有没有想过这个问题?我也在尝试将 Spring Data Elasticsearch 用于 Geo Shapes。

标签: spring-data-elasticsearch


【解决方案1】:

不知道这是否有帮助,但我所做的是建立一个类

import com.fasterxml.jackson.databind.JsonNode;
public static class GeoShape {
        String type;
        JsonNode coordinates;
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public JsonNode getCoordinates() {
            return coordinates;
        }

        public void setCoordinates(JsonNode coordinates) {
            this.coordinates = coordinates;
        }
    }

我有弹性搜索的地理形状映射,这适用于保存/检索的弹簧数据。

【讨论】:

    【解决方案2】:

    @m1416 的回答是正确的 - 但只有当你只是使用 ES 时才有效。

    你会发现如果你尝试将 JPA/Hibernate together 与 ES 一起使用,这是行不通的。 Hibernate 会抱怨它不能转换值。如果您搜索 json 节点的转换器类,您会发现几个类在提交后回滚步骤中不起作用或产生神秘的序列化错误。

    我一直在寻找许多小时来寻找如何让它以一种让您通过 ES 进行地理搜索而不会出现这些序列化错误的方式工作。

    这是一个有效的 JsonNodeConverter:

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import javax.persistence.AttributeConverter;
    import java.io.IOException;
    
    public class JsonNodeConverter implements AttributeConverter<JsonNode, String> {
    
        @Override
        public String convertToDatabaseColumn(JsonNode jsonNode){
            if (jsonNode == null  || jsonNode.asText() == null) {
                return null;
            }
            return jsonNode.asText();
    
        }
    
        @Override
        public JsonNode convertToEntityAttribute(String s) {
            if (s == null || s.length() == 0) {
                return null;
            }
            ObjectMapper mapper = new ObjectMapper();
            JsonNode jsonNode = null;
            try {
                jsonNode = mapper.readTree(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonNode;
        }
    }
    

    要使用它,请在您的实体的 JsonNode 属性上执行以下注释:

    @Column(name="geography", columnDefinition="LONGTEXT")
    @Convert(converter=JsonNodeConverter.class)
    private JsonNode geometry;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多