From 5428e38ef0e87e4e1cac6945b4ee8f160e370a84 Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Thu, 14 Aug 2025 01:14:57 +0900 Subject: [PATCH] appview: indexer: add indexer mappings Change-Id: tmkvwxporzqzvnpkluklsnvpztkoropq Signed-off-by: Seongmin Lee --- appview/db/issues.go | 1 + appview/indexer/issues/indexer.go | 57 ++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/appview/db/issues.go b/appview/db/issues.go index 67ad95e..4045b2a 100644 --- a/appview/db/issues.go +++ b/appview/db/issues.go @@ -434,6 +434,7 @@ func GetIssueWithComments(e Execer, repoAt syntax.ATURI, issueId int) (*Issue, [ return nil, nil, err } + issue.RepoAt = repoAt createdTime, err := time.Parse(time.RFC3339, createdAt) if err != nil { return nil, nil, err diff --git a/appview/indexer/issues/indexer.go b/appview/indexer/issues/indexer.go index d6b28af..f0f899c 100644 --- a/appview/indexer/issues/indexer.go +++ b/appview/indexer/issues/indexer.go @@ -8,6 +8,7 @@ import ( "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/index/upsidedown" + "github.com/blevesearch/bleve/v2/mapping" "github.com/blevesearch/bleve/v2/search/query" "tangled.sh/tangled.sh/core/appview/db" "tangled.sh/tangled.sh/core/appview/indexer/base36" @@ -16,6 +17,10 @@ import ( "tangled.sh/tangled.sh/core/appview/pagination" ) +const ( + issueIndexerDocType = "issueIndexerDocType" +) + type Indexer struct { indexer bleve.Index path string @@ -43,6 +48,37 @@ func (ix *Indexer) Init(ctx context.Context, e db.Execer) { log.Println("Initialized the issue indexer") } +func generateIssueIndexMapping() (mapping.IndexMapping, error) { + mapping := bleve.NewIndexMapping() + docMapping := bleve.NewDocumentMapping() + + textFieldMapping := bleve.NewTextFieldMapping() + textFieldMapping.Store = false + textFieldMapping.IncludeInAll = false + + boolFieldMapping := bleve.NewBooleanFieldMapping() + boolFieldMapping.Store = false + boolFieldMapping.IncludeInAll = false + + keywordFieldMapping := bleve.NewKeywordFieldMapping() + keywordFieldMapping.Store = false + keywordFieldMapping.IncludeInAll = false + + // numericFieldMapping := bleve.NewNumericFieldMapping() + + docMapping.AddFieldMappingsAt("title", textFieldMapping) + docMapping.AddFieldMappingsAt("body", textFieldMapping) + + docMapping.AddFieldMappingsAt("repo_at", keywordFieldMapping) + docMapping.AddFieldMappingsAt("is_open", boolFieldMapping) + + mapping.AddDocumentMapping(issueIndexerDocType, docMapping) + mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping()) + mapping.DefaultMapping = bleve.NewDocumentDisabledMapping() + + return mapping, nil +} + func (ix *Indexer) intialize(_ context.Context) (bool, error) { if ix.indexer != nil { return false, errors.New("indexer is already initialized") @@ -57,7 +93,10 @@ func (ix *Indexer) intialize(_ context.Context) (bool, error) { return true, nil } - mapping := bleve.NewIndexMapping() + mapping, err := generateIssueIndexMapping() + if err != nil { + return false, err + } indexer, err = bleve.New(ix.path, mapping) if err != nil { return false, err @@ -96,11 +135,13 @@ func PopulateIndexer(ctx context.Context, ix *Indexer, e db.Execer) error { } dataList = append(dataList, &IssueData{ ID: issue.ID, + RepoAt: issue.RepoAt.String(), IssueID: issue.IssueId, Title: issue.Title, Body: issue.Body, IsOpen: issue.Open, }) + log.Println(dataList[len(dataList)-1]) } err = ix.Index(ctx, dataList...) if err != nil { @@ -117,6 +158,7 @@ func PopulateIndexer(ctx context.Context, ix *Indexer, e db.Execer) error { // IssueData data stored and will be indexed type IssueData struct { ID int64 `json:"id"` + RepoAt string `json:"repo_at"` IssueID int `json:"issue_id"` Title string `json:"title"` Body string `json:"body"` @@ -125,6 +167,12 @@ type IssueData struct { Comments []IssueCommentData `json:"comments"` } +// Type returns the document type, for bleve's mapping.Classifier interface. +func (i *IssueData) Type() string { + return issueIndexerDocType +} + + type IssueCommentData struct { Body string `json:"body"` } @@ -156,6 +204,7 @@ func (ix *Indexer) Search(ctx context.Context, opts models.IssueSearchOptions) ( matchAndQuery(opts.Keyword, "body"), )) } + queries = append(queries, keywordFieldQuery(opts.RepoAt, "repo_at")) queries = append(queries, boolFieldQuery(opts.IsOpen, "is_open")) // TODO: append more queries @@ -190,3 +239,9 @@ func boolFieldQuery(val bool, field string) query.Query { q.FieldVal = field return q } + +func keywordFieldQuery(keyword, field string) query.Query { + q := bleve.NewTermQuery(keyword) + q.FieldVal = field + return q +} -- 2.43.0