Skip to content

Commit 312f706

Browse files
committed
Improvements on code snippet search (updated tokenizer and made ES' search case insensitive)
Signed-off-by: Bruno Sofiato <[email protected]>
1 parent 9206fbb commit 312f706

17 files changed

+81
-10
lines changed

modules/indexer/code/bleve/bleve.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
3232
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
3333
"github.com/blevesearch/bleve/v2/analysis/token/unicodenorm"
34+
"github.com/blevesearch/bleve/v2/analysis/tokenizer/letter"
3435
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
3536
"github.com/blevesearch/bleve/v2/mapping"
3637
"github.com/blevesearch/bleve/v2/search/query"
@@ -105,7 +106,7 @@ func generateBleveIndexMapping() (mapping.IndexMapping, error) {
105106
} else if err := mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]any{
106107
"type": analyzer_custom.Name,
107108
"char_filters": []string{},
108-
"tokenizer": unicode.Name,
109+
"tokenizer": letter.Name,
109110
"token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name},
110111
}); err != nil {
111112
return nil, err

modules/indexer/code/elasticsearch/elasticsearch.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const (
6060
"settings": {
6161
"analysis": {
6262
"analyzer": {
63+
"content_analyzer": {
64+
"tokenizer": "content_tokenizer",
65+
"filter" : ["lowercase"]
66+
},
6367
"filename_path_analyzer": {
6468
"tokenizer": "path_tokenizer"
6569
},
@@ -68,6 +72,10 @@ const (
6872
}
6973
},
7074
"tokenizer": {
75+
"content_tokenizer": {
76+
"type": "simple_pattern_split",
77+
"pattern": "[^a-zA-Z0-9]"
78+
},
7179
"path_tokenizer": {
7280
"type": "path_hierarchy",
7381
"delimiter": "/"
@@ -104,7 +112,8 @@ const (
104112
"content": {
105113
"type": "text",
106114
"term_vector": "with_positions_offsets",
107-
"index": true
115+
"index": true,
116+
"analyzer": "content_analyzer"
108117
},
109118
"commit_id": {
110119
"type": "keyword",

modules/indexer/code/indexer_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,55 @@ func testIndexer(name string, t *testing.T, indexer internal.Indexer) {
181181
},
182182
},
183183
},
184+
// Search for matches on the contents of files regardless of case.
185+
{
186+
RepoIDs: nil,
187+
Keyword: "dESCRIPTION",
188+
Langs: 1,
189+
Results: []codeSearchResult{
190+
{
191+
Filename: "README.md",
192+
Content: "# repo1\n\nDescription for repo1",
193+
},
194+
},
195+
},
196+
// Search for an exact match on the filename within the repo '62' (case insenstive).
197+
// This scenario yields a single result (the file avocado.md on the repo '62')
198+
{
199+
RepoIDs: []int64{62},
200+
Keyword: "AVOCADO.MD",
201+
Langs: 1,
202+
Results: []codeSearchResult{
203+
{
204+
Filename: "avocado.md",
205+
Content: "# repo1\n\npineaple pie of cucumber juice",
206+
},
207+
},
208+
},
209+
// Search for matches on the contents of files when the criteria is a expression.
210+
{
211+
RepoIDs: []int64{62},
212+
Keyword: "console.log",
213+
Langs: 1,
214+
Results: []codeSearchResult{
215+
{
216+
Filename: "example-file.js",
217+
Content: "console.log(\"Hello, World!\")",
218+
},
219+
},
220+
},
221+
// Search for matches on the contents of files when the criteria is part of a expression.
222+
{
223+
RepoIDs: []int64{62},
224+
Keyword: "log",
225+
Langs: 1,
226+
Results: []codeSearchResult{
227+
{
228+
Filename: "example-file.js",
229+
Content: "console.log(\"Hello, World!\")",
230+
},
231+
},
232+
},
184233
}
185234

186235
for _, kw := range keywords {

modules/indexer/internal/bleve/util.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package bleve
66
import (
77
"errors"
88
"os"
9+
"unicode"
910

1011
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/util"
1213

1314
"github.com/blevesearch/bleve/v2"
14-
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
15+
unicode_tokenizer "github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
1516
"github.com/blevesearch/bleve/v2/index/upsidedown"
1617
"github.com/ethantkoenig/rupture"
1718
)
@@ -57,7 +58,7 @@ func openIndexer(path string, latestVersion int) (bleve.Index, int, error) {
5758
// may be different on two string and they still be considered equivalent.
5859
// Given a phrasse, its shortest word determines its fuzziness. If a phrase uses CJK (eg: `갃갃갃` `啊啊啊`), the fuzziness is zero.
5960
func GuessFuzzinessByKeyword(s string) int {
60-
tokenizer := unicode.NewUnicodeTokenizer()
61+
tokenizer := unicode_tokenizer.NewUnicodeTokenizer()
6162
tokens := tokenizer.Tokenize([]byte(s))
6263

6364
if len(tokens) > 0 {
@@ -77,8 +78,10 @@ func guessFuzzinessByKeyword(s string) int {
7778
// according to https://github.com/blevesearch/bleve/issues/1563, the supported max fuzziness is 2
7879
// magic number 4 was chosen to determine the levenshtein distance per each character of a keyword
7980
// BUT, when using CJK (eg: `갃갃갃` `啊啊啊`), it mismatches a lot.
81+
// Likewise, queries whose terms contains characters that are *not* letters should not use fuzziness
82+
8083
for _, r := range s {
81-
if r >= 128 {
84+
if r >= 128 || !unicode.IsLetter(r) {
8285
return 0
8386
}
8487
}

modules/indexer/internal/bleve/util_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ func TestBleveGuessFuzzinessByKeyword(t *testing.T) {
3535
Input: "갃갃갃",
3636
Fuzziness: 0,
3737
},
38+
{
39+
Input: "repo1",
40+
Fuzziness: 0,
41+
},
42+
{
43+
Input: "avocado.md",
44+
Fuzziness: 0,
45+
},
3846
}
3947

4048
for _, scenario := range scenarios {

tests/gitea-repositories-meta/org42/search-by-path.git/description

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ This repository will be used to test code search. The snippet below shows its di
44
├── avocado.md
55
├── cucumber.md
66
├── ham.md
7-
└── potato
8-
└── ham.md
7+
├── potato
8+
| └── ham.md
9+
└── example-file.js

tests/gitea-repositories-meta/org42/search-by-path.git/info/refs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
65f1bf27bc3bf70f64657658635e66094edbcb4d refs/heads/develop
44
65f1bf27bc3bf70f64657658635e66094edbcb4d refs/heads/feature/1
55
78fb907e3a3309eae4fe8fef030874cebbf1cd5e refs/heads/home-md-img-check
6-
3731fe53b763859aaf83e703ee731f6b9447ff1e refs/heads/master
6+
9f894b61946fd2f7b8b9d8e370e4d62f915522f5 refs/heads/master
77
62fb502a7172d4453f0322a2cc85bddffa57f07a refs/heads/pr-to-update
88
4649299398e4d39a5c09eb4f534df6f1e1eb87cc refs/heads/sub-home-md-img-check
99
3fa2f829675543ecfc16b2891aebe8bf0608a8f4 refs/notes/commits
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
P pack-393dc29256bc27cb2ec73898507df710be7a3cf5.pack
1+
P pack-a7bef76cf6e2b46bc816936ab69306fb10aea571.pack
22

tests/gitea-repositories-meta/org42/search-by-path.git/packed-refs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
65f1bf27bc3bf70f64657658635e66094edbcb4d refs/heads/develop
55
65f1bf27bc3bf70f64657658635e66094edbcb4d refs/heads/feature/1
66
78fb907e3a3309eae4fe8fef030874cebbf1cd5e refs/heads/home-md-img-check
7-
3731fe53b763859aaf83e703ee731f6b9447ff1e refs/heads/master
7+
9f894b61946fd2f7b8b9d8e370e4d62f915522f5 refs/heads/master
88
62fb502a7172d4453f0322a2cc85bddffa57f07a refs/heads/pr-to-update
99
4649299398e4d39a5c09eb4f534df6f1e1eb87cc refs/heads/sub-home-md-img-check
1010
3fa2f829675543ecfc16b2891aebe8bf0608a8f4 refs/notes/commits

0 commit comments

Comments
 (0)