2017-01-25 03:43:02 +01:00
|
|
|
// Copyright 2016 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 (
|
2017-09-16 22:16:21 +02:00
|
|
|
"fmt"
|
2018-02-05 19:29:17 +01:00
|
|
|
"os"
|
2017-09-16 22:16:21 +02:00
|
|
|
"strconv"
|
|
|
|
|
2018-02-05 19:29:17 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2017-09-16 22:16:21 +02:00
|
|
|
"github.com/blevesearch/bleve"
|
2017-09-25 02:08:48 +02:00
|
|
|
"github.com/blevesearch/bleve/analysis/token/unicodenorm"
|
2018-02-05 19:29:17 +01:00
|
|
|
"github.com/blevesearch/bleve/index/upsidedown"
|
2017-09-25 02:08:48 +02:00
|
|
|
"github.com/blevesearch/bleve/mapping"
|
2017-09-16 22:16:21 +02:00
|
|
|
"github.com/blevesearch/bleve/search/query"
|
2018-02-05 19:29:17 +01:00
|
|
|
"github.com/ethantkoenig/rupture"
|
2017-01-25 03:43:02 +01:00
|
|
|
)
|
|
|
|
|
2017-09-16 22:16:21 +02:00
|
|
|
// indexerID a bleve-compatible unique identifier for an integer id
|
|
|
|
func indexerID(id int64) string {
|
|
|
|
return strconv.FormatInt(id, 36)
|
|
|
|
}
|
|
|
|
|
|
|
|
// idOfIndexerID the integer id associated with an indexer id
|
|
|
|
func idOfIndexerID(indexerID string) (int64, error) {
|
|
|
|
id, err := strconv.ParseInt(indexerID, 36, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Unexpected indexer ID %s: %v", indexerID, err)
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// numericEqualityQuery a numeric equality query for the given value and field
|
|
|
|
func numericEqualityQuery(value int64, field string) *query.NumericRangeQuery {
|
|
|
|
f := float64(value)
|
|
|
|
tru := true
|
|
|
|
q := bleve.NewNumericRangeInclusiveQuery(&f, &f, &tru, &tru)
|
|
|
|
q.SetField(field)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMatchPhraseQuery(matchPhrase, field, analyzer string) *query.MatchPhraseQuery {
|
|
|
|
q := bleve.NewMatchPhraseQuery(matchPhrase)
|
|
|
|
q.FieldVal = field
|
|
|
|
q.Analyzer = analyzer
|
|
|
|
return q
|
2017-01-25 03:43:02 +01:00
|
|
|
}
|
2017-09-25 02:08:48 +02:00
|
|
|
|
|
|
|
const unicodeNormalizeName = "unicodeNormalize"
|
|
|
|
|
|
|
|
func addUnicodeNormalizeTokenFilter(m *mapping.IndexMappingImpl) error {
|
|
|
|
return m.AddCustomTokenFilter(unicodeNormalizeName, map[string]interface{}{
|
|
|
|
"type": unicodenorm.Name,
|
|
|
|
"form": unicodenorm.NFC,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxBatchSize = 16
|
|
|
|
|
2018-02-05 19:29:17 +01:00
|
|
|
// openIndexer open the index at the specified path, checking for metadata
|
|
|
|
// updates and bleve version updates. If index needs to be created (or
|
|
|
|
// re-created), returns (nil, nil)
|
|
|
|
func openIndexer(path string, latestVersion int) (bleve.Index, error) {
|
|
|
|
_, err := os.Stat(setting.Indexer.IssuePath)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2017-09-25 02:08:48 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 19:29:17 +01:00
|
|
|
metadata, err := rupture.ReadIndexMetadata(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if metadata.Version < latestVersion {
|
|
|
|
// the indexer is using a previous version, so we should delete it and
|
|
|
|
// re-populate
|
|
|
|
return nil, os.RemoveAll(path)
|
2017-09-25 02:08:48 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 19:29:17 +01:00
|
|
|
index, err := bleve.Open(path)
|
|
|
|
if err != nil && err == upsidedown.IncompatibleVersion {
|
|
|
|
// the indexer was built with a previous version of bleve, so we should
|
|
|
|
// delete it and re-populate
|
|
|
|
return nil, os.RemoveAll(path)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2017-09-25 02:08:48 +02:00
|
|
|
}
|
2018-02-05 19:29:17 +01:00
|
|
|
return index, nil
|
2017-09-25 02:08:48 +02:00
|
|
|
}
|