From a8d4e40af56ffeab9c040a13e01b9e0d5487bd3b Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Sat, 3 Aug 2019 19:15:48 -0300 Subject: [PATCH] Make repository indexer tokenize by camel case selectable --- custom/conf/app.ini.sample | 3 +++ .../doc/advanced/config-cheat-sheet.en-us.md | 2 ++ modules/indexer/repo.go | 13 +++++++++-- modules/setting/indexer.go | 22 ++++++++++--------- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 991a2a3e6b..42b9f56342 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -296,6 +296,9 @@ REPO_INDEXER_ENABLED = false REPO_INDEXER_PATH = indexers/repos.bleve UPDATE_BUFFER_LEN = 20 MAX_FILE_SIZE = 1048576 +; Break camel case names into separate words for indexing. +; It's imperative to delete any previous indexes from REPO_INDEXER_PATH after changing this setting. +REPO_INDEXER_CAMEL_CASE = true [admin] ; Disallow regular (non-admin) users from creating organizations. diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 6f3bc465a6..0e71b4f022 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -179,6 +179,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `REPO_INDEXER_PATH`: **indexers/repos.bleve**: Index file used for code search. - `UPDATE_BUFFER_LEN`: **20**: Buffer length of index request. - `MAX_FILE_SIZE`: **1048576**: Maximum size in bytes of files to be indexed. +- `REPO_INDEXER_CAMEL_CASE`: **true**: When `REPO_INDEXER_CAMEL_CASE` is true, repository indexer will break camel case into words, so thisCameCaseName will be indexed as this, camel, case, name. It's imperative to delete any previous indexes from REPO_INDEXER_PATH after changing this setting. + ## Security (`security`) diff --git a/modules/indexer/repo.go b/modules/indexer/repo.go index 287d23854b..de45c2beb8 100644 --- a/modules/indexer/repo.go +++ b/modules/indexer/repo.go @@ -107,11 +107,20 @@ func createRepoIndexer(path string, latestVersion int) error { mapping := bleve.NewIndexMapping() if err = addUnicodeNormalizeTokenFilter(mapping); err != nil { return err - } else if err = mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]interface{}{ + } + + var tokenFilters []string + if setting.Indexer.RepoUseCamelCaseTokenizer { + tokenFilters = []string{unicodeNormalizeName, camelcase.Name, lowercase.Name, unique.Name} + } else { + tokenFilters = []string{unicodeNormalizeName, lowercase.Name, unique.Name} + } + + if err = mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]interface{}{ "type": custom.Name, "char_filters": []string{}, "tokenizer": unicode.Name, - "token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name, unique.Name}, + "token_filters": tokenFilters, }); err != nil { return err } diff --git a/modules/setting/indexer.go b/modules/setting/indexer.go index 36fd4a020b..ede8530bd1 100644 --- a/modules/setting/indexer.go +++ b/modules/setting/indexer.go @@ -19,16 +19,17 @@ const ( var ( // Indexer settings Indexer = struct { - IssueType string - IssuePath string - RepoIndexerEnabled bool - RepoPath string - UpdateQueueLength int - MaxIndexerFileSize int64 - IssueQueueType string - IssueQueueDir string - IssueQueueConnStr string - IssueQueueBatchNumber int + IssueType string + IssuePath string + RepoIndexerEnabled bool + RepoPath string + UpdateQueueLength int + MaxIndexerFileSize int64 + IssueQueueType string + IssueQueueDir string + IssueQueueConnStr string + IssueQueueBatchNumber int + RepoUseCamelCaseTokenizer bool }{ IssueType: "bleve", IssuePath: "indexers/issues.bleve", @@ -53,6 +54,7 @@ func newIndexerService() { } Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20) Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(1024 * 1024) + Indexer.RepoUseCamelCaseTokenizer = sec.Key("REPO_INDEXER_CAMEL_CASE").MustBool(true) Indexer.IssueQueueType = sec.Key("ISSUE_INDEXER_QUEUE_TYPE").MustString(LevelQueueType) Indexer.IssueQueueDir = sec.Key("ISSUE_INDEXER_QUEUE_DIR").MustString(path.Join(AppDataPath, "indexers/issues.queue")) Indexer.IssueQueueConnStr = sec.Key("ISSUE_INDEXER_QUEUE_CONN_STR").MustString(path.Join(AppDataPath, ""))