You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexer.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package indexer
  5. import (
  6. "os"
  7. "strconv"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/blevesearch/bleve"
  10. "github.com/blevesearch/bleve/analysis/token/unicodenorm"
  11. "github.com/blevesearch/bleve/index/upsidedown"
  12. "github.com/blevesearch/bleve/mapping"
  13. "github.com/blevesearch/bleve/search/query"
  14. "github.com/ethantkoenig/rupture"
  15. )
  16. // indexerID a bleve-compatible unique identifier for an integer id
  17. func indexerID(id int64) string {
  18. return strconv.FormatInt(id, 36)
  19. }
  20. // numericEqualityQuery a numeric equality query for the given value and field
  21. func numericEqualityQuery(value int64, field string) *query.NumericRangeQuery {
  22. f := float64(value)
  23. tru := true
  24. q := bleve.NewNumericRangeInclusiveQuery(&f, &f, &tru, &tru)
  25. q.SetField(field)
  26. return q
  27. }
  28. const unicodeNormalizeName = "unicodeNormalize"
  29. func addUnicodeNormalizeTokenFilter(m *mapping.IndexMappingImpl) error {
  30. return m.AddCustomTokenFilter(unicodeNormalizeName, map[string]interface{}{
  31. "type": unicodenorm.Name,
  32. "form": unicodenorm.NFC,
  33. })
  34. }
  35. const maxBatchSize = 16
  36. // openIndexer open the index at the specified path, checking for metadata
  37. // updates and bleve version updates. If index needs to be created (or
  38. // re-created), returns (nil, nil)
  39. func openIndexer(path string, latestVersion int) (bleve.Index, error) {
  40. _, err := os.Stat(setting.Indexer.IssuePath)
  41. if err != nil && os.IsNotExist(err) {
  42. return nil, nil
  43. } else if err != nil {
  44. return nil, err
  45. }
  46. metadata, err := rupture.ReadIndexMetadata(path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if metadata.Version < latestVersion {
  51. // the indexer is using a previous version, so we should delete it and
  52. // re-populate
  53. return nil, os.RemoveAll(path)
  54. }
  55. index, err := bleve.Open(path)
  56. if err != nil && err == upsidedown.IncompatibleVersion {
  57. // the indexer was built with a previous version of bleve, so we should
  58. // delete it and re-populate
  59. return nil, os.RemoveAll(path)
  60. } else if err != nil {
  61. return nil, err
  62. }
  63. return index, nil
  64. }