129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
// "strings"
|
|
|
|
"github.com/blevesearch/bleve/v2"
|
|
"github.com/blevesearch/bleve/v2/search"
|
|
)
|
|
|
|
type pageStruct struct {
|
|
Timestamp string `json:"timestamp"`
|
|
Group string `json:"group"`
|
|
Category string `json:"category"`
|
|
Season string `json:"season"`
|
|
File string `json:"file"`
|
|
PageNum int `json:"pageNum"`
|
|
Readability float32 `json:"readability"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// Do search and return results
|
|
// Query reference: https://pkg.go.dev/github.com/blevesearch/bleve@v1.0.14/search/query#MatchQuery
|
|
func doSearch(term string, fuzziness int) (search.DocumentMatchCollection, error) {
|
|
// Single word
|
|
var search *bleve.SearchRequest
|
|
query := bleve.NewQueryStringQuery(term)
|
|
search = bleve.NewSearchRequestOptions(query, 100, 0, false)
|
|
|
|
search.Fields = []string{"*"}
|
|
results, err := index.Search(search)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return results.Hits, nil
|
|
}
|
|
|
|
// Create index if it doesn't already exist and return it
|
|
func openIndex(path string) (bool, bool) {
|
|
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
|
|
// Index does not exist
|
|
var err error
|
|
index, err = bleve.New(path, bleve.NewIndexMapping())
|
|
if !checkError(err) {
|
|
return false, false
|
|
}
|
|
return false, true
|
|
}
|
|
|
|
// Index already exists
|
|
var err error
|
|
index, err = bleve.Open(path)
|
|
if !checkError(err) {
|
|
return true, false
|
|
}
|
|
return true, true
|
|
}
|
|
|
|
// Index all documents
|
|
func indexDocuments() bool {
|
|
log.Println("Indexing documents...")
|
|
|
|
GRPS, err := ioutil.ReadDir(JSON)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
|
|
for _, GRP := range GRPS {
|
|
// Iterate through groups
|
|
GRP_PATH := JSON + "/" + GRP.Name()
|
|
CATS, _ := ioutil.ReadDir(GRP_PATH)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
|
|
for _, CAT := range CATS {
|
|
// Iterate through categories
|
|
CAT_PATH := GRP_PATH + "/" + CAT.Name()
|
|
FILES, _ := ioutil.ReadDir(CAT_PATH)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
|
|
for _, FILE := range FILES {
|
|
// Iterate through files
|
|
FILE_PATH := CAT_PATH + "/" + FILE.Name()
|
|
PAGES, _ := ioutil.ReadDir(FILE_PATH)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
|
|
for _, PAGE := range PAGES {
|
|
// Iterate through pages
|
|
PAGE_PATH := FILE_PATH + "/" + PAGE.Name()
|
|
|
|
// Read file
|
|
f, _ := os.Open(PAGE_PATH)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
b, _ := ioutil.ReadAll(f)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
err = f.Close()
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
|
|
// Index file
|
|
var page pageStruct
|
|
json.Unmarshal(b, &page)
|
|
|
|
ID := GRP.Name() + "-" + CAT.Name() + "-" + FILE.Name() + "-" + PAGE.Name()
|
|
err = index.Index(ID, page)
|
|
if !checkError(err) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|