···
1
+
// heavily inspired by gitea's model (basically copy-pasted)
···
"github.com/blevesearch/bleve/v2"
10
+
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
11
+
"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
12
+
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
13
+
"github.com/blevesearch/bleve/v2/analysis/token/unicodenorm"
14
+
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
"github.com/blevesearch/bleve/v2/index/upsidedown"
16
+
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/search/query"
"tangled.org/core/appview/db"
"tangled.org/core/appview/indexer/base36"
···
tlog "tangled.org/core/log"
27
+
issueIndexerAnalyzer = "issueIndexer"
28
+
issueIndexerDocType = "issueIndexerDocType"
30
+
unicodeNormalizeName = "uicodeNormalize"
···
l.Info("Initialized the issue indexer")
61
+
func generateIssueIndexMapping() (mapping.IndexMapping, error) {
62
+
mapping := bleve.NewIndexMapping()
63
+
docMapping := bleve.NewDocumentMapping()
65
+
textFieldMapping := bleve.NewTextFieldMapping()
66
+
textFieldMapping.Store = false
67
+
textFieldMapping.IncludeInAll = false
69
+
boolFieldMapping := bleve.NewBooleanFieldMapping()
70
+
boolFieldMapping.Store = false
71
+
boolFieldMapping.IncludeInAll = false
73
+
keywordFieldMapping := bleve.NewKeywordFieldMapping()
74
+
keywordFieldMapping.Store = false
75
+
keywordFieldMapping.IncludeInAll = false
77
+
// numericFieldMapping := bleve.NewNumericFieldMapping()
79
+
docMapping.AddFieldMappingsAt("title", textFieldMapping)
80
+
docMapping.AddFieldMappingsAt("body", textFieldMapping)
82
+
docMapping.AddFieldMappingsAt("repo_at", keywordFieldMapping)
83
+
docMapping.AddFieldMappingsAt("is_open", boolFieldMapping)
85
+
err := mapping.AddCustomTokenFilter(unicodeNormalizeName, map[string]any{
86
+
"type": unicodenorm.Name,
87
+
"form": unicodenorm.NFC,
93
+
err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]any{
94
+
"type": custom.Name,
95
+
"char_filters": []string{},
96
+
"tokenizer": unicode.Name,
97
+
"token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name},
103
+
mapping.DefaultAnalyzer = issueIndexerAnalyzer
104
+
mapping.AddDocumentMapping(issueIndexerDocType, docMapping)
105
+
mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping())
106
+
mapping.DefaultMapping = bleve.NewDocumentDisabledMapping()
108
+
return mapping, nil
func (ix *Indexer) intialize(ctx context.Context) (bool, error) {
return false, errors.New("indexer is already initialized")
···
61
-
mapping := bleve.NewIndexMapping()
125
+
mapping, err := generateIssueIndexMapping()
indexer, err = bleve.New(ix.path, mapping)
···
for _, issue := range issues {
dataList = append(dataList, &IssueData{
164
+
RepoAt: issue.RepoAt.String(),
···
// IssueData data stored and will be indexed
185
+
RepoAt string `json:"repo_at"`
IssueID int `json:"issue_id"`
Title string `json:"title"`
Body string `json:"body"`
···
Comments []IssueCommentData `json:"comments"`
194
+
// Type returns the document type, for bleve's mapping.Classifier interface.
195
+
func (i *IssueData) Type() string {
196
+
return issueIndexerDocType
type IssueCommentData struct {
Body string `json:"body"`
···
queries = append(queries, bleve.NewDisjunctionQuery(
152
-
matchAndQuery(opts.Keyword, "title"),
153
-
matchAndQuery(opts.Keyword, "body"),
227
+
matchAndQuery(opts.Keyword, "title", issueIndexerAnalyzer, 0),
228
+
matchAndQuery(opts.Keyword, "body", issueIndexerAnalyzer, 0),
231
+
queries = append(queries, keywordFieldQuery(opts.RepoAt, "repo_at"))
queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open"))
// TODO: append more queries
···
179
-
func matchAndQuery(keyword, field string) query.Query {
255
+
func matchAndQuery(keyword, field, analyzer string, fuzziness int) query.Query {
q := bleve.NewMatchQuery(keyword)
258
+
q.Analyzer = analyzer
259
+
q.Fuzziness = fuzziness
···
269
+
func keywordFieldQuery(keyword, field string) query.Query {
270
+
q := bleve.NewTermQuery(keyword)