Skip to content
Merged
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
4 changes: 2 additions & 2 deletions app/controllers/base_documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def index
# Get similar documents but preserve existing filters
@documents = @documents.related_by_embedding(embedding)

# Sort by neighbor_distance using SQL to maintain ActiveRecord relation
@documents = @documents.order('neighbor_distance ASC')
# Force pure distance ordering to keep nearest-neighbor queries index-friendly.
@documents = @documents.reorder('neighbor_distance ASC')
else
# Only apply default sorting if not doing similarity search
@documents = if params[:sort] == 'questions'
Expand Down
3 changes: 1 addition & 2 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def unique_viewers
# Because of the ordering, having too many documents may cause the most relevant documents to be lost.
scope :related_by_embedding, lambda { |embedding, limit = nil|
limit ||= ENV.fetch('RELATED_DOCUMENTS_LIMIT', 25).to_i
scope = nearest_neighbors(:embedding, embedding, distance: 'euclidean')
scope.order(updated_at: :desc).limit(limit)
nearest_neighbors(:embedding, embedding, distance: 'euclidean').limit(limit)
}

# Default scope to only show non-deleted documents
Expand Down
Loading