Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] TS error when using _geo_distance in sort #963

Closed
j0k3r opened this issue Jan 16, 2025 · 4 comments
Closed

[BUG] TS error when using _geo_distance in sort #963

j0k3r opened this issue Jan 16, 2025 · 4 comments
Assignees

Comments

@j0k3r
Copy link

j0k3r commented Jan 16, 2025

What is the bug?

TS doesn't allow the field with geo data to be added in the _geo_distance of the sort object when performing a search.

How can one reproduce the bug?

Re-using the example from the doc: https://opensearch.org/docs/latest/search-plugins/searching-data/sort/#sorting-by-geo-distance

import { Client, type API } from '@opensearch-project/opensearch';

const client = new Client({ node: 'http://localhost:9200' });

const searchRequest: API.Search_Request = {
  "sort": [
    {
      "_geo_distance": {
        "point": [59, -54],
        "order": "asc",
        "distance_type": "arc"
      }
    }
  ],
  "query": {
    "match_all": {}
  }
};
const searchResponseBody = (await client.search(searchRequest)).body; 

TS will raise:

error TS2322: Type '{ point: number[]; order: "asc"; distance_type: "arc"; }' is not assignable to type 'FieldSort'.
  Object literal may only specify known properties, and 'point' does not exist in type '{ format?: string | undefined; missing?: FieldValue; mode?: SortMode | undefined; nested?: NestedSortValue | undefined; numeric_type?: FieldSortNumericType | undefined; order?: SortOrder | undefined; unmapped_type?: FieldType | undefined; }'.

What is the expected behavior?

The _geo_distance should access the point field (like it was before 3.0.0, but maybe because it wasn't properly typed)

What is your host/environment?

MacOS, Node 22.

Do you have any additional context?

GeoDistanceSort type doesn't allow any additional key:

export type GeoDistanceSort = {
distance_type?: GeoDistanceType;
ignore_unmapped?: boolean;
mode?: SortMode;
order?: SortOrder;
unit?: DistanceUnit;
}

@nhtruong
Copy link
Collaborator

This looks a bug in the API generator since the JSON Schema of GeoDistanceSort on the spec is:

      type: object
      properties:
        mode:
          $ref: '#/components/schemas/SortMode'
        distance_type:
          $ref: '#/components/schemas/GeoDistanceType'
        ignore_unmapped:
          type: boolean
        order:
          $ref: '#/components/schemas/SortOrder'
        unit:
          $ref: '#/components/schemas/DistanceUnit'

Note that additionalProperties is not specified and should be understood as true.

Thanks for reporting this issue. I'll take a look at it and prepare a hotfix this week.

@nhtruong
Copy link
Collaborator

nhtruong commented Jan 22, 2025

This is actually a bug on the spec that has been resolved.

Rerunning the API Code gen and preparing for the next minor version release with changes from the spec that will resolve this.

@nhtruong
Copy link
Collaborator

3.2.0 has been released with the fix from the spec.

@j0k3r
Copy link
Author

j0k3r commented Jan 23, 2025

@nhtruong Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants