Skip to content
Merged
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
26 changes: 24 additions & 2 deletions app/Queries/CommitSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,43 @@ public function get(string $term, ?User $viewer, int $limit = 20): Collection
->generate(provider: Lab::OpenAI, model: OpenAI::EMBEDDING_MODEL)
->first();

$vector = array_values($embedding);

$candidates = Commit::query()
->whereIn('commits.id', $this->nearest($vector, $viewer, $limit * 2))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
->visibleTo($viewer)
->withoutMutedAuthorsFor($viewer)
->whereNotNull('embedding')
->withVisibleReplyCount($viewer)
->withContinuationId($viewer)
->withCardDataFor($viewer)
->orderByVectorDistance('embedding', array_values($embedding))
->orderByVectorDistance('embedding', $vector)
->limit($limit)
->get()
->toBase();

return $this->onlyRelevant($term, $candidates);
}

/**
* The nearest commits are looked up on the vector index before the blocks and mutes are checked. With those checks
* in the same query, the database expects so few commits to match that it compares the term against every
* embedding instead, reading all of them from disk. Twice as many as asked for leaves room for the ones the checks
* then leave out.
*
* @param list<float> $vector
*/
private function nearest(array $vector, ?User $viewer, int $count): CommitBuilder
{
return Commit::query()
->select('id')
->where(fn (CommitBuilder $query): CommitBuilder => $query
->approved()
->when($viewer, fn (CommitBuilder $query, User $viewer): CommitBuilder => $query->orWhere('user_id', $viewer->id)))
->whereNotNull('embedding')
->orderByVectorDistance('embedding', $vector)
->limit($count);
}

/**
* The vector search is generous: the nearest by meaning can include posts that merely share a word with the term.
* The decision model reads the term against each candidate and keeps meaningful connections to it, so the
Expand Down
Loading