summaryrefslogtreecommitdiffstats
path: root/modules/indexer/issue.go
blob: 62a18e2b3baad8c3091beb7747a9e575784371d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package indexer

import (
	"os"

	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/setting"

	"github.com/blevesearch/bleve"
	"github.com/blevesearch/bleve/analysis/analyzer/custom"
	"github.com/blevesearch/bleve/analysis/token/lowercase"
	"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
	"github.com/blevesearch/bleve/index/upsidedown"
)

// issueIndexer (thread-safe) index for searching issues
var issueIndexer bleve.Index

// IssueIndexerData data stored in the issue indexer
type IssueIndexerData struct {
	RepoID   int64
	Title    string
	Content  string
	Comments []string
}

// IssueIndexerUpdate an update to the issue indexer
type IssueIndexerUpdate struct {
	IssueID int64
	Data    *IssueIndexerData
}

func (update IssueIndexerUpdate) addToBatch(batch *bleve.Batch) error {
	return batch.Index(indexerID(update.IssueID), update.Data)
}

const issueIndexerAnalyzer = "issueIndexer"

// InitIssueIndexer initialize issue indexer
func InitIssueIndexer(populateIndexer func() error) {
	_, err := os.Stat(setting.Indexer.IssuePath)
	if err != nil && !os.IsNotExist(err) {
		log.Fatal(4, "InitIssueIndexer: %v", err)
	} else if err == nil {
		issueIndexer, err = bleve.Open(setting.Indexer.IssuePath)
		if err == nil {
			return
		} else if err != upsidedown.IncompatibleVersion {
			log.Fatal(4, "InitIssueIndexer, open index: %v", err)
		}
		log.Warn("Incompatible bleve version, deleting and recreating issue indexer")
		if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil {
			log.Fatal(4, "InitIssueIndexer: remove index, %v", err)
		}
	}
	if err = createIssueIndexer(); err != nil {
		log.Fatal(4, "InitIssuesIndexer: create index, %v", err)
	}
	if err = populateIndexer(); err != nil {
		log.Fatal(4, "InitIssueIndexer: populate index, %v", err)
	}
}

// createIssueIndexer create an issue indexer if one does not already exist
func createIssueIndexer() error {
	mapping := bleve.NewIndexMapping()
	docMapping := bleve.NewDocumentMapping()

	docMapping.AddFieldMappingsAt("RepoID", bleve.NewNumericFieldMapping())

	textFieldMapping := bleve.NewTextFieldMapping()
	docMapping.AddFieldMappingsAt("Title", textFieldMapping)
	docMapping.AddFieldMappingsAt("Content", textFieldMapping)
	docMapping.AddFieldMappingsAt("Comments", textFieldMapping)

	if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
		return err
	} else if err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]interface{}{
		"type":          custom.Name,
		"char_filters":  []string{},
		"tokenizer":     unicode.Name,
		"token_filters": []string{unicodeNormalizeName, lowercase.Name},
	}); err != nil {
		return err
	}

	mapping.DefaultAnalyzer = issueIndexerAnalyzer
	mapping.AddDocumentMapping("issues", docMapping)

	var err error
	issueIndexer, err = bleve.New(setting.Indexer.IssuePath, mapping)
	return err
}

// IssueIndexerBatch batch to add updates to
func IssueIndexerBatch() *Batch {
	return &Batch{
		batch: issueIndexer.NewBatch(),
		index: issueIndexer,
	}
}

// SearchIssuesByKeyword searches for issues by given conditions.
// Returns the matching issue IDs
func SearchIssuesByKeyword(repoID int64, keyword string) ([]int64, error) {
	indexerQuery := bleve.NewConjunctionQuery(
		numericEqualityQuery(repoID, "RepoID"),
		bleve.NewDisjunctionQuery(
			newMatchPhraseQuery(keyword, "Title", issueIndexerAnalyzer),
			newMatchPhraseQuery(keyword, "Content", issueIndexerAnalyzer),
			newMatchPhraseQuery(keyword, "Comments", issueIndexerAnalyzer),
		))
	search := bleve.NewSearchRequestOptions(indexerQuery, 2147483647, 0, false)

	result, err := issueIndexer.Search(search)
	if err != nil {
		return nil, err
	}

	issueIDs := make([]int64, len(result.Hits))
	for i, hit := range result.Hits {
		issueIDs[i], err = idOfIndexerID(hit.ID)
		if err != nil {
			return nil, err
		}
	}
	return issueIDs, nil
}