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 return nil, nil, err 435 } 436 437 createdTime, err := time.Parse(time.RFC3339, createdAt) 438 if err != nil { 439 return nil, nil, err
··· 434 return nil, nil, err 435 } 436 437 + issue.RepoAt = repoAt 438 createdTime, err := time.Parse(time.RFC3339, createdAt) 439 if err != nil { 440 return nil, nil, err
+56 -1
appview/indexer/issues/indexer.go
··· 8 9 "github.com/blevesearch/bleve/v2" 10 "github.com/blevesearch/bleve/v2/index/upsidedown" 11 "github.com/blevesearch/bleve/v2/search/query" 12 "tangled.sh/tangled.sh/core/appview/db" 13 "tangled.sh/tangled.sh/core/appview/indexer/base36" ··· 16 "tangled.sh/tangled.sh/core/appview/pagination" 17 ) 18 19 type Indexer struct { 20 indexer bleve.Index 21 path string ··· 43 log.Println("Initialized the issue indexer") 44 } 45 46 func (ix *Indexer) intialize(_ context.Context) (bool, error) { 47 if ix.indexer != nil { 48 return false, errors.New("indexer is already initialized") ··· 57 return true, nil 58 } 59 60 - mapping := bleve.NewIndexMapping() 61 indexer, err = bleve.New(ix.path, mapping) 62 if err != nil { 63 return false, err ··· 96 } 97 dataList = append(dataList, &IssueData{ 98 ID: issue.ID, 99 IssueID: issue.IssueId, 100 Title: issue.Title, 101 Body: issue.Body, 102 IsOpen: issue.Open, 103 }) 104 } 105 err = ix.Index(ctx, dataList...) 106 if err != nil { ··· 117 // IssueData data stored and will be indexed 118 type IssueData struct { 119 ID int64 `json:"id"` 120 IssueID int `json:"issue_id"` 121 Title string `json:"title"` 122 Body string `json:"body"` ··· 125 Comments []IssueCommentData `json:"comments"` 126 } 127 128 type IssueCommentData struct { 129 Body string `json:"body"` 130 } ··· 156 matchAndQuery(opts.Keyword, "body"), 157 )) 158 } 159 queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open")) 160 // TODO: append more queries 161 ··· 190 q.FieldVal = field 191 return q 192 }
··· 8 9 "github.com/blevesearch/bleve/v2" 10 "github.com/blevesearch/bleve/v2/index/upsidedown" 11 + "github.com/blevesearch/bleve/v2/mapping" 12 "github.com/blevesearch/bleve/v2/search/query" 13 "tangled.sh/tangled.sh/core/appview/db" 14 "tangled.sh/tangled.sh/core/appview/indexer/base36" ··· 17 "tangled.sh/tangled.sh/core/appview/pagination" 18 ) 19 20 + const ( 21 + issueIndexerDocType = "issueIndexerDocType" 22 + ) 23 + 24 type Indexer struct { 25 indexer bleve.Index 26 path string ··· 48 log.Println("Initialized the issue indexer") 49 } 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 + 82 func (ix *Indexer) intialize(_ context.Context) (bool, error) { 83 if ix.indexer != nil { 84 return false, errors.New("indexer is already initialized") ··· 93 return true, nil 94 } 95 96 + mapping, err := generateIssueIndexMapping() 97 + if err != nil { 98 + return false, err 99 + } 100 indexer, err = bleve.New(ix.path, mapping) 101 if err != nil { 102 return false, err ··· 135 } 136 dataList = append(dataList, &IssueData{ 137 ID: issue.ID, 138 + RepoAt: issue.RepoAt.String(), 139 IssueID: issue.IssueId, 140 Title: issue.Title, 141 Body: issue.Body, 142 IsOpen: issue.Open, 143 }) 144 + log.Println(dataList[len(dataList)-1]) 145 } 146 err = ix.Index(ctx, dataList...) 147 if err != nil { ··· 158 // IssueData data stored and will be indexed 159 type IssueData struct { 160 ID int64 `json:"id"` 161 + RepoAt string `json:"repo_at"` 162 IssueID int `json:"issue_id"` 163 Title string `json:"title"` 164 Body string `json:"body"` ··· 167 Comments []IssueCommentData `json:"comments"` 168 } 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 + 176 type IssueCommentData struct { 177 Body string `json:"body"` 178 } ··· 204 matchAndQuery(opts.Keyword, "body"), 205 )) 206 } 207 + queries = append(queries, keywordFieldQuery(opts.RepoAt, "repo_at")) 208 queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open")) 209 // TODO: append more queries 210 ··· 239 q.FieldVal = field 240 return q 241 } 242 + 243 + func keywordFieldQuery(keyword, field string) query.Query { 244 + q := bleve.NewTermQuery(keyword) 245 + q.FieldVal = field 246 + return q 247 + }