-
Notifications
You must be signed in to change notification settings - Fork 322
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
PosgreSQL hybrid search #958
base: main
Are you sure you want to change the base?
Conversation
Include: - Text search index - Hybrid Search configuration - Vector or hybrid search
@microsoft-github-policy-service agree |
- Index name should be different per table. - Add missing filter
@@ -175,7 +183,8 @@ public async Task CreateTableAsync( | |||
{this._colContent} TEXT DEFAULT '' NOT NULL, | |||
{this._colPayload} JSONB DEFAULT '{{}}'::JSONB NOT NULL | |||
); | |||
CREATE INDEX IF NOT EXISTS idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_content ON {tableName} USING GIN(to_tsvector('english',{this._colContent})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For users already using KM + PG, their tables will lack this index. I'm assuming that if the index is missing, it will only affect performance, making hybrid search very slow, without causing any error.
@@ -175,7 +183,8 @@ public async Task CreateTableAsync( | |||
{this._colContent} TEXT DEFAULT '' NOT NULL, | |||
{this._colPayload} JSONB DEFAULT '{{}}'::JSONB NOT NULL | |||
); | |||
CREATE INDEX IF NOT EXISTS idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_content ON {tableName} USING GIN(to_tsvector('english',{this._colContent})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the language be configurable?
@@ -175,7 +183,8 @@ public async Task CreateTableAsync( | |||
{this._colContent} TEXT DEFAULT '' NOT NULL, | |||
{this._colPayload} JSONB DEFAULT '{{}}'::JSONB NOT NULL | |||
); | |||
CREATE INDEX IF NOT EXISTS idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_tags ON {tableName} USING GIN({this._colTags}); | |||
CREATE INDEX IF NOT EXISTS {tableName}_idx_content ON {tableName} USING GIN(to_tsvector('english',{this._colContent})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you update this file too https://github.com/microsoft/kernel-memory/blob/main/extensions/Postgres/README.md please?
@@ -413,6 +426,8 @@ DO UPDATE SET | |||
|
|||
// Column names | |||
string columns = withEmbeddings ? this._columnsListWithEmbeddings : this._columnsListNoEmbeddings; | |||
string columnsHibrid = this._columnsListHybrid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: var name typo columnsHibrid
=> columnsHybrid
string columnsHibrid = this._columnsListHybrid; | ||
string columnsListHybridCoalesce = this._columnsListHybridCoalesce; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these 2 vars could be removed, is there a reason to copy the values?
FROM {tableName}, plainto_tsquery('english', @query) query | ||
WHERE {filterSqlHybridText} AND to_tsvector('english', {this._colContent}) @@ query | ||
ORDER BY ts_rank_cd(to_tsvector('english', {this._colContent}), query) DESC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto: language should be configurable
COALESCE(1.0 / (60 + semantic_search.rank), 0.0) + | ||
COALESCE(1.0 / (60 + keyword_search.rank), 0.0) AS {colDistance} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you document or point to documentation explaining these formulas? e.g. what is 60
?
COALESCE(semantic_search.{this._colTags}, keyword_search.{this._colTags}) AS {this._colTags}, | ||
COALESCE(semantic_search.{this._colContent}, keyword_search.{this._colContent}) AS {this._colContent}, | ||
COALESCE(semantic_search.{this._colPayload}, keyword_search.{this._colPayload}) AS {this._colPayload}, | ||
COALESCE(semantic_search.{this._colEmbedding}, keyword_search.{this._colEmbedding}) AS {this._colEmbedding}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove the trailing comma (consistency with the way these values are used)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks almost ready to merge, only a few minor tweaks, please see the comments inline. Two most important:
- configurable language
- documenting the new SQL and the hard coded calculations
This PR Includes:
Motivation and Context (Why the change? What's the scenario?)
Vector search do not provide the best results in an important number of scenarios. Hybrid search provides better results.
High level description (Approach, Design)
This change includes a new parameter for activate hybrid search on PostgreSQL extension. This parameters defaults to the previous implementation (vector search).
The search will use vector search or hybrid search depending on the parameter.