|
@@ -152,8 +152,27 @@ class PGVector(BaseVector):
|
|
|
return docs
|
|
|
|
|
|
def search_by_full_text(self, query: str, **kwargs: Any) -> list[Document]:
|
|
|
- # do not support bm25 search
|
|
|
- return []
|
|
|
+ top_k = kwargs.get("top_k", 5)
|
|
|
+
|
|
|
+ with self._get_cursor() as cur:
|
|
|
+ cur.execute(
|
|
|
+ f"""SELECT meta, text, ts_rank(to_tsvector(coalesce(text, '')), to_tsquery(%s)) AS score
|
|
|
+ FROM {self.table_name}
|
|
|
+ WHERE to_tsvector(text) @@ plainto_tsquery(%s)
|
|
|
+ ORDER BY score DESC
|
|
|
+ LIMIT {top_k}""",
|
|
|
+ # f"'{query}'" is required in order to account for whitespace in query
|
|
|
+ (f"'{query}'", f"'{query}'"),
|
|
|
+ )
|
|
|
+
|
|
|
+ docs = []
|
|
|
+
|
|
|
+ for record in cur:
|
|
|
+ metadata, text, score = record
|
|
|
+ metadata["score"] = score
|
|
|
+ docs.append(Document(page_content=text, metadata=metadata))
|
|
|
+
|
|
|
+ return docs
|
|
|
|
|
|
def delete(self) -> None:
|
|
|
with self._get_cursor() as cur:
|