jsonmultidimensional-arrayinstantsearch.jstypesense

How to store array with key value in typesense


I am new in typesense, I create one schema for the geoloc in typesense.

Schema

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

I try to store with

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 ,  "long" : 29.25 },"links_count":1,"objectID": "1040"}'

But it returns an error

{"code":400,"document":"{","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing object key - unexpected end of input; expected string literal","success":false}
{"code":400,"document":"\t\"name\": \"Kalemie\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"city\": \"Kalemie\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"country\": \""Congo (Kinshasa)\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 11: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"iata_code\": \"FMI\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"_geoloc\" : [{\"lat\":-5.875556 , \"lng\": 29.25 }],","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 12: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"links_count\": 1,","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 15: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"objectID\": \"1040\"","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 12: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"}","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected '}'; expected '[', '{', or a literal","success":false}

I don't have any idea about how to store _geoloc with lat long.


Solution

  • You've to use a field of type geopoint.

    Here's an example from the docs: https://typesense.org/docs/0.21.0/api/documents.html#geosearch