Skip to content

Commit cb01b86

Browse files
Add open/closed field support for issue index (#25708)
A couple of notes: * Future changes should refactor arguments into a struct * This filtering only is supported by meilisearch right now * Issue index number is bumped which will cause a re-index
1 parent 7586b58 commit cb01b86

File tree

12 files changed

+51
-36
lines changed

12 files changed

+51
-36
lines changed

modules/indexer/issues/bleve/bleve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (b *Indexer) Delete(_ context.Context, ids ...int64) error {
138138

139139
// Search searches for issues by given conditions.
140140
// Returns the matching issue IDs
141-
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
141+
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
142142
var repoQueriesP []*query.NumericRangeQuery
143143
for _, repoID := range repoIDs {
144144
repoQueriesP = append(repoQueriesP, numericEqualityQuery(repoID, "repo_id"))

modules/indexer/issues/bleve/bleve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestBleveIndexAndSearch(t *testing.T) {
7777
}
7878

7979
for _, kw := range keywords {
80-
res, err := indexer.Search(context.TODO(), kw.Keyword, []int64{2}, 10, 0)
80+
res, err := indexer.Search(context.TODO(), kw.Keyword, []int64{2}, 10, 0, "")
8181
assert.NoError(t, err)
8282

8383
ids := make([]int64, 0, len(res.Hits))

modules/indexer/issues/db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (i *Indexer) Delete(_ context.Context, _ ...int64) error {
3636
}
3737

3838
// Search searches for issues
39-
func (i *Indexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
39+
func (i *Indexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
4040
total, ids, err := issues_model.SearchIssueIDsByKeyword(ctx, kw, repoIDs, limit, start)
4141
if err != nil {
4242
return nil, err

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (b *Indexer) Delete(ctx context.Context, ids ...int64) error {
140140

141141
// Search searches for issues by given conditions.
142142
// Returns the matching issue IDs
143-
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
143+
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
144144
kwQuery := elastic.NewMultiMatchQuery(keyword, "title", "content", "comments")
145145
query := elastic.NewBoolQuery()
146146
query = query.Must(kwQuery)

modules/indexer/issues/indexer.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,18 @@ func UpdateIssueIndexer(issue *issues_model.Issue) {
242242
comments = append(comments, comment.Content)
243243
}
244244
}
245+
issueType := "issue"
246+
if issue.IsPull {
247+
issueType = "pull"
248+
}
245249
indexerData := &internal.IndexerData{
246-
ID: issue.ID,
247-
RepoID: issue.RepoID,
248-
Title: issue.Title,
249-
Content: issue.Content,
250-
Comments: comments,
250+
ID: issue.ID,
251+
RepoID: issue.RepoID,
252+
State: string(issue.State()),
253+
IssueType: issueType,
254+
Title: issue.Title,
255+
Content: issue.Content,
256+
Comments: comments,
251257
}
252258
log.Debug("Adding to channel: %v", indexerData)
253259
if err := issueIndexerQueue.Push(indexerData); err != nil {
@@ -278,10 +284,10 @@ func DeleteRepoIssueIndexer(ctx context.Context, repo *repo_model.Repository) {
278284

279285
// SearchIssuesByKeyword search issue ids by keywords and repo id
280286
// WARNNING: You have to ensure user have permission to visit repoIDs' issues
281-
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword string) ([]int64, error) {
287+
func SearchIssuesByKeyword(ctx context.Context, repoIDs []int64, keyword, state string) ([]int64, error) {
282288
var issueIDs []int64
283289
indexer := *globalIndexer.Load()
284-
res, err := indexer.Search(ctx, keyword, repoIDs, 50, 0)
290+
res, err := indexer.Search(ctx, keyword, repoIDs, 50, 0, state)
285291
if err != nil {
286292
return nil, err
287293
}

modules/indexer/issues/indexer_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func TestBleveSearchIssues(t *testing.T) {
5050

5151
time.Sleep(5 * time.Second)
5252

53-
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
53+
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", "")
5454
assert.NoError(t, err)
5555
assert.EqualValues(t, []int64{2}, ids)
5656

57-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
57+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", "")
5858
assert.NoError(t, err)
5959
assert.EqualValues(t, []int64{1}, ids)
6060

61-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
61+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", "")
6262
assert.NoError(t, err)
6363
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
6464

65-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
65+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", "")
6666
assert.NoError(t, err)
6767
assert.EqualValues(t, []int64{1}, ids)
6868
}
@@ -73,19 +73,19 @@ func TestDBSearchIssues(t *testing.T) {
7373
setting.Indexer.IssueType = "db"
7474
InitIssueIndexer(true)
7575

76-
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2")
76+
ids, err := SearchIssuesByKeyword(context.TODO(), []int64{1}, "issue2", "")
7777
assert.NoError(t, err)
7878
assert.EqualValues(t, []int64{2}, ids)
7979

80-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first")
80+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "first", "")
8181
assert.NoError(t, err)
8282
assert.EqualValues(t, []int64{1}, ids)
8383

84-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for")
84+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "for", "")
8585
assert.NoError(t, err)
8686
assert.ElementsMatch(t, []int64{1, 2, 3, 5, 11}, ids)
8787

88-
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good")
88+
ids, err = SearchIssuesByKeyword(context.TODO(), []int64{1}, "good", "")
8989
assert.NoError(t, err)
9090
assert.EqualValues(t, []int64{1}, ids)
9191
}

modules/indexer/issues/internal/indexer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Indexer interface {
1515
internal.Indexer
1616
Index(ctx context.Context, issue []*IndexerData) error
1717
Delete(ctx context.Context, ids ...int64) error
18-
Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*SearchResult, error)
18+
Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*SearchResult, error)
1919
}
2020

2121
// NewDummyIndexer returns a dummy indexer
@@ -37,6 +37,6 @@ func (d *dummyIndexer) Delete(ctx context.Context, ids ...int64) error {
3737
return fmt.Errorf("indexer is not ready")
3838
}
3939

40-
func (d *dummyIndexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int) (*SearchResult, error) {
40+
func (d *dummyIndexer) Search(ctx context.Context, kw string, repoIDs []int64, limit, start int, state string) (*SearchResult, error) {
4141
return nil, fmt.Errorf("indexer is not ready")
4242
}

modules/indexer/issues/internal/model.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package internal
55

66
// IndexerData data stored in the issue indexer
77
type IndexerData struct {
8-
ID int64 `json:"id"`
9-
RepoID int64 `json:"repo_id"`
10-
Title string `json:"title"`
11-
Content string `json:"content"`
12-
Comments []string `json:"comments"`
13-
IsDelete bool `json:"is_delete"`
14-
IDs []int64 `json:"ids"`
8+
ID int64 `json:"id"`
9+
RepoID int64 `json:"repo_id"`
10+
State string `json:"state"` // open, closed, all
11+
IssueType string `json:"type"` // issue or pull
12+
Title string `json:"title"`
13+
Content string `json:"content"`
14+
Comments []string `json:"comments"`
15+
IsDelete bool `json:"is_delete"`
16+
IDs []int64 `json:"ids"`
1517
}
1618

1719
// Match represents on search result

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
const (
19-
issueIndexerLatestVersion = 0
19+
issueIndexerLatestVersion = 1
2020
)
2121

2222
var _ internal.Indexer = &Indexer{}
@@ -70,12 +70,19 @@ func (b *Indexer) Delete(_ context.Context, ids ...int64) error {
7070

7171
// Search searches for issues by given conditions.
7272
// Returns the matching issue IDs
73-
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int) (*internal.SearchResult, error) {
73+
func (b *Indexer) Search(ctx context.Context, keyword string, repoIDs []int64, limit, start int, state string) (*internal.SearchResult, error) {
7474
repoFilters := make([]string, 0, len(repoIDs))
7575
for _, repoID := range repoIDs {
7676
repoFilters = append(repoFilters, "repo_id = "+strconv.FormatInt(repoID, 10))
7777
}
7878
filter := strings.Join(repoFilters, " OR ")
79+
if state == "open" || state == "closed" {
80+
if filter != "" {
81+
filter = "(" + filter + ") AND state = " + state
82+
} else {
83+
filter = "state = " + state
84+
}
85+
}
7986
searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(keyword, &meilisearch.SearchRequest{
8087
Filter: filter,
8188
Limit: int64(limit),

routers/api/v1/repo/issue.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func SearchIssues(ctx *context.APIContext) {
195195
}
196196
var issueIDs []int64
197197
if len(keyword) > 0 && len(repoIDs) > 0 {
198-
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword); err != nil {
198+
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword, ctx.FormString("state")); err != nil {
199199
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
200200
return
201201
}
@@ -394,7 +394,7 @@ func ListIssues(ctx *context.APIContext) {
394394
var issueIDs []int64
395395
var labelIDs []int64
396396
if len(keyword) > 0 {
397-
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword)
397+
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword, ctx.FormString("state"))
398398
if err != nil {
399399
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err)
400400
return

routers/web/repo/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
189189

190190
var issueIDs []int64
191191
if len(keyword) > 0 {
192-
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{repo.ID}, keyword)
192+
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{repo.ID}, keyword, ctx.FormString("state"))
193193
if err != nil {
194194
if issue_indexer.IsAvailable(ctx) {
195195
ctx.ServerError("issueIndexer.Search", err)
@@ -2466,7 +2466,7 @@ func SearchIssues(ctx *context.Context) {
24662466
}
24672467
var issueIDs []int64
24682468
if len(keyword) > 0 && len(repoIDs) > 0 {
2469-
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword); err != nil {
2469+
if issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, repoIDs, keyword, ctx.FormString("state")); err != nil {
24702470
ctx.Error(http.StatusInternalServerError, "SearchIssuesByKeyword", err.Error())
24712471
return
24722472
}
@@ -2614,7 +2614,7 @@ func ListIssues(ctx *context.Context) {
26142614
var issueIDs []int64
26152615
var labelIDs []int64
26162616
if len(keyword) > 0 {
2617-
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword)
2617+
issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx, []int64{ctx.Repo.Repository.ID}, keyword, ctx.FormString("state"))
26182618
if err != nil {
26192619
ctx.Error(http.StatusInternalServerError, err.Error())
26202620
return

routers/web/user/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ func issueIDsFromSearch(ctx *context.Context, ctxUser *user_model.User, keyword
725725
if err != nil {
726726
return nil, fmt.Errorf("GetRepoIDsForIssuesOptions: %w", err)
727727
}
728-
issueIDsFromSearch, err := issue_indexer.SearchIssuesByKeyword(ctx, searchRepoIDs, keyword)
728+
issueIDsFromSearch, err := issue_indexer.SearchIssuesByKeyword(ctx, searchRepoIDs, keyword, ctx.FormString("state"))
729729
if err != nil {
730730
return nil, fmt.Errorf("SearchIssuesByKeyword: %w", err)
731731
}

0 commit comments

Comments
 (0)