55package indexer
66
77import (
8- "os"
9-
108 "code.gitea.io/gitea/modules/log"
119 "code.gitea.io/gitea/modules/setting"
1210
1311 "github.com/blevesearch/bleve"
1412 "github.com/blevesearch/bleve/analysis/analyzer/custom"
1513 "github.com/blevesearch/bleve/analysis/token/lowercase"
1614 "github.com/blevesearch/bleve/analysis/tokenizer/unicode"
17- "github.com/blevesearch/bleve/index/upsidedown "
15+ "github.com/ethantkoenig/rupture "
1816)
1917
2018// issueIndexer (thread-safe) index for searching issues
2119var issueIndexer bleve.Index
2220
21+ const (
22+ issueIndexerAnalyzer = "issueIndexer"
23+ issueIndexerDocType = "issueIndexerDocType"
24+
25+ issueIndexerLatestVersion = 1
26+ )
27+
2328// IssueIndexerData data stored in the issue indexer
2429type IssueIndexerData struct {
2530 RepoID int64
@@ -28,35 +33,33 @@ type IssueIndexerData struct {
2833 Comments []string
2934}
3035
36+ // Type returns the document type, for bleve's mapping.Classifier interface.
37+ func (i * IssueIndexerData ) Type () string {
38+ return issueIndexerDocType
39+ }
40+
3141// IssueIndexerUpdate an update to the issue indexer
3242type IssueIndexerUpdate struct {
3343 IssueID int64
3444 Data * IssueIndexerData
3545}
3646
37- func (update IssueIndexerUpdate ) addToBatch (batch * bleve.Batch ) error {
38- return batch .Index (indexerID (update .IssueID ), update .Data )
47+ // AddToFlushingBatch adds the update to the given flushing batch.
48+ func (i IssueIndexerUpdate ) AddToFlushingBatch (batch rupture.FlushingBatch ) error {
49+ return batch .Index (indexerID (i .IssueID ), i .Data )
3950}
4051
41- const issueIndexerAnalyzer = "issueIndexer"
42-
4352// InitIssueIndexer initialize issue indexer
4453func InitIssueIndexer (populateIndexer func () error ) {
45- _ , err := os .Stat (setting .Indexer .IssuePath )
46- if err != nil && ! os .IsNotExist (err ) {
54+ var err error
55+ issueIndexer , err = openIndexer (setting .Indexer .IssuePath , issueIndexerLatestVersion )
56+ if err != nil {
4757 log .Fatal (4 , "InitIssueIndexer: %v" , err )
48- } else if err == nil {
49- issueIndexer , err = bleve .Open (setting .Indexer .IssuePath )
50- if err == nil {
51- return
52- } else if err != upsidedown .IncompatibleVersion {
53- log .Fatal (4 , "InitIssueIndexer, open index: %v" , err )
54- }
55- log .Warn ("Incompatible bleve version, deleting and recreating issue indexer" )
56- if err = os .RemoveAll (setting .Indexer .IssuePath ); err != nil {
57- log .Fatal (4 , "InitIssueIndexer: remove index, %v" , err )
58- }
5958 }
59+ if issueIndexer != nil {
60+ return
61+ }
62+
6063 if err = createIssueIndexer (); err != nil {
6164 log .Fatal (4 , "InitIssuesIndexer: create index, %v" , err )
6265 }
@@ -70,9 +73,13 @@ func createIssueIndexer() error {
7073 mapping := bleve .NewIndexMapping ()
7174 docMapping := bleve .NewDocumentMapping ()
7275
73- docMapping .AddFieldMappingsAt ("RepoID" , bleve .NewNumericFieldMapping ())
76+ numericFieldMapping := bleve .NewNumericFieldMapping ()
77+ numericFieldMapping .IncludeInAll = false
78+ docMapping .AddFieldMappingsAt ("RepoID" , numericFieldMapping )
7479
7580 textFieldMapping := bleve .NewTextFieldMapping ()
81+ textFieldMapping .Store = false
82+ textFieldMapping .IncludeInAll = false
7683 docMapping .AddFieldMappingsAt ("Title" , textFieldMapping )
7784 docMapping .AddFieldMappingsAt ("Content" , textFieldMapping )
7885 docMapping .AddFieldMappingsAt ("Comments" , textFieldMapping )
@@ -89,19 +96,17 @@ func createIssueIndexer() error {
8996 }
9097
9198 mapping .DefaultAnalyzer = issueIndexerAnalyzer
92- mapping .AddDocumentMapping ("issues" , docMapping )
99+ mapping .AddDocumentMapping (issueIndexerDocType , docMapping )
100+ mapping .AddDocumentMapping ("_all" , bleve .NewDocumentDisabledMapping ())
93101
94102 var err error
95103 issueIndexer , err = bleve .New (setting .Indexer .IssuePath , mapping )
96104 return err
97105}
98106
99107// IssueIndexerBatch batch to add updates to
100- func IssueIndexerBatch () * Batch {
101- return & Batch {
102- batch : issueIndexer .NewBatch (),
103- index : issueIndexer ,
104- }
108+ func IssueIndexerBatch () rupture.FlushingBatch {
109+ return rupture .NewFlushingBatch (issueIndexer , maxBatchSize )
105110}
106111
107112// SearchIssuesByKeyword searches for issues by given conditions.
0 commit comments