Skip to content

feat: add support for script-based sort#10

Merged
harshit98 merged 8 commits intomasterfrom
script-based-sorting
May 21, 2025
Merged

feat: add support for script-based sort#10
harshit98 merged 8 commits intomasterfrom
script-based-sorting

Conversation

@harshit98
Copy link
Member

@harshit98 harshit98 commented May 10, 2025

Summary of Changes

  1. Introduced SortOption Interface:
  • Added a new interface SortOption to enable different types of sorting options.
  • Provided implementations for rawSortField and ScriptSortParams.
  1. Script-Based Sort Implementation:
  • Added ScriptSortParams to support script-based sorting, including its mapping to Elasticsearch-compatible formats.
  • Provided a Map method to generate the required structure for script-based sorting.
  1. New Sorting Methods in SearchRequest:
  • Added methods like ClearSort, SortScript, SortRaw, and SortByScript to enhance flexibility in specifying sort criteria.
  • These methods allow sorting by fields, scripts, or raw strings (e.g., _score).
  • ClearSort will help us avoid accumulation and re-use of old sorting options from previous request.
  1. Unit Tests for Script-Based Sorting:
  • Extended the test cases with various scenarios of script-based sorting.
  • Validated scenarios like sorting with scripts, sorting with raw fields, and combining multiple sorting criteria.

@harshit98
Copy link
Member Author

Wrote a sample test case locally, looks like the current implementation is considering the old existing sort parameters from the old request. Let's discuss how we can solve this out.

=== RUN   TestSortReuse
    sort_test.go:187: Second request: expected {"sort":[{"field2":{"order":"desc"}}]}, got {"sort":[{"field1":{"order":"asc"}},{"field2":{"order":"desc"}}]}
    sort_test.go:217: Third request: expected {"sort":[{"_script":{"order":"asc","script":{"lang":"painless","source":"doc['field_name'].value"},"type":"number"}}]}, got {"sort":[{"field1":{"order":"asc"}},{"field2":{"order":"desc"}},{"_script":{"order":"asc","script":{"lang":"painless","source":"doc['field_name'].value"},"type":"number"}}]}
    sort_test.go:236: Fourth request: expected {"sort":[{"_score":{}}]}, got {"sort":[{"field1":{"order":"asc"}},{"field2":{"order":"desc"}},{"_script":{"order":"asc","script":{"lang":"painless","source":"doc['field_name'].value"},"type":"number"}},{"_score":{}}]}
--- FAIL: TestSortReuse (0.00s)

@harshit98 harshit98 merged commit 086e335 into master May 21, 2025
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

Successfully merging this pull request may close these issues.

3 participants