github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/search/mapping.go (about) 1 // Copyright 2019 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package search 6 7 import ( 8 "github.com/blevesearch/bleve" 9 "github.com/blevesearch/bleve/analysis" 10 "github.com/blevesearch/bleve/analysis/char/html" 11 "github.com/blevesearch/bleve/analysis/token/lowercase" 12 "github.com/blevesearch/bleve/analysis/tokenizer/web" 13 "github.com/blevesearch/bleve/mapping" 14 "github.com/blevesearch/bleve/registry" 15 ) 16 17 const ( 18 htmlAnalyzerName = "kbfsHTML" 19 htmlFieldName = "HTML" 20 ) 21 22 func htmlAnalyzerConstructor( 23 config map[string]interface{}, cache *registry.Cache) ( 24 *analysis.Analyzer, error) { 25 tokenizer, err := cache.TokenizerNamed(web.Name) 26 if err != nil { 27 return nil, err 28 } 29 htmlFilter, err := cache.CharFilterNamed(html.Name) 30 if err != nil { 31 return nil, err 32 } 33 toLowerFilter, err := cache.TokenFilterNamed(lowercase.Name) 34 if err != nil { 35 return nil, err 36 } 37 rv := analysis.Analyzer{ 38 Tokenizer: tokenizer, 39 CharFilters: []analysis.CharFilter{ 40 htmlFilter, 41 }, 42 TokenFilters: []analysis.TokenFilter{ 43 toLowerFilter, 44 }, 45 } 46 return &rv, nil 47 } 48 49 func init() { 50 registry.RegisterAnalyzer(htmlAnalyzerName, htmlAnalyzerConstructor) 51 } 52 53 func makeIndexMapping() (*mapping.IndexMappingImpl, error) { 54 // Register a mapping for text and HTML files, so when we index 55 // text files we can mark them as such. 56 indexMapping := bleve.NewIndexMapping() 57 58 textMapping := mapping.NewDocumentMapping() 59 indexMapping.AddDocumentMapping(textFileType, textMapping) 60 61 htmlFieldMapping := mapping.NewTextFieldMapping() 62 htmlFieldMapping.Analyzer = htmlAnalyzerName 63 htmlDocMapping := mapping.NewDocumentMapping() 64 htmlDocMapping.AddFieldMappingsAt(htmlFieldName, htmlFieldMapping) 65 indexMapping.AddDocumentMapping(htmlFileType, htmlDocMapping) 66 67 return indexMapping, nil 68 }