Added search index

This commit is contained in:
Eduard Urbach 2017-06-20 23:27:14 +02:00
parent 65d43d2b43
commit 4837bf7e4f
3 changed files with 90 additions and 49 deletions

56
jobs/search-index/main.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"strings"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
)
func main() {
aero.Parallel(updateAnimeIndex, updateUserIndex)
}
func updateAnimeIndex() {
animeSearchIndex := arn.NewSearchIndex()
// Anime
animeStream, err := arn.AllAnime()
if err != nil {
panic(err)
}
for anime := range animeStream {
animeSearchIndex.TextToID[strings.ToLower(anime.Title.Canonical)] = anime.ID
}
// Save in database
err = arn.DB.Set("SearchIndex", "Anime", animeSearchIndex)
if err != nil {
panic(err)
}
}
func updateUserIndex() {
userSearchIndex := arn.NewSearchIndex()
// Users
userStream, err := arn.AllUsers()
if err != nil {
panic(err)
}
for user := range userStream {
userSearchIndex.TextToID[strings.ToLower(user.Nick)] = user.ID
}
// Save in database
err = arn.DB.Set("SearchIndex", "User", userSearchIndex)
if err != nil {
panic(err)
}
}

View File

@ -8,49 +8,8 @@ import (
"github.com/animenotifier/notify.moe/components"
)
const maxUsers = 18
const maxAnime = 18
type AnimeID = string
type UserID = string
var animeSearchIndex = make(map[string]AnimeID)
var userSearchIndex = make(map[string]UserID)
func init() {
updateSearchIndex()
}
func updateSearchIndex() {
updateAnimeIndex()
updateUserIndex()
}
func updateAnimeIndex() {
// Anime
animeStream, err := arn.AllAnime()
if err != nil {
panic(err)
}
for anime := range animeStream {
animeSearchIndex[strings.ToLower(anime.Title.Canonical)] = anime.ID
}
}
func updateUserIndex() {
// Users
userStream, err := arn.AllUsers()
if err != nil {
panic(err)
}
for user := range userStream {
userSearchIndex[strings.ToLower(user.Nick)] = user.ID
}
}
const maxUsers = 9 * 7
const maxAnime = 9 * 7
// Get search page.
func Get(ctx *aero.Context) string {
@ -59,10 +18,20 @@ func Get(ctx *aero.Context) string {
var users []*arn.User
var animeResults []*arn.Anime
// Search everything in parallel
aero.Parallel(func() {
for name, id := range userSearchIndex {
// Search users
var user *arn.User
userSearchIndex, err := arn.GetSearchIndex("User")
if err != nil {
return
}
for name, id := range userSearchIndex.TextToID {
if strings.Index(name, term) != -1 {
user, err := arn.GetUser(id)
user, err = arn.GetUser(id)
if err != nil {
continue
@ -76,9 +45,18 @@ func Get(ctx *aero.Context) string {
}
}
}, func() {
for title, id := range animeSearchIndex {
// Search anime
var anime *arn.Anime
animeSearchIndex, err := arn.GetSearchIndex("Anime")
if err != nil {
return
}
for title, id := range animeSearchIndex.TextToID {
if strings.Index(title, term) != -1 {
anime, err := arn.GetAnime(id)
anime, err = arn.GetAnime(id)
if err != nil {
continue

View File

@ -21,7 +21,14 @@ export function search(arn: AnimeNotifier, search: HTMLInputElement, e: Keyboard
return
}
arn.app.content.innerHTML = "<h2>" + term + "</h2><div id='results'></div>"
var results = arn.app.find("results")
if(!results) {
results = document.createElement("div")
results.id = "results"
arn.app.content.innerHTML = ""
arn.app.content.appendChild(results)
}
arn.app.get("/_/search/" + encodeURI(term))
.then(html => {
@ -29,7 +36,7 @@ export function search(arn: AnimeNotifier, search: HTMLInputElement, e: Keyboard
return
}
arn.app.find("results").innerHTML = html
results.innerHTML = html
arn.app.emit("DOMContentLoaded")
})
}