Skip to content

Commit

Permalink
feat: Create separate searchable fields for id, title, cast, site & d…
Browse files Browse the repository at this point in the history
…escription (xbapps#760)

* Change search from one string of id, title, synopsis and cast to seperate search fields

* Use the Simple Analyzer for Title and Search indexes

* Fix whitespace formating
  • Loading branch information
toshski authored Jun 24, 2022
1 parent 1105826 commit 5ac6355
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/api/scenes.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (i SceneResource) searchSceneIndex(req *restful.Request, resp *restful.Resp
query := bleve.NewQueryStringQuery(q)

searchRequest := bleve.NewSearchRequest(query)
searchRequest.Fields = []string{"fulltext"}
searchRequest.Fields = []string{"Id", "title", "cast", "site", "description"}
searchRequest.IncludeLocations = true
searchRequest.From = 0
searchRequest.Size = 25
Expand Down
14 changes: 14 additions & 0 deletions pkg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,20 @@ func Migrate() {
return nil
},
},
{
// rebuild search indexes with new fields
ID: "034-rebuild-new-indexes",
Migrate: func(d *gorm.DB) error {
os.RemoveAll(common.IndexDirV2)
os.MkdirAll(common.IndexDirV2, os.ModePerm)
// rebuild asynchronously, no need to hold up startup, blocking the UI
go func() {
tasks.SearchIndex()
tasks.CalculateCacheSizes()
}()
return nil
},
},
})

if err := m.Migrate(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/models/model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ type Scene struct {

NeedsUpdate bool `json:"needs_update"`

Fulltext string `gorm:"-" json:"fulltext"`
Score float64 `gorm:"-" json:"_score"`
Description string `gorm:"-" json:"description"`
Score float64 `gorm:"-" json:"_score"`
}

type Image struct {
Expand Down
25 changes: 23 additions & 2 deletions pkg/tasks/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/analysis/analyzer/simple"
"github.com/blevesearch/bleve/index/scorch"
"github.com/sirupsen/logrus"
"github.com/xbapps/xbvr/pkg/common"
Expand All @@ -17,15 +18,31 @@ type Index struct {
}

type SceneIndexed struct {
Fulltext string `json:"fulltext"`
Description string `json:"description"`
Title string `json:"title"`
Cast string `json:"cast"`
Site string `json:"site"`
Id string `json:"id"`
}

func NewIndex(name string) (*Index, error) {
i := new(Index)

path := filepath.Join(common.IndexDirV2, name)

// the simple analyzer is more approriate for the title and cast
// note this does not effect search unless the query includes cast: or title:
titleFieldMapping := bleve.NewTextFieldMapping()
titleFieldMapping.Analyzer = simple.Name
castFieldMapping := bleve.NewTextFieldMapping()
castFieldMapping.Analyzer = simple.Name
sceneMapping := bleve.NewDocumentMapping()
sceneMapping.AddFieldMappingsAt("title", titleFieldMapping)
sceneMapping.AddFieldMappingsAt("cast", castFieldMapping)

mapping := bleve.NewIndexMapping()
mapping.AddDocumentMapping("_default", sceneMapping)

idx, err := bleve.NewUsing(path, mapping, scorch.Name, scorch.Name, nil)
if err != nil && err == bleve.ErrorIndexPathExists {
idx, err = bleve.Open(path)
Expand Down Expand Up @@ -55,7 +72,11 @@ func (i *Index) PutScene(scene models.Scene) error {
}

si := SceneIndexed{
Fulltext: fmt.Sprintf("%v %v %v %v %v %v", scene.SceneID, scene.Title, scene.Site, scene.Synopsis, cast, castConcat),
Title: fmt.Sprintf("%v", scene.Title),
Description: fmt.Sprintf("%v", scene.Synopsis),
Cast: fmt.Sprintf("%v %v", cast, castConcat),
Site: fmt.Sprintf("%v", scene.Site),
Id: fmt.Sprintf("%v", scene.SceneID),
}
if err := i.Bleve.Index(scene.SceneID, si); err != nil {
return err
Expand Down

0 comments on commit 5ac6355

Please sign in to comment.