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.

93 lines
2.6 KiB

  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. "fmt"
  7. "os"
  8. "strconv"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/blevesearch/bleve"
  11. "github.com/blevesearch/bleve/analysis/token/unicodenorm"
  12. "github.com/blevesearch/bleve/index/upsidedown"
  13. "github.com/blevesearch/bleve/mapping"
  14. "github.com/blevesearch/bleve/search/query"
  15. "github.com/ethantkoenig/rupture"
  16. )
  17. // indexerID a bleve-compatible unique identifier for an integer id
  18. func indexerID(id int64) string {
  19. return strconv.FormatInt(id, 36)
  20. }
  21. // idOfIndexerID the integer id associated with an indexer id
  22. func idOfIndexerID(indexerID string) (int64, error) {
  23. id, err := strconv.ParseInt(indexerID, 36, 64)
  24. if err != nil {
  25. return 0, fmt.Errorf("Unexpected indexer ID %s: %v", indexerID, err)
  26. }
  27. return id, nil
  28. }
  29. // numericEqualityQuery a numeric equality query for the given value and field
  30. func numericEqualityQuery(value int64, field string) *query.NumericRangeQuery {
  31. f := float64(value)
  32. tru := true
  33. q := bleve.NewNumericRangeInclusiveQuery(&f, &f, &tru, &tru)
  34. q.SetField(field)
  35. return q
  36. }
  37. func newMatchPhraseQuery(matchPhrase, field, analyzer string) *query.MatchPhraseQuery {
  38. q := bleve.NewMatchPhraseQuery(matchPhrase)
  39. q.FieldVal = field
  40. q.Analyzer = analyzer
  41. return q
  42. }
  43. const unicodeNormalizeName = "unicodeNormalize"
  44. func addUnicodeNormalizeTokenFilter(m *mapping.IndexMappingImpl) error {
  45. return m.AddCustomTokenFilter(unicodeNormalizeName, map[string]interface{}{
  46. "type": unicodenorm.Name,
  47. "form": unicodenorm.NFC,
  48. })
  49. }
  50. const maxBatchSize = 16
  51. // openIndexer open the index at the specified path, checking for metadata
  52. // updates and bleve version updates. If index needs to be created (or
  53. // re-created), returns (nil, nil)
  54. func openIndexer(path string, latestVersion int) (bleve.Index, error) {
  55. _, err := os.Stat(setting.Indexer.IssuePath)
  56. if err != nil && os.IsNotExist(err) {
  57. return nil, nil
  58. } else if err != nil {
  59. return nil, err
  60. }
  61. metadata, err := rupture.ReadIndexMetadata(path)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if metadata.Version < latestVersion {
  66. // the indexer is using a previous version, so we should delete it and
  67. // re-populate
  68. return nil, os.RemoveAll(path)
  69. }
  70. index, err := bleve.Open(path)
  71. if err != nil && err == upsidedown.IncompatibleVersion {
  72. // the indexer was built with a previous version of bleve, so we should
  73. // delete it and re-populate
  74. return nil, os.RemoveAll(path)
  75. } else if err != nil {
  76. return nil, err
  77. }
  78. return index, nil
  79. }