2017-09-16 14:16:21 -06:00
|
|
|
// 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 (
|
|
|
|
"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"
|
2018-02-05 11:29:17 -07:00
|
|
|
"github.com/ethantkoenig/rupture"
|
2017-09-16 14:16:21 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// issueIndexer (thread-safe) index for searching issues
|
|
|
|
var issueIndexer bleve.Index
|
|
|
|
|
2018-02-05 11:29:17 -07:00
|
|
|
const (
|
|
|
|
issueIndexerAnalyzer = "issueIndexer"
|
|
|
|
issueIndexerDocType = "issueIndexerDocType"
|
|
|
|
|
|
|
|
issueIndexerLatestVersion = 1
|
|
|
|
)
|
|
|
|
|
2017-09-16 14:16:21 -06:00
|
|
|
// IssueIndexerData data stored in the issue indexer
|
|
|
|
type IssueIndexerData struct {
|
|
|
|
RepoID int64
|
|
|
|
Title string
|
|
|
|
Content string
|
|
|
|
Comments []string
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:29:17 -07:00
|
|
|
// Type returns the document type, for bleve's mapping.Classifier interface.
|
|
|
|
func (i *IssueIndexerData) Type() string {
|
|
|
|
return issueIndexerDocType
|
|
|
|
}
|
|
|
|
|
2017-09-16 14:16:21 -06:00
|
|
|
// IssueIndexerUpdate an update to the issue indexer
|
|
|
|
type IssueIndexerUpdate struct {
|
|
|
|
IssueID int64
|
|
|
|
Data *IssueIndexerData
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:29:17 -07:00
|
|
|
// AddToFlushingBatch adds the update to the given flushing batch.
|
|
|
|
func (i IssueIndexerUpdate) AddToFlushingBatch(batch rupture.FlushingBatch) error {
|
|
|
|
return batch.Index(indexerID(i.IssueID), i.Data)
|
2017-09-24 18:08:48 -06:00
|
|
|
}
|
|
|
|
|
2017-09-16 14:16:21 -06:00
|
|
|
// InitIssueIndexer initialize issue indexer
|
|
|
|
func InitIssueIndexer(populateIndexer func() error) {
|
2018-02-05 11:29:17 -07:00
|
|
|
var err error
|
|
|
|
issueIndexer, err = openIndexer(setting.Indexer.IssuePath, issueIndexerLatestVersion)
|
|
|
|
if err != nil {
|
2017-09-22 15:30:56 -06:00
|
|
|
log.Fatal(4, "InitIssueIndexer: %v", err)
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
2018-02-05 11:29:17 -07:00
|
|
|
if issueIndexer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:37:48 -07:00
|
|
|
if err = createIssueIndexer(setting.Indexer.IssuePath, issueIndexerLatestVersion); err != nil {
|
2017-09-22 15:30:56 -06:00
|
|
|
log.Fatal(4, "InitIssuesIndexer: create index, %v", err)
|
|
|
|
}
|
|
|
|
if err = populateIndexer(); err != nil {
|
|
|
|
log.Fatal(4, "InitIssueIndexer: populate index, %v", err)
|
|
|
|
}
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// createIssueIndexer create an issue indexer if one does not already exist
|
2018-12-19 01:37:48 -07:00
|
|
|
func createIssueIndexer(path string, latestVersion int) error {
|
2017-09-16 14:16:21 -06:00
|
|
|
mapping := bleve.NewIndexMapping()
|
|
|
|
docMapping := bleve.NewDocumentMapping()
|
|
|
|
|
2018-02-05 11:29:17 -07:00
|
|
|
numericFieldMapping := bleve.NewNumericFieldMapping()
|
|
|
|
numericFieldMapping.IncludeInAll = false
|
|
|
|
docMapping.AddFieldMappingsAt("RepoID", numericFieldMapping)
|
2017-09-16 14:16:21 -06:00
|
|
|
|
|
|
|
textFieldMapping := bleve.NewTextFieldMapping()
|
2018-02-05 11:29:17 -07:00
|
|
|
textFieldMapping.Store = false
|
|
|
|
textFieldMapping.IncludeInAll = false
|
2017-09-16 14:16:21 -06:00
|
|
|
docMapping.AddFieldMappingsAt("Title", textFieldMapping)
|
|
|
|
docMapping.AddFieldMappingsAt("Content", textFieldMapping)
|
|
|
|
docMapping.AddFieldMappingsAt("Comments", textFieldMapping)
|
|
|
|
|
2017-09-24 18:08:48 -06:00
|
|
|
if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
|
2017-09-16 14:16:21 -06:00
|
|
|
return err
|
|
|
|
} else if err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]interface{}{
|
|
|
|
"type": custom.Name,
|
|
|
|
"char_filters": []string{},
|
|
|
|
"tokenizer": unicode.Name,
|
2017-09-24 18:08:48 -06:00
|
|
|
"token_filters": []string{unicodeNormalizeName, lowercase.Name},
|
2017-09-16 14:16:21 -06:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapping.DefaultAnalyzer = issueIndexerAnalyzer
|
2018-02-05 11:29:17 -07:00
|
|
|
mapping.AddDocumentMapping(issueIndexerDocType, docMapping)
|
|
|
|
mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping())
|
2017-09-16 14:16:21 -06:00
|
|
|
|
|
|
|
var err error
|
2018-12-19 01:37:48 -07:00
|
|
|
issueIndexer, err = bleve.New(path, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rupture.WriteIndexMetadata(path, &rupture.IndexMetadata{
|
|
|
|
Version: latestVersion,
|
|
|
|
})
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
|
|
|
|
2017-09-24 18:08:48 -06:00
|
|
|
// IssueIndexerBatch batch to add updates to
|
2018-02-05 11:29:17 -07:00
|
|
|
func IssueIndexerBatch() rupture.FlushingBatch {
|
|
|
|
return rupture.NewFlushingBatch(issueIndexer, maxBatchSize)
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|