Search code examples
node.jstypescripttypesense

Receiving RequestMalformed error when doing Typesense upsert


I have the following interface in typescript:

export interface TypesenseAtlistedProEvent {
  // IDs
  id: string;
  proId: string;
  eventId: string;
  startTime: Number;
  stopTime: Number;
  eventRate: Number;
  remainingSlots: Number;
  displayName: string;
  photoURL: string;
  indexOptions: string;
  location: Number[];
}

and the following schema in Typesense:

{
  "created_at": 1665530883,
  "default_sorting_field": "location",
  "fields": [
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "proId",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "eventId",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "startTime",
      "optional": false,
      "sort": true,
      "type": "int64"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "stopTime",
      "optional": false,
      "sort": true,
      "type": "int64"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "eventRate",
      "optional": false,
      "sort": true,
      "type": "float"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "remainingSlots",
      "optional": false,
      "sort": true,
      "type": "int32"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "displayName",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "photoURL",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "indexOptions",
      "optional": false,
      "sort": false,
      "type": "string"
    },
    {
      "facet": false,
      "index": true,
      "infix": false,
      "locale": "",
      "name": "location",
      "optional": false,
      "sort": true,
      "type": "geopoint"
    }
  ],
  "name": "atlistedProEventIndex",
  "num_documents": 0,
  "symbols_to_index": [],
  "token_separators": []
}

I look to upsert like the in the following:

const indexedDoc: TypesenseAtlistedProEvent = {
      id: proId + eventId,
      proId: proId,
      eventId: eventId,
      startTime: publicEvent.startTime.seconds,
      stopTime: publicEvent.stopTime.seconds,
      eventRate: publicEvent.eventRate,
      remainingSlots: publicEvent.remainingSlots,
      displayName: tpi.displayName,
      photoURL: tpi.photoURL,
      indexOptions: tpi.indexOptions,
      location: [tpi.lat, tpi.lng],
    };

return await typesenseClient
        .collections("atlistedProEventIndex")
          .documents()
          .upsert(indexedDoc)
          .then(() => {
            return {success: true, exit: 0};
          })

I am getting the following upon the query:

RequestMalformed: Request failed with HTTP code 400 | Server said: [json.exception.type_error.302] type must be number

I am passing it location as Number[], and trying to get that to update the geopoint in typesense. This is not working and thus it would be useful if:

  1. I was able to locate the logs to go through. I would particularly like the logs given by the Typesense Cloud, and am feeling at a loss that I cannot find these.

  2. I would like to pass in the geopoint as the right type in typescript. Right now, as you can see above, the location is of type Number[], which, from the examples I saw, assumed was right. It also may be the case that another field is off and I'm just missing it. Either way, I could really use some kind of server side logging coming from Typesense Cloud.


Solution

  • The error message is a little confusing, but the core of the issue is that the default_sorting_field can only be a numeric field, but it's currently set as a geopoint field (location), which is what that error is trying to convey.

    So if you create a new collection without default_sorting_field, the error should not show up.

    If you want to sort by geo location, you want to use the sort_by parameter: https://typesense.org/docs/0.23.1/api/geosearch.html#searching-within-a-radius

    let searchParameters = {
      'q'         : '*',
      'query_by'  : 'title',
      'filter_by' : 'location:(48.90615915923891, 2.3435897727061175, 5.1 km)',
      'sort_by'   : 'location(48.853, 2.344):asc'
    }
    
    client.collections('companies').documents().search(searchParameters)