You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Typesense中存储带经纬度的_geoloc键值数组并解决导入报错

解决方案

你遇到的问题由三个错误共同导致,按优先级修正即可:

1. 集合Schema的地理位置字段类型定义错误

Typesense内置了专用的geopoint类型用于存储经纬度数据,不需要自行定义为string[]。如果需要存储单个经纬度用geopoint类型,存储多个经纬度用geopoint[]类型,修正后的创建集合代码如下:

curl "http://localhost:8108/collections" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: xyz" \
        -d '{
              "name": "airports4",
              "fields": [
                {"name": "name", "type": "string" },
                {"name": "city", "type": "string" },
                {"name": "country", "type": "string" },
                {"name": "iata_code", "type": "string" },
                {"name": "_geoloc", "type": "geopoint" , "facet": true },
                {"name": "links_count", "type": "int32" },
                {"name": "objectID", "type": "string" }
              ]
            }'

注意:这里把集合名同步改为了你导入时用的airports4,避免集合不匹配的问题

2. 批量导入接口的请求格式错误

你使用的/collections/{集合名}/documents/import是批量导入接口,要求请求体为JSON Lines格式:即每一行是一个完整的独立JSON对象,不能有换行缩进的格式化JSON。你把单个文档的JSON拆成了多行,导致Typesense把每一行都当成单独的文档解析,才会报每行的JSON语法错误。

如果只需要导入单个文档,直接用单文档写入接口即可,避免JSON Lines格式的限制,请求代码如下:

curl "http://localhost:8108/collections/airports4/documents" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: xyz" \
        -d '{"name": "Kalemie","city": "Kalemie","country": "Congo (Kinshasa)","iata_code": "FMI","_geoloc": { "lat" : -5.875556 ,  "lng" : 29.25 },"links_count":1,"objectID": "1040"}'

3. 经纬度字段键名错误

Typesense的geopoint类型只识别lat(纬度)和lng(经度)作为键名,你原来写的long会被当成无效字段,需要改为lng。

如果需要使用批量导入接口,按JSON Lines格式调整请求体即可,示例如下:

curl "http://localhost:8108/collections/airports4/documents/import" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: xyz" \
        -d '{"name": "Kalemie","city": "Kalemie","country": "Congo (Kinshasa)","iata_code": "FMI","_geoloc": { "lat" : -5.875556 ,  "lng" : 29.25 },"links_count":1,"objectID": "1040"}
{"name": "其他机场","city": "xxx","country": "xxx","iata_code": "XXX","_geoloc": { "lat" : 12.345 ,  "lng" : 67.89 },"links_count":2,"objectID": "1041"}'

每个文档占独立一行,不要有换行和缩进

内容的提问来源于stack exchange,提问作者jacklogan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 08:51:00