Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

appview: indexer: add indexer mappings

Signed-off-by: Seongmin Lee <boltlessengineer@proton.me>

authored by boltless.me and committed by

Tangled fc9517ce 4da76144

+77 -4
+3 -1
appview/indexer/bleve/query.go
··· 5 5 "github.com/blevesearch/bleve/v2/search/query" 6 6 ) 7 7 8 - func MatchAndQuery(field, keyword string) query.Query { 8 + func MatchAndQuery(field, keyword, analyzer string, fuzziness int) query.Query { 9 9 q := bleve.NewMatchQuery(keyword) 10 10 q.FieldVal = field 11 + q.Analyzer = analyzer 12 + q.Fuzziness = fuzziness 11 13 return q 12 14 } 13 15
+74 -3
appview/indexer/issues/indexer.go
··· 8 8 "os" 9 9 10 10 "github.com/blevesearch/bleve/v2" 11 + "github.com/blevesearch/bleve/v2/analysis/analyzer/custom" 12 + "github.com/blevesearch/bleve/v2/analysis/token/camelcase" 13 + "github.com/blevesearch/bleve/v2/analysis/token/lowercase" 14 + "github.com/blevesearch/bleve/v2/analysis/token/unicodenorm" 15 + "github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode" 11 16 "github.com/blevesearch/bleve/v2/index/upsidedown" 17 + "github.com/blevesearch/bleve/v2/mapping" 12 18 "github.com/blevesearch/bleve/v2/search/query" 13 19 "tangled.org/core/appview/db" 14 20 "tangled.org/core/appview/indexer/base36" ··· 22 16 "tangled.org/core/appview/models" 23 17 "tangled.org/core/appview/pagination" 24 18 tlog "tangled.org/core/log" 19 + ) 20 + 21 + const ( 22 + issueIndexerAnalyzer = "issueIndexer" 23 + issueIndexerDocType = "issueIndexerDocType" 24 + 25 + unicodeNormalizeName = "uicodeNormalize" 25 26 ) 26 27 27 28 type Indexer struct { ··· 59 46 l.Info("Initialized the issue indexer") 60 47 } 61 48 49 + func generateIssueIndexMapping() (mapping.IndexMapping, error) { 50 + mapping := bleve.NewIndexMapping() 51 + docMapping := bleve.NewDocumentMapping() 52 + 53 + textFieldMapping := bleve.NewTextFieldMapping() 54 + textFieldMapping.Store = false 55 + textFieldMapping.IncludeInAll = false 56 + 57 + boolFieldMapping := bleve.NewBooleanFieldMapping() 58 + boolFieldMapping.Store = false 59 + boolFieldMapping.IncludeInAll = false 60 + 61 + keywordFieldMapping := bleve.NewKeywordFieldMapping() 62 + keywordFieldMapping.Store = false 63 + keywordFieldMapping.IncludeInAll = false 64 + 65 + // numericFieldMapping := bleve.NewNumericFieldMapping() 66 + 67 + docMapping.AddFieldMappingsAt("title", textFieldMapping) 68 + docMapping.AddFieldMappingsAt("body", textFieldMapping) 69 + 70 + docMapping.AddFieldMappingsAt("repo_at", keywordFieldMapping) 71 + docMapping.AddFieldMappingsAt("is_open", boolFieldMapping) 72 + 73 + err := mapping.AddCustomTokenFilter(unicodeNormalizeName, map[string]any{ 74 + "type": unicodenorm.Name, 75 + "form": unicodenorm.NFC, 76 + }) 77 + if err != nil { 78 + return nil, err 79 + } 80 + 81 + err = mapping.AddCustomAnalyzer(issueIndexerAnalyzer, map[string]any{ 82 + "type": custom.Name, 83 + "char_filters": []string{}, 84 + "tokenizer": unicode.Name, 85 + "token_filters": []string{unicodeNormalizeName, camelcase.Name, lowercase.Name}, 86 + }) 87 + if err != nil { 88 + return nil, err 89 + } 90 + 91 + mapping.DefaultAnalyzer = issueIndexerAnalyzer 92 + mapping.AddDocumentMapping(issueIndexerDocType, docMapping) 93 + mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping()) 94 + mapping.DefaultMapping = bleve.NewDocumentDisabledMapping() 95 + 96 + return mapping, nil 97 + } 98 + 62 99 func (ix *Indexer) intialize(ctx context.Context) (bool, error) { 63 100 if ix.indexer != nil { 64 101 return false, errors.New("indexer is already initialized") ··· 123 60 return true, nil 124 61 } 125 62 126 - mapping := bleve.NewIndexMapping() 63 + mapping, err := generateIssueIndexMapping() 64 + if err != nil { 65 + return false, err 66 + } 127 67 indexer, err = bleve.New(ix.path, mapping) 128 68 if err != nil { 129 69 return false, err ··· 189 123 } 190 124 } 191 125 126 + // Type returns the document type, for bleve's mapping.Classifier interface. 127 + func (i *issueData) Type() string { 128 + return issueIndexerDocType 129 + } 130 + 192 131 type IssueCommentData struct { 193 132 Body string `json:"body"` 194 133 } ··· 222 151 223 152 if opts.Keyword != "" { 224 153 queries = append(queries, bleve.NewDisjunctionQuery( 225 - bleveutil.MatchAndQuery("title", opts.Keyword), 226 - bleveutil.MatchAndQuery("body", opts.Keyword), 154 + bleveutil.MatchAndQuery("title", opts.Keyword, issueIndexerAnalyzer, 0), 155 + bleveutil.MatchAndQuery("body", opts.Keyword, issueIndexerAnalyzer, 0), 227 156 )) 228 157 } 229 158 queries = append(queries, bleveutil.KeywordFieldQuery("repo_at", opts.RepoAt))