首页 文章

用于多点使用的Spring Data Elasticsearch Geo Shape Type

提问于
浏览
4

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

有没有办法指定geo_shape?

如果没有,我看到有一个自定义映射对象 . 我们可以在模板/映射中指定Geo Shape并使用一些自定义实体来映射我们需要的内容 .

2 回答

  • 0

    Dunno如果这有帮助,但我所做的就是 Build 一个 class

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

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

  • 0

    @ m1416的答案是正确的 - 但只有在你使用ES时才有效 .

    您会发现,如果您尝试将JPA / Hibernate与ES一起使用,则不会转换为't work. Hibernate will complain that it can't . 如果您为json节点搜索转换器类,则在提交后回滚步骤中'll find several classes that don'工作或产生隐藏的序列化错误 .

    我一直在寻找很多时间来找到如何使这种方式工作,让您通过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;
    

相关问题