From a8d4e40af56ffeab9c040a13e01b9e0d5487bd3b Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Sat, 3 Aug 2019 19:15:48 -0300 Subject: [PATCH 1/5] 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 991a2a3e6b4b1..42b9f563426f6 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 6f3bc465a659a..0e71b4f0227fc 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 287d23854b2d8..de45c2beb8e32 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 36fd4a020b2ce..ede8530bd1c5c 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, "")) From ea1f22d2bba4bbba4d1c5bd3154196b213ee6ec0 Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Sun, 4 Aug 2019 11:02:07 -0300 Subject: [PATCH 2/5] Revert "Make repository indexer tokenize by camel case selectable" This reverts commit a8d4e40af56ffeab9c040a13e01b9e0d5487bd3b. --- 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, 12 insertions(+), 28 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 42b9f563426f6..991a2a3e6b4b1 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -296,9 +296,6 @@ 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 0e71b4f0227fc..6f3bc465a659a 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -179,8 +179,6 @@ 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 de45c2beb8e32..287d23854b2d8 100644 --- a/modules/indexer/repo.go +++ b/modules/indexer/repo.go @@ -107,20 +107,11 @@ func createRepoIndexer(path string, latestVersion int) error { mapping := bleve.NewIndexMapping() if err = addUnicodeNormalizeTokenFilter(mapping); err != nil { return err - } - - 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{}{ + } else if err = mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]interface{}{ "type": custom.Name, "char_filters": []string{}, "tokenizer": unicode.Name, - "token_filters": tokenFilters, + "token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name, unique.Name}, }); err != nil { return err } diff --git a/modules/setting/indexer.go b/modules/setting/indexer.go index ede8530bd1c5c..36fd4a020b2ce 100644 --- a/modules/setting/indexer.go +++ b/modules/setting/indexer.go @@ -19,17 +19,16 @@ 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 - RepoUseCamelCaseTokenizer bool + IssueType string + IssuePath string + RepoIndexerEnabled bool + RepoPath string + UpdateQueueLength int + MaxIndexerFileSize int64 + IssueQueueType string + IssueQueueDir string + IssueQueueConnStr string + IssueQueueBatchNumber int }{ IssueType: "bleve", IssuePath: "indexers/issues.bleve", @@ -54,7 +53,6 @@ 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, "")) From 6a443bff35bb93d6543cca9cbb3ee922c2fe3d27 Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Sun, 4 Aug 2019 12:08:30 -0300 Subject: [PATCH 3/5] Remove app.ini setting, permanently remove camel case tokenizer --- modules/indexer/repo.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/indexer/repo.go b/modules/indexer/repo.go index 287d23854b2d8..ac9ecd3604ecf 100644 --- a/modules/indexer/repo.go +++ b/modules/indexer/repo.go @@ -12,7 +12,8 @@ import ( "github.com/blevesearch/bleve" "github.com/blevesearch/bleve/analysis/analyzer/custom" - "github.com/blevesearch/bleve/analysis/token/camelcase" + // Don't break indexes from previous versions created with camelcase + _ "github.com/blevesearch/bleve/analysis/token/camelcase" "github.com/blevesearch/bleve/analysis/token/lowercase" "github.com/blevesearch/bleve/analysis/token/unique" "github.com/blevesearch/bleve/analysis/tokenizer/unicode" @@ -111,7 +112,7 @@ func createRepoIndexer(path string, latestVersion int) error { "type": custom.Name, "char_filters": []string{}, "tokenizer": unicode.Name, - "token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name, unique.Name}, + "token_filters": []string{unicodeNormalizeName, lowercase.Name, unique.Name}, }); err != nil { return err } From b222c0e26043b11a077647e7a02ab17a40c461cc Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Mon, 5 Aug 2019 01:49:38 -0300 Subject: [PATCH 4/5] Increase repoIndexerLatestVersion to force indexes to be rebuilt --- modules/indexer/repo.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/indexer/repo.go b/modules/indexer/repo.go index ac9ecd3604ecf..787f9a9467ab0 100644 --- a/modules/indexer/repo.go +++ b/modules/indexer/repo.go @@ -12,8 +12,6 @@ import ( "github.com/blevesearch/bleve" "github.com/blevesearch/bleve/analysis/analyzer/custom" - // Don't break indexes from previous versions created with camelcase - _ "github.com/blevesearch/bleve/analysis/token/camelcase" "github.com/blevesearch/bleve/analysis/token/lowercase" "github.com/blevesearch/bleve/analysis/token/unique" "github.com/blevesearch/bleve/analysis/tokenizer/unicode" @@ -25,7 +23,7 @@ const ( repoIndexerAnalyzer = "repoIndexerAnalyzer" repoIndexerDocType = "repoIndexerDocType" - repoIndexerLatestVersion = 1 + repoIndexerLatestVersion = 2 ) // repoIndexer (thread-safe) index for repository contents From 2e4479c9cd8b031379c11ceb2811952550307182 Mon Sep 17 00:00:00 2001 From: Guillermo Prandi Date: Mon, 5 Aug 2019 21:05:37 -0300 Subject: [PATCH 5/5] Changes introduced by the commmand make vendor --- .../analysis/token/camelcase/camelcase.go | 78 ------------- .../bleve/analysis/token/camelcase/parser.go | 109 ------------------ .../bleve/analysis/token/camelcase/states.go | 87 -------------- vendor/modules.txt | 1 - 4 files changed, 275 deletions(-) delete mode 100644 vendor/github.com/blevesearch/bleve/analysis/token/camelcase/camelcase.go delete mode 100644 vendor/github.com/blevesearch/bleve/analysis/token/camelcase/parser.go delete mode 100644 vendor/github.com/blevesearch/bleve/analysis/token/camelcase/states.go diff --git a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/camelcase.go b/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/camelcase.go deleted file mode 100644 index 42b7483c65674..0000000000000 --- a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/camelcase.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) 2016 Couchbase, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package camelcase - -import ( - "bytes" - "unicode/utf8" - - "github.com/blevesearch/bleve/analysis" - "github.com/blevesearch/bleve/registry" -) - -const Name = "camelCase" - -// CamelCaseFilter splits a given token into a set of tokens where each resulting token -// falls into one the following classes: -// 1) Upper case followed by lower case letters. -// Terminated by a number, an upper case letter, and a non alpha-numeric symbol. -// 2) Upper case followed by upper case letters. -// Terminated by a number, an upper case followed by a lower case letter, and a non alpha-numeric symbol. -// 3) Lower case followed by lower case letters. -// Terminated by a number, an upper case letter, and a non alpha-numeric symbol. -// 4) Number followed by numbers. -// Terminated by a letter, and a non alpha-numeric symbol. -// 5) Non alpha-numeric symbol followed by non alpha-numeric symbols. -// Terminated by a number, and a letter. -// -// It does a one-time sequential pass over an input token, from left to right. -// The scan is greedy and generates the longest substring that fits into one of the classes. -// -// See the test file for examples of classes and their parsings. -type CamelCaseFilter struct{} - -func NewCamelCaseFilter() *CamelCaseFilter { - return &CamelCaseFilter{} -} - -func (f *CamelCaseFilter) Filter(input analysis.TokenStream) analysis.TokenStream { - rv := make(analysis.TokenStream, 0, len(input)) - - nextPosition := 1 - for _, token := range input { - runeCount := utf8.RuneCount(token.Term) - runes := bytes.Runes(token.Term) - - p := NewParser(runeCount, nextPosition, token.Start) - for i := 0; i < runeCount; i++ { - if i+1 >= runeCount { - p.Push(runes[i], nil) - } else { - p.Push(runes[i], &runes[i+1]) - } - } - rv = append(rv, p.FlushTokens()...) - nextPosition = p.NextPosition() - } - return rv -} - -func CamelCaseFilterConstructor(config map[string]interface{}, cache *registry.Cache) (analysis.TokenFilter, error) { - return NewCamelCaseFilter(), nil -} - -func init() { - registry.RegisterTokenFilter(Name, CamelCaseFilterConstructor) -} diff --git a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/parser.go b/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/parser.go deleted file mode 100644 index ff4ce2fea772f..0000000000000 --- a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/parser.go +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (c) 2016 Couchbase, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package camelcase - -import ( - "github.com/blevesearch/bleve/analysis" -) - -func (p *Parser) buildTokenFromTerm(buffer []rune) *analysis.Token { - term := analysis.BuildTermFromRunes(buffer) - token := &analysis.Token{ - Term: term, - Position: p.position, - Start: p.index, - End: p.index + len(term), - } - p.position++ - p.index += len(term) - return token -} - -// Parser accepts a symbol and passes it to the current state (representing a class). -// The state can accept it (and accumulate it). Otherwise, the parser creates a new state that -// starts with the pushed symbol. -// -// Parser accumulates a new resulting token every time it switches state. -// Use FlushTokens() to get the results after the last symbol was pushed. -type Parser struct { - bufferLen int - buffer []rune - current State - tokens []*analysis.Token - position int - index int -} - -func NewParser(length, position, index int) *Parser { - return &Parser{ - bufferLen: length, - buffer: make([]rune, 0, length), - tokens: make([]*analysis.Token, 0, length), - position: position, - index: index, - } -} - -func (p *Parser) Push(sym rune, peek *rune) { - if p.current == nil { - // the start of parsing - p.current = p.NewState(sym) - p.buffer = append(p.buffer, sym) - - } else if p.current.Member(sym, peek) { - // same state, just accumulate - p.buffer = append(p.buffer, sym) - - } else { - // the old state is no more, thus convert the buffer - p.tokens = append(p.tokens, p.buildTokenFromTerm(p.buffer)) - - // let the new state begin - p.current = p.NewState(sym) - p.buffer = make([]rune, 0, p.bufferLen) - p.buffer = append(p.buffer, sym) - } -} - -// Note. States have to have different starting symbols. -func (p *Parser) NewState(sym rune) State { - var found State - - found = &LowerCaseState{} - if found.StartSym(sym) { - return found - } - - found = &UpperCaseState{} - if found.StartSym(sym) { - return found - } - - found = &NumberCaseState{} - if found.StartSym(sym) { - return found - } - - return &NonAlphaNumericCaseState{} -} - -func (p *Parser) FlushTokens() []*analysis.Token { - p.tokens = append(p.tokens, p.buildTokenFromTerm(p.buffer)) - return p.tokens -} - -func (p *Parser) NextPosition() int { - return p.position -} diff --git a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/states.go b/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/states.go deleted file mode 100644 index 97e08d044741b..0000000000000 --- a/vendor/github.com/blevesearch/bleve/analysis/token/camelcase/states.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) 2016 Couchbase, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package camelcase - -import ( - "unicode" -) - -// States codify the classes that the parser recognizes. -type State interface { - // is _sym_ the start character - StartSym(sym rune) bool - - // is _sym_ a member of a class. - // peek, the next sym on the tape, can also be used to determine a class. - Member(sym rune, peek *rune) bool -} - -type LowerCaseState struct{} - -func (s *LowerCaseState) Member(sym rune, peek *rune) bool { - return unicode.IsLower(sym) -} - -func (s *LowerCaseState) StartSym(sym rune) bool { - return s.Member(sym, nil) -} - -type UpperCaseState struct { - startedCollecting bool // denotes that the start character has been read - collectingUpper bool // denotes if this is a class of all upper case letters -} - -func (s *UpperCaseState) Member(sym rune, peek *rune) bool { - if !(unicode.IsLower(sym) || unicode.IsUpper(sym)) { - return false - } - - if peek != nil && unicode.IsUpper(sym) && unicode.IsLower(*peek) { - return false - } - - if !s.startedCollecting { - // now we have to determine if upper-case letters are collected. - s.startedCollecting = true - s.collectingUpper = unicode.IsUpper(sym) - return true - } - - return s.collectingUpper == unicode.IsUpper(sym) -} - -func (s *UpperCaseState) StartSym(sym rune) bool { - return unicode.IsUpper(sym) -} - -type NumberCaseState struct{} - -func (s *NumberCaseState) Member(sym rune, peek *rune) bool { - return unicode.IsNumber(sym) -} - -func (s *NumberCaseState) StartSym(sym rune) bool { - return s.Member(sym, nil) -} - -type NonAlphaNumericCaseState struct{} - -func (s *NonAlphaNumericCaseState) Member(sym rune, peek *rune) bool { - return !unicode.IsLower(sym) && !unicode.IsUpper(sym) && !unicode.IsNumber(sym) -} - -func (s *NonAlphaNumericCaseState) StartSym(sym rune) bool { - return s.Member(sym, nil) -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 187d128d7f13d..703f161a8aabc 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -24,7 +24,6 @@ github.com/beorn7/perks/quantile # github.com/blevesearch/bleve v0.0.0-20190214220507-05d86ea8f6e3 github.com/blevesearch/bleve github.com/blevesearch/bleve/analysis/analyzer/custom -github.com/blevesearch/bleve/analysis/token/camelcase github.com/blevesearch/bleve/analysis/token/lowercase github.com/blevesearch/bleve/analysis/token/unicodenorm github.com/blevesearch/bleve/analysis/token/unique