wip: add indexer mappings #495

merged
opened by boltless.me targeting master from boltless.me/core: feat/search
Changed files
+57 -1
appview
db
indexer
issues
+1
appview/db/issues.go
··· 434 434 return nil, nil, err 435 435 } 436 436 437 + issue.RepoAt = repoAt 437 438 createdTime, err := time.Parse(time.RFC3339, createdAt) 438 439 if err != nil { 439 440 return nil, nil, err
+56 -1
appview/indexer/issues/indexer.go
··· 8 8 9 9 "github.com/blevesearch/bleve/v2" 10 10 "github.com/blevesearch/bleve/v2/index/upsidedown" 11 + "github.com/blevesearch/bleve/v2/mapping" 11 12 "github.com/blevesearch/bleve/v2/search/query" 12 13 "tangled.sh/tangled.sh/core/appview/db" 13 14 "tangled.sh/tangled.sh/core/appview/indexer/base36" ··· 16 17 "tangled.sh/tangled.sh/core/appview/pagination" 17 18 ) 18 19 20 + const ( 21 + issueIndexerDocType = "issueIndexerDocType" 22 + ) 23 + 19 24 type Indexer struct { 20 25 indexer bleve.Index 21 26 path string ··· 43 48 log.Println("Initialized the issue indexer") 44 49 } 45 50 51 + func generateIssueIndexMapping() (mapping.IndexMapping, error) { 52 + mapping := bleve.NewIndexMapping() 53 + docMapping := bleve.NewDocumentMapping() 54 + 55 + textFieldMapping := bleve.NewTextFieldMapping() 56 + textFieldMapping.Store = false 57 + textFieldMapping.IncludeInAll = false 58 + 59 + boolFieldMapping := bleve.NewBooleanFieldMapping() 60 + boolFieldMapping.Store = false 61 + boolFieldMapping.IncludeInAll = false 62 + 63 + keywordFieldMapping := bleve.NewKeywordFieldMapping() 64 + keywordFieldMapping.Store = false 65 + keywordFieldMapping.IncludeInAll = false 66 + 67 + // numericFieldMapping := bleve.NewNumericFieldMapping() 68 + 69 + docMapping.AddFieldMappingsAt("title", textFieldMapping) 70 + docMapping.AddFieldMappingsAt("body", textFieldMapping) 71 + 72 + docMapping.AddFieldMappingsAt("repo_at", keywordFieldMapping) 73 + docMapping.AddFieldMappingsAt("is_open", boolFieldMapping) 74 + 75 + mapping.AddDocumentMapping(issueIndexerDocType, docMapping) 76 + mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping()) 77 + mapping.DefaultMapping = bleve.NewDocumentDisabledMapping() 78 + 79 + return mapping, nil 80 + } 81 + 46 82 func (ix *Indexer) intialize(_ context.Context) (bool, error) { 47 83 if ix.indexer != nil { 48 84 return false, errors.New("indexer is already initialized") ··· 57 93 return true, nil 58 94 } 59 95 60 - mapping := bleve.NewIndexMapping() 96 + mapping, err := generateIssueIndexMapping() 97 + if err != nil { 98 + return false, err 99 + } 61 100 indexer, err = bleve.New(ix.path, mapping) 62 101 if err != nil { 63 102 return false, err ··· 96 135 } 97 136 dataList = append(dataList, &IssueData{ 98 137 ID: issue.ID, 138 + RepoAt: issue.RepoAt.String(), 99 139 IssueID: issue.IssueId, 100 140 Title: issue.Title, 101 141 Body: issue.Body, 102 142 IsOpen: issue.Open, 103 143 }) 144 + log.Println(dataList[len(dataList)-1]) 104 145 } 105 146 err = ix.Index(ctx, dataList...) 106 147 if err != nil { ··· 117 158 // IssueData data stored and will be indexed 118 159 type IssueData struct { 119 160 ID int64 `json:"id"` 161 + RepoAt string `json:"repo_at"` 120 162 IssueID int `json:"issue_id"` 121 163 Title string `json:"title"` 122 164 Body string `json:"body"` ··· 125 167 Comments []IssueCommentData `json:"comments"` 126 168 } 127 169 170 + // Type returns the document type, for bleve's mapping.Classifier interface. 171 + func (i *IssueData) Type() string { 172 + return issueIndexerDocType 173 + } 174 + 175 + 128 176 type IssueCommentData struct { 129 177 Body string `json:"body"` 130 178 } ··· 156 204 matchAndQuery(opts.Keyword, "body"), 157 205 )) 158 206 } 207 + queries = append(queries, keywordFieldQuery(opts.RepoAt, "repo_at")) 159 208 queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open")) 160 209 // TODO: append more queries 161 210 ··· 190 239 q.FieldVal = field 191 240 return q 192 241 } 242 + 243 + func keywordFieldQuery(keyword, field string) query.Query { 244 + q := bleve.NewTermQuery(keyword) 245 + q.FieldVal = field 246 + return q 247 + }