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.

137 lines
4.1 KiB

  1. // Copyright 2017 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. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/blevesearch/bleve"
  9. "github.com/blevesearch/bleve/analysis/analyzer/custom"
  10. "github.com/blevesearch/bleve/analysis/token/lowercase"
  11. "github.com/blevesearch/bleve/analysis/tokenizer/unicode"
  12. "github.com/ethantkoenig/rupture"
  13. )
  14. // issueIndexer (thread-safe) index for searching issues
  15. var issueIndexer bleve.Index
  16. const (
  17. issueIndexerAnalyzer = "issueIndexer"
  18. issueIndexerDocType = "issueIndexerDocType"
  19. issueIndexerLatestVersion = 1
  20. )
  21. // IssueIndexerData data stored in the issue indexer
  22. type IssueIndexerData struct {
  23. RepoID int64
  24. Title string
  25. Content string
  26. Comments []string
  27. }
  28. // Type returns the document type, for bleve's mapping.Classifier interface.
  29. func (i *IssueIndexerData) Type() string {
  30. return issueIndexerDocType
  31. }
  32. // IssueIndexerUpdate an update to the issue indexer
  33. type IssueIndexerUpdate struct {
  34. IssueID int64
  35. Data *IssueIndexerData
  36. }
  37. // AddToFlushingBatch adds the update to the given flushing batch.
  38. func (i IssueIndexerUpdate) AddToFlushingBatch(batch rupture.FlushingBatch) error {
  39. return batch.Index(indexerID(i.IssueID), i.Data)
  40. }
  41. // InitIssueIndexer initialize issue indexer
  42. func InitIssueIndexer(populateIndexer func() error) {
  43. var err error
  44. issueIndexer, err = openIndexer(setting.Indexer.IssuePath, issueIndexerLatestVersion)
  45. if err != nil {
  46. log.Fatal(4, "InitIssueIndexer: %v", err)
  47. }
  48. if issueIndexer != nil {
  49. return
  50. }
  51. if err = createIssueIndexer(); err != nil {
  52. log.Fatal(4, "InitIssuesIndexer: create index, %v", err)
  53. }
  54. if err = populateIndexer(); err != nil {
  55. log.Fatal(4, "InitIssueIndexer: populate index, %v", err)
  56. }
  57. }
  58. // createIssueIndexer create an issue indexer if one does not already exist
  59. func createIssueIndexer() error {
  60. mapping := bleve.NewIndexMapping()
  61. docMapping := bleve.NewDocumentMapping()
  62. numericFieldMapping := bleve.NewNumericFieldMapping()
  63. numericFieldMapping.IncludeInAll = false
  64. docMapping.AddFieldMappingsAt("RepoID", numericFieldMapping)
  65. textFieldMapping := bleve.NewTextFieldMapping()
  66. textFieldMapping.Store = false
  67. textFieldMapping.IncludeInAll = false
  68. docMapping.AddFieldMappingsAt("Title", textFieldMapping)
  69. docMapping.AddFieldMappingsAt("Content", textFieldMapping)
  70. docMapping.AddFieldMappingsAt("Comments", textFieldMapping)
  71. if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
  72. return err
  73. } else if err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]interface{}{
  74. "type": custom.Name,
  75. "char_filters": []string{},
  76. "tokenizer": unicode.Name,
  77. "token_filters": []string{unicodeNormalizeName, lowercase.Name},
  78. }); err != nil {
  79. return err
  80. }
  81. mapping.DefaultAnalyzer = issueIndexerAnalyzer
  82. mapping.AddDocumentMapping(issueIndexerDocType, docMapping)
  83. mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping())
  84. var err error
  85. issueIndexer, err = bleve.New(setting.Indexer.IssuePath, mapping)
  86. return err
  87. }
  88. // IssueIndexerBatch batch to add updates to
  89. func IssueIndexerBatch() rupture.FlushingBatch {
  90. return rupture.NewFlushingBatch(issueIndexer, maxBatchSize)
  91. }
  92. // SearchIssuesByKeyword searches for issues by given conditions.
  93. // Returns the matching issue IDs
  94. func SearchIssuesByKeyword(repoID int64, keyword string) ([]int64, error) {
  95. indexerQuery := bleve.NewConjunctionQuery(
  96. numericEqualityQuery(repoID, "RepoID"),
  97. bleve.NewDisjunctionQuery(
  98. newMatchPhraseQuery(keyword, "Title", issueIndexerAnalyzer),
  99. newMatchPhraseQuery(keyword, "Content", issueIndexerAnalyzer),
  100. newMatchPhraseQuery(keyword, "Comments", issueIndexerAnalyzer),
  101. ))
  102. search := bleve.NewSearchRequestOptions(indexerQuery, 2147483647, 0, false)
  103. result, err := issueIndexer.Search(search)
  104. if err != nil {
  105. return nil, err
  106. }
  107. issueIDs := make([]int64, len(result.Hits))
  108. for i, hit := range result.Hits {
  109. issueIDs[i], err = idOfIndexerID(hit.ID)
  110. if err != nil {
  111. return nil, err
  112. }
  113. }
  114. return issueIDs, nil
  115. }