-
Notifications
You must be signed in to change notification settings - Fork 32
Use retrievers in search query #1835
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,125 @@ public ElasticsearchGateway(ElasticsearchOptions elasticsearchOptions, ILogger<E | |
| } | ||
|
|
||
| public async Task<(int TotalHits, List<SearchResultItem> Results)> SearchAsync(string query, int pageNumber, int pageSize, Cancel ctx = default) => | ||
| await ExactSearchAsync(query, pageNumber, pageSize, ctx); | ||
| await HybridSearchWithRrfAsync(query, pageNumber, pageSize, ctx); | ||
|
|
||
| public async Task<(int TotalHits, List<SearchResultItem> Results)> HybridSearchWithRrfAsync(string query, int pageNumber, int pageSize, Cancel ctx = default) | ||
| { | ||
| _logger.LogInformation("Starting RRF hybrid search for '{Query}' with pageNumber={PageNumber}, pageSize={PageSize}", query, pageNumber, pageSize); | ||
|
|
||
| var searchQuery = query.Replace("dotnet", "net", StringComparison.InvariantCultureIgnoreCase); | ||
|
|
||
| try | ||
| { | ||
| var response = await _client.SearchAsync<DocumentDto>(s => s | ||
| .Indices(_elasticsearchOptions.IndexName) | ||
| .Retriever(r => r | ||
| .Rrf(rrf => rrf | ||
| .Retrievers( | ||
| // Lexical/Traditional search retriever | ||
| ret => ret.Standard(std => std | ||
| .Query(q => q | ||
| .Bool(b => b | ||
| .Should( | ||
| // Tier 1: Exact/Prefix matches (highest priority) | ||
| sh => sh.Prefix(p => p | ||
| .Field("title.keyword") | ||
| .Value(searchQuery) | ||
| .CaseInsensitive(true) | ||
| .Boost(10.0f) // Highest importance - exact prefix matches | ||
| ), | ||
| // Tier 2: Title matching with AND operator | ||
| sh => sh.Match(m => m | ||
| .Field(f => f.Title) | ||
| .Query(searchQuery) | ||
| .Operator(Operator.And) | ||
| .Boost(8.0f) // High importance - all terms must match | ||
| ), | ||
| // Tier 3: Match bool prefix for partial matches | ||
| sh => sh.MatchBoolPrefix(m => m | ||
| .Field(f => f.Title) | ||
| .Query(searchQuery) | ||
| .Boost(6.0f) // Medium-high importance - partial matches | ||
| ), | ||
| // Tier 4: Abstract matching | ||
| sh => sh.Match(m => m | ||
| .Field(f => f.Abstract) | ||
| .Query(searchQuery) | ||
| .Boost(4.0f) // Medium importance - content matching | ||
| ), | ||
| // Tier 5: Parent matching | ||
| sh => sh.Match(m => m | ||
| .Field("parents.title") | ||
reakaleek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .Query(searchQuery) | ||
| .Boost(2.0f) // Lower importance - parent context | ||
| ), | ||
| // Tier 6: Fuzzy fallback | ||
| sh => sh.Match(m => m | ||
| .Field(f => f.Title) | ||
| .Query(searchQuery) | ||
| .Fuzziness(1) | ||
| .Boost(1.0f) // Lowest importance - fuzzy fallback | ||
| ) | ||
| ) | ||
| .MustNot(mn => mn.Terms(t => t | ||
| .Field("url.keyword") | ||
| .Terms(factory => factory.Value("/docs", "/docs/", "/docs/404", "/docs/404/")) | ||
| )) | ||
| .MinimumShouldMatch(1) | ||
| ) | ||
| ) | ||
| ), | ||
| // Semantic search retriever | ||
| ret => ret.Standard(std => std | ||
| .Query(q => q | ||
| .Bool(b => b | ||
| .Should( | ||
| // Title semantic search | ||
| sh => sh.Semantic(sem => sem | ||
| .Field("title.semantic_text") | ||
reakaleek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .Query(searchQuery) | ||
| .Boost(5.0f) // Higher importance - title semantic matching | ||
| ), | ||
| // Abstract semantic search | ||
| sh => sh.Semantic(sem => sem | ||
| .Field("abstract") | ||
| .Query(searchQuery) | ||
| .Boost(3.0f) // Medium importance - content semantic matching | ||
| ) | ||
| ) | ||
| .MustNot(mn => mn.Terms(t => t | ||
| .Field("url.keyword") | ||
| .Terms(factory => factory.Value("/docs", "/docs/", "/docs/404", "/docs/404/")) | ||
| )) | ||
| .MinimumShouldMatch(1) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| .RankConstant(60) // Controls how much weight is given to document ranking | ||
| ) | ||
| ) | ||
| .From((pageNumber - 1) * pageSize) | ||
| .Size(pageSize), ctx); | ||
|
|
||
| if (!response.IsValidResponse) | ||
| { | ||
| _logger.LogWarning("Elasticsearch RRF search response was not valid. Reason: {Reason}", | ||
| response.ElasticsearchServerError?.Error?.Reason ?? "Unknown"); | ||
| } | ||
| else | ||
| { | ||
|
||
| _logger.LogInformation("RRF search completed for '{Query}'. Total hits: {TotalHits}", query, response.Total); | ||
| } | ||
reakaleek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return ProcessSearchResponse(response); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "Error occurred during Elasticsearch RRF search for '{Query}'", query); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| public async Task<(int TotalHits, List<SearchResultItem> Results)> ExactSearchAsync(string query, int pageNumber, int pageSize, Cancel ctx = default) | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.