mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Update index when updating resources.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
@@ -20,6 +21,19 @@ type ResourceParams struct {
|
||||
|
||||
var index bleve.Index
|
||||
|
||||
func AddResourceToIndex(r model.Resource) error {
|
||||
return index.Index(fmt.Sprintf("%d", r.ID), ResourceParams{
|
||||
Id: r.ID,
|
||||
Title: r.Title,
|
||||
Subtitles: r.AlternativeTitles,
|
||||
Time: r.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func RemoveResourceFromIndex(id uint) error {
|
||||
return index.Delete(fmt.Sprintf("%d", id))
|
||||
}
|
||||
|
||||
func createIndex() error {
|
||||
for !dao.IsReady() {
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -32,12 +46,7 @@ func createIndex() error {
|
||||
return err
|
||||
}
|
||||
for _, r := range res {
|
||||
err := index.Index(fmt.Sprintf("%d", r.ID), ResourceParams{
|
||||
Id: r.ID,
|
||||
Title: r.Title,
|
||||
Subtitles: r.AlternativeTitles,
|
||||
Time: r.CreatedAt,
|
||||
})
|
||||
err := AddResourceToIndex(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -53,7 +62,7 @@ func init() {
|
||||
|
||||
var err error
|
||||
index, err = bleve.Open(indexPath)
|
||||
if err == bleve.ErrorIndexPathDoesNotExist {
|
||||
if errors.Is(err, bleve.ErrorIndexPathDoesNotExist) {
|
||||
mapping := bleve.NewIndexMapping()
|
||||
index, err = bleve.New(indexPath, mapping)
|
||||
if err != nil {
|
||||
@@ -74,7 +83,6 @@ func SearchResource(keyword string) ([]uint, error) {
|
||||
query := bleve.NewMatchQuery(keyword)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
searchRequest.Size = 1000
|
||||
searchRequest.Fields = []string{"Time"}
|
||||
searchResults, err := index.Search(searchRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,7 +90,7 @@ func SearchResource(keyword string) ([]uint, error) {
|
||||
|
||||
results := make([]uint, 0)
|
||||
for _, hit := range searchResults.Hits {
|
||||
if hit.Score < 0.8 {
|
||||
if hit.Score < 0.6 {
|
||||
continue
|
||||
}
|
||||
id, err := strconv.ParseUint(hit.ID, 10, 32)
|
||||
|
@@ -73,6 +73,9 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
if err != nil {
|
||||
log.Error("AddNewResourceActivity error: ", err)
|
||||
}
|
||||
if err := search.AddResourceToIndex(r); err != nil {
|
||||
log.Error("AddResourceToIndex error: ", err)
|
||||
}
|
||||
return r.ID, nil
|
||||
}
|
||||
|
||||
@@ -297,18 +300,7 @@ func SearchResource(query string, page int) ([]model.ResourceView, int, error) {
|
||||
return nil, 0, err
|
||||
}
|
||||
if first {
|
||||
for _, id := range res {
|
||||
found := false
|
||||
for _, id2 := range temp {
|
||||
if id == id2 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
temp = append(temp, id)
|
||||
}
|
||||
}
|
||||
temp = utils.RemoveDuplicate(res)
|
||||
first = false
|
||||
} else {
|
||||
temp1 := make([]uint, 0)
|
||||
@@ -324,21 +316,7 @@ func SearchResource(query string, page int) ([]model.ResourceView, int, error) {
|
||||
}
|
||||
}
|
||||
resources = append(resources, temp...)
|
||||
|
||||
// remove duplicates
|
||||
temp = make([]uint, 0)
|
||||
for _, id := range resources {
|
||||
found := false
|
||||
for _, id2 := range temp {
|
||||
if id == id2 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
temp = append(temp, id)
|
||||
}
|
||||
}
|
||||
resources = utils.RemoveDuplicate(resources)
|
||||
|
||||
if start >= len(resources) {
|
||||
return []model.ResourceView{}, 0, nil
|
||||
@@ -393,6 +371,9 @@ func DeleteResource(uid, id uint) error {
|
||||
if err != nil {
|
||||
log.Error("Error updating cached tag list:", err)
|
||||
}
|
||||
if err := search.RemoveResourceFromIndex(id); err != nil {
|
||||
log.Error("RemoveResourceFromIndex error: ", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -474,6 +455,9 @@ func EditResource(uid, rid uint, params *ResourceParams) error {
|
||||
if err != nil {
|
||||
log.Error("AddUpdateResourceActivity error: ", err)
|
||||
}
|
||||
if err := search.AddResourceToIndex(r); err != nil {
|
||||
log.Error("AddResourceToIndex error: ", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
13
server/utils/slice.go
Normal file
13
server/utils/slice.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
func RemoveDuplicate[T comparable](slice []T) []T {
|
||||
seen := make(map[T]struct{})
|
||||
var result []T
|
||||
for _, v := range slice {
|
||||
if _, ok := seen[v]; !ok {
|
||||
seen[v] = struct{}{}
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user