···
1
+
// heavily inspired by gitea's model (basically copy-pasted)
2
+
package issues_indexer
9
+
"github.com/blevesearch/bleve/v2"
10
+
"github.com/blevesearch/bleve/v2/index/upsidedown"
11
+
"github.com/blevesearch/bleve/v2/search/query"
12
+
"tangled.org/core/appview/db"
13
+
"tangled.org/core/appview/indexer/base36"
14
+
"tangled.org/core/appview/indexer/bleve"
15
+
"tangled.org/core/appview/models"
16
+
"tangled.org/core/appview/pagination"
17
+
tlog "tangled.org/core/log"
20
+
type Indexer struct {
25
+
func NewIndexer(indexDir string) *Indexer {
31
+
// Init initializes the indexer
32
+
func (ix *Indexer) Init(ctx context.Context, e db.Execer) {
33
+
l := tlog.FromContext(ctx)
34
+
existed, err := ix.intialize(ctx)
36
+
l.Error("failed to initialize issue indexer", "err", err)
39
+
l.Debug("Populating the issue indexer")
40
+
err := PopulateIndexer(ctx, ix, e)
42
+
l.Error("failed to populate issue indexer", "err", err)
45
+
l.Info("Initialized the issue indexer")
48
+
func (ix *Indexer) intialize(ctx context.Context) (bool, error) {
49
+
if ix.indexer != nil {
50
+
return false, errors.New("indexer is already initialized")
53
+
indexer, err := openIndexer(ctx, ix.path)
58
+
ix.indexer = indexer
62
+
mapping := bleve.NewIndexMapping()
63
+
indexer, err = bleve.New(ix.path, mapping)
68
+
ix.indexer = indexer
73
+
func openIndexer(ctx context.Context, path string) (bleve.Index, error) {
74
+
l := tlog.FromContext(ctx)
75
+
indexer, err := bleve.Open(path)
77
+
if errors.Is(err, upsidedown.IncompatibleVersion) {
78
+
l.Info("Indexer was built with a previous version of bleve, deleting and rebuilding")
79
+
return nil, os.RemoveAll(path)
86
+
func PopulateIndexer(ctx context.Context, ix *Indexer, e db.Execer) error {
87
+
l := tlog.FromContext(ctx)
89
+
err := pagination.IterateAll(
90
+
func(page pagination.Page) ([]models.Issue, error) {
91
+
return db.GetIssuesPaginated(e, page)
93
+
func(issues []models.Issue) error {
94
+
count += len(issues)
95
+
return ix.Index(ctx, issues...)
98
+
l.Info("issues indexed", "count", count)
102
+
// issueData data stored and will be indexed
103
+
type issueData struct {
104
+
ID int64 `json:"id"`
105
+
RepoAt string `json:"repo_at"`
106
+
IssueID int `json:"issue_id"`
107
+
Title string `json:"title"`
108
+
Body string `json:"body"`
110
+
IsOpen bool `json:"is_open"`
111
+
Comments []IssueCommentData `json:"comments"`
114
+
func makeIssueData(issue *models.Issue) *issueData {
117
+
RepoAt: issue.RepoAt.String(),
118
+
IssueID: issue.IssueId,
119
+
Title: issue.Title,
121
+
IsOpen: issue.Open,
125
+
type IssueCommentData struct {
126
+
Body string `json:"body"`
129
+
type SearchResult struct {
134
+
const maxBatchSize = 20
136
+
func (ix *Indexer) Index(ctx context.Context, issues ...models.Issue) error {
137
+
batch := bleveutil.NewFlushingBatch(ix.indexer, maxBatchSize)
138
+
for _, issue := range issues {
139
+
issueData := makeIssueData(&issue)
140
+
if err := batch.Index(base36.Encode(issue.Id), issueData); err != nil {
144
+
return batch.Flush()
147
+
// Search searches for issues
148
+
func (ix *Indexer) Search(ctx context.Context, opts models.IssueSearchOptions) (*SearchResult, error) {
149
+
var queries []query.Query
151
+
if opts.Keyword != "" {
152
+
queries = append(queries, bleve.NewDisjunctionQuery(
153
+
matchAndQuery(opts.Keyword, "title"),
154
+
matchAndQuery(opts.Keyword, "body"),
157
+
queries = append(queries, keywordFieldQuery(opts.RepoAt, "repo_at"))
158
+
queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open"))
159
+
// TODO: append more queries
161
+
var indexerQuery query.Query = bleve.NewConjunctionQuery(queries...)
162
+
searchReq := bleve.NewSearchRequestOptions(indexerQuery, opts.Page.Limit, opts.Page.Offset, false)
163
+
res, err := ix.indexer.SearchInContext(ctx, searchReq)
167
+
ret := &SearchResult{
169
+
Hits: make([]int64, len(res.Hits)),
171
+
for i, hit := range res.Hits {
172
+
id, err := base36.Decode(hit.ID)
181
+
func matchAndQuery(keyword, field string) query.Query {
182
+
q := bleve.NewMatchQuery(keyword)
187
+
func boolFieldQuery(val bool, field string) query.Query {
188
+
q := bleve.NewBoolFieldQuery(val)
193
+
func keywordFieldQuery(keyword, field string) query.Query {
194
+
q := bleve.NewTermQuery(keyword)