opensearch-project / opensearch-project/sql
[FEATURE] Add K-NN vector and query support to SQL Plugin
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 176
- Forks
- 229
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 43
Description
Is your feature request related to a problem?
The k-nn, or k-nearest neighbours plugin enables users to define knn_vector objects and perform nearest-neighbour queries on that objects in OpenSearch.
What solution would you like?
We want to add the following features:
knn_vectortype to the list of supported OS-SQL types- Add
Approximate k-NNto the list of supported OpenSearch query functions - Add
Script Score k-NNto the list of supported OpenSearch scripted functions, using thescriptfield. - Add
k-NN’s Painless extensionsas part of the script score k-nn
Questions:
TODO: determine if we can use theScriptScoreFunctionprovided by OpenSearch.- Determine which syntax 'makes sense' for each language. It seems simpler to use the
whereclause in PPL (with multiple `where clauses) to denote pre- and post- filter processing of the knn function. This makes less sense in SQL - and we may consider using the window functions or ordering the WHERE clause on purpose.
Proposed PPL syntax
knnandknn_scorefalls into thewherecommand. We can chain multiplewherecommands for pre-filter and post-filter processing.
where <pre-filter expression> | where knn_score(field, vector, k) | where <post-filter expression> | top size
Proposed SQL Syntax:
Option 1) use OpenSearch functions to call knn in the ORDER BY clause.
WHERE <pre-filter expression> ORDER BY knn(field, vector, k, <post-filter expression>) LIMIT size
- Approximate knn:
ORDER BY knn(field, vector, k) - Approximate knn with pre-filter query:
WHERE expression ORDER BY knn(field, vector, k) - Approximate knn with post-filter query:
ORDER BY knn(field, vector, k, expression)
WHERE <pre-filter expression> AND knn_score(field, vector, k) AND <post-filter expression> LIMIT size
Option 2) USe pgvector syntax to map to OpenSearch DSL functions
see: pgvector
Do you have any additional context?
reference:
- https://opensearch.org/docs/latest/search-plugins/knn/index/
- https://github.com/opensearch-project/sql/issues/1412
Proposed Schedule
- Add knn_vector datatype to OS-SQL and OS-PPL
- Add exact knn_score to PPL language
- Add approximate knn to PPL language with filtering
- Port syntax to SQL
- Add pgvector syntax to SQL
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the OpenSearch k-NN references and the related issue #1412, then inspect the SQL and PPL language areas for supported data types, query functions, and scripted functions. The work is done when knn_vector, exact and approximate k-NN with filtering, script-score extensions, and the agreed SQL syntax are supported and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, sql
- Domain
- backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100