You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

model.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package internal
  4. import (
  5. "code.gitea.io/gitea/models/db"
  6. "code.gitea.io/gitea/modules/optional"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. )
  9. // IndexerData data stored in the issue indexer
  10. type IndexerData struct {
  11. ID int64 `json:"id"`
  12. RepoID int64 `json:"repo_id"`
  13. IsPublic bool `json:"is_public"` // If the repo is public
  14. // Fields used for keyword searching
  15. Title string `json:"title"`
  16. Content string `json:"content"`
  17. Comments []string `json:"comments"`
  18. // Fields used for filtering
  19. IsPull bool `json:"is_pull"`
  20. IsClosed bool `json:"is_closed"`
  21. LabelIDs []int64 `json:"label_ids"`
  22. NoLabel bool `json:"no_label"` // True if LabelIDs is empty
  23. MilestoneID int64 `json:"milestone_id"`
  24. ProjectID int64 `json:"project_id"`
  25. ProjectBoardID int64 `json:"project_board_id"`
  26. PosterID int64 `json:"poster_id"`
  27. AssigneeID int64 `json:"assignee_id"`
  28. MentionIDs []int64 `json:"mention_ids"`
  29. ReviewedIDs []int64 `json:"reviewed_ids"`
  30. ReviewRequestedIDs []int64 `json:"review_requested_ids"`
  31. SubscriberIDs []int64 `json:"subscriber_ids"`
  32. UpdatedUnix timeutil.TimeStamp `json:"updated_unix"`
  33. // Fields used for sorting
  34. // UpdatedUnix is both used for filtering and sorting.
  35. // ID is used for sorting too, to make the sorting stable.
  36. CreatedUnix timeutil.TimeStamp `json:"created_unix"`
  37. DeadlineUnix timeutil.TimeStamp `json:"deadline_unix"`
  38. CommentCount int64 `json:"comment_count"`
  39. }
  40. // Match represents on search result
  41. type Match struct {
  42. ID int64 `json:"id"`
  43. Score float64 `json:"score"`
  44. }
  45. // SearchResult represents search results
  46. type SearchResult struct {
  47. Total int64
  48. Hits []Match
  49. }
  50. // SearchOptions represents search options.
  51. //
  52. // It has a slightly different design from database query options.
  53. // In database query options, a field is never a pointer, so it could be confusing when it's zero value:
  54. // Do you want to find data with a field value of 0, or do you not specify the field in the options?
  55. // To avoid this confusion, db introduced db.NoConditionID(-1).
  56. // So zero value means the field is not specified in the search options, and db.NoConditionID means "== 0" or "id NOT IN (SELECT id FROM ...)"
  57. // It's still not ideal, it trapped developers many times.
  58. // And sometimes -1 could be a valid value, like issue ID, negative numbers indicate exclusion.
  59. // Since db.NoConditionID is for "db" (the package name is db), it makes sense not to use it in the indexer:
  60. // Why do bleve/elasticsearch/meilisearch indexers need to know about db.NoConditionID?
  61. // So in SearchOptions, we use pointer for fields which could be not specified,
  62. // and always use the value to filter if it's not nil, even if it's zero or negative.
  63. // It can handle almost all cases, if there is an exception, we can add a new field, like NoLabelOnly.
  64. // Unfortunately, we still use db for the indexer and have to convert between db.NoConditionID and nil for legacy reasons.
  65. type SearchOptions struct {
  66. Keyword string // keyword to search
  67. IsFuzzyKeyword bool // if false the levenshtein distance is 0
  68. RepoIDs []int64 // repository IDs which the issues belong to
  69. AllPublic bool // if include all public repositories
  70. IsPull optional.Option[bool] // if the issues is a pull request
  71. IsClosed optional.Option[bool] // if the issues is closed
  72. IncludedLabelIDs []int64 // labels the issues have
  73. ExcludedLabelIDs []int64 // labels the issues don't have
  74. IncludedAnyLabelIDs []int64 // labels the issues have at least one. It will be ignored if IncludedLabelIDs is not empty. It's an uncommon filter, but it has been supported accidentally by issues.IssuesOptions.IncludedLabelNames.
  75. NoLabelOnly bool // if the issues have no label, if true, IncludedLabelIDs and ExcludedLabelIDs, IncludedAnyLabelIDs will be ignored
  76. MilestoneIDs []int64 // milestones the issues have
  77. ProjectID optional.Option[int64] // project the issues belong to
  78. ProjectBoardID optional.Option[int64] // project board the issues belong to
  79. PosterID optional.Option[int64] // poster of the issues
  80. AssigneeID optional.Option[int64] // assignee of the issues, zero means no assignee
  81. MentionID optional.Option[int64] // mentioned user of the issues
  82. ReviewedID optional.Option[int64] // reviewer of the issues
  83. ReviewRequestedID optional.Option[int64] // requested reviewer of the issues
  84. SubscriberID optional.Option[int64] // subscriber of the issues
  85. UpdatedAfterUnix optional.Option[int64]
  86. UpdatedBeforeUnix optional.Option[int64]
  87. db.Paginator
  88. SortBy SortBy // sort by field
  89. }
  90. // Copy returns a copy of the options.
  91. // Be careful, it's not a deep copy, so `SearchOptions.RepoIDs = {...}` is OK while `SearchOptions.RepoIDs[0] = ...` is not.
  92. func (o *SearchOptions) Copy(edit ...func(options *SearchOptions)) *SearchOptions {
  93. if o == nil {
  94. return nil
  95. }
  96. v := *o
  97. for _, e := range edit {
  98. e(&v)
  99. }
  100. return &v
  101. }
  102. type SortBy string
  103. const (
  104. SortByCreatedDesc SortBy = "-created_unix"
  105. SortByUpdatedDesc SortBy = "-updated_unix"
  106. SortByCommentsDesc SortBy = "-comment_count"
  107. SortByDeadlineDesc SortBy = "-deadline_unix"
  108. SortByCreatedAsc SortBy = "created_unix"
  109. SortByUpdatedAsc SortBy = "updated_unix"
  110. SortByCommentsAsc SortBy = "comment_count"
  111. SortByDeadlineAsc SortBy = "deadline_unix"
  112. // Unsupported sort types which are supported by issues.IssuesOptions.SortType:
  113. //
  114. // - "priorityrepo":
  115. // It's impossible to support it in the indexer.
  116. // It is based on the specified repository in the request, so we cannot add static field to the indexer.
  117. // If we do something like that query the issues in the specified repository first then append other issues,
  118. // it will break the pagination.
  119. //
  120. // - "project-column-sorting":
  121. // Although it's possible to support it by adding project.ProjectIssue.Sorting to the indexer,
  122. // but what if the issue belongs to multiple projects?
  123. // Since it's unsupported to search issues with keyword in project page, we don't need to support it.
  124. )