aboutsummaryrefslogtreecommitdiffstats
path: root/modules/indexer/code/elasticsearch
diff options
context:
space:
mode:
author6543 <m.huber@kithara.com>2024-03-16 11:32:45 +0100
committerGitHub <noreply@github.com>2024-03-16 10:32:45 +0000
commit1262ff6734543b37d834e63a6a623648c77ee4f4 (patch)
treecc99d1a96d094881a820659ea7fdf2d326b8a862 /modules/indexer/code/elasticsearch
parente0ea3811c4178ffa30452b7ca4bd211e59326f91 (diff)
downloadgitea-1262ff6734543b37d834e63a6a623648c77ee4f4.tar.gz
gitea-1262ff6734543b37d834e63a6a623648c77ee4f4.zip
Refactor code_indexer to use an SearchOptions struct for PerformSearch (#29724)
similar to how it's already done for the issue_indexer --- *Sponsored by Kithara Software GmbH*
Diffstat (limited to 'modules/indexer/code/elasticsearch')
-rw-r--r--modules/indexer/code/elasticsearch/elasticsearch.go26
1 files changed, 11 insertions, 15 deletions
diff --git a/modules/indexer/code/elasticsearch/elasticsearch.go b/modules/indexer/code/elasticsearch/elasticsearch.go
index 065b0b2061..e4622fd66e 100644
--- a/modules/indexer/code/elasticsearch/elasticsearch.go
+++ b/modules/indexer/code/elasticsearch/elasticsearch.go
@@ -281,18 +281,18 @@ func extractAggs(searchResult *elastic.SearchResult) []*internal.SearchResultLan
}
// Search searches for codes and language stats by given conditions.
-func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
+func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
searchType := esMultiMatchTypePhrasePrefix
- if isFuzzy {
+ if opts.IsKeywordFuzzy {
searchType = esMultiMatchTypeBestFields
}
- kwQuery := elastic.NewMultiMatchQuery(keyword, "content").Type(searchType)
+ kwQuery := elastic.NewMultiMatchQuery(opts.Keyword, "content").Type(searchType)
query := elastic.NewBoolQuery()
query = query.Must(kwQuery)
- if len(repoIDs) > 0 {
- repoStrs := make([]any, 0, len(repoIDs))
- for _, repoID := range repoIDs {
+ if len(opts.RepoIDs) > 0 {
+ repoStrs := make([]any, 0, len(opts.RepoIDs))
+ for _, repoID := range opts.RepoIDs {
repoStrs = append(repoStrs, repoID)
}
repoQuery := elastic.NewTermsQuery("repo_id", repoStrs...)
@@ -300,16 +300,12 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
}
var (
- start int
- kw = "<em>" + keyword + "</em>"
- aggregation = elastic.NewTermsAggregation().Field("language").Size(10).OrderByCountDesc()
+ start, pageSize = opts.GetSkipTake()
+ kw = "<em>" + opts.Keyword + "</em>"
+ aggregation = elastic.NewTermsAggregation().Field("language").Size(10).OrderByCountDesc()
)
- if page > 0 {
- start = (page - 1) * pageSize
- }
-
- if len(language) == 0 {
+ if len(opts.Language) == 0 {
searchResult, err := b.inner.Client.Search().
Index(b.inner.VersionedIndexName()).
Aggregation("language", aggregation).
@@ -330,7 +326,7 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
return convertResult(searchResult, kw, pageSize)
}
- langQuery := elastic.NewMatchQuery("language", language)
+ langQuery := elastic.NewMatchQuery("language", opts.Language)
countResult, err := b.inner.Client.Search().
Index(b.inner.VersionedIndexName()).
Aggregation("language", aggregation).