Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions langchain_iris/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,22 +521,37 @@ def similarity_search_with_score_by_vector(

# Execute the query and fetch the results
with Session(self._conn) as session:
results: Sequence[Row] = (
# Create the distance column explicitly to avoid textual label reference issues
distance_expr = (
self.distance_strategy(embedding)
if self.native_vector
else self.table.c.embedding.func(
self.distance_strategy, embedding
)
)

distance_col = distance_expr.label("distance")

# Note: For IRIS without modern pagination support, using .limit() with .order_by()
# doesn't include ORDER BY in the SQL (it only generates TOP N).
# So we fetch all results ordered and limit in Python.
query = (
session.query(
self.table,
(
self.distance_strategy(embedding).label("distance")
if self.native_vector
else self.table.c.embedding.func(
self.distance_strategy, embedding
).label("distance")
),
distance_col,
)
.filter(filter_by)
.order_by(asc("distance"))
.limit(k)
.all()
.order_by(distance_expr)
)

# Only add limit to SQL if dialect supports modern pagination
if self._conn.dialect.supports_modern_pagination:
query = query.limit(k)
results: Sequence[Row] = query.all()
else:
# For legacy pagination, fetch all and limit in Python
all_results = query.all()
results = all_results[:k]

documents_with_scores = [
(
Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pytest
testcontainers-iris>=1.3.0
langchain
langchain-community
iris-embedded-python-wrapper