better indexing and classifier
This commit is contained in:
parent
38c2a72fe0
commit
98b53d3a33
9 changed files with 168 additions and 32 deletions
|
|
@ -7,16 +7,25 @@ import (
|
|||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||
"git.giftfish.de/ston1th/godrop/v2"
|
||||
"github.com/blevesearch/bleve"
|
||||
"github.com/blevesearch/bleve/mapping"
|
||||
"html/template"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxSearchResult = 101
|
||||
|
||||
type indexDocument struct {
|
||||
Text string `json:"text"`
|
||||
Date string `json:"date"`
|
||||
Type string `json:"type"`
|
||||
const docType = "document"
|
||||
|
||||
type document struct {
|
||||
Text string `json:"text"`
|
||||
Keyword []string `json:"keyword"`
|
||||
Created time.Time `json:"created"`
|
||||
}
|
||||
|
||||
func (d *document) Type() string {
|
||||
return docType
|
||||
}
|
||||
|
||||
type Index struct {
|
||||
|
|
@ -30,8 +39,10 @@ func NewIndex(path string) (i *Index, err error) {
|
|||
return
|
||||
}
|
||||
if _, err = os.Stat(path); os.IsNotExist(err) {
|
||||
mapping := bleve.NewIndexMapping()
|
||||
bi, err = bleve.New(path, mapping)
|
||||
var m *mapping.IndexMappingImpl
|
||||
m, err = createMapping()
|
||||
//m := bleve.NewIndexMapping()
|
||||
bi, err = bleve.New(path, m)
|
||||
if err != nil {
|
||||
err = errors.New("index: " + err.Error())
|
||||
return
|
||||
|
|
@ -51,7 +62,7 @@ func (i *Index) Close() error {
|
|||
return i.i.Close()
|
||||
}
|
||||
|
||||
func (i *Index) Add(text, docType string) (id string, err error) {
|
||||
func (i *Index) Add(text string) (id string, err error) {
|
||||
id = newID()
|
||||
for {
|
||||
doc, _ := i.i.Document(id)
|
||||
|
|
@ -60,7 +71,8 @@ func (i *Index) Add(text, docType string) (id string, err error) {
|
|||
}
|
||||
id = newID()
|
||||
}
|
||||
err = i.i.Index(id, indexDocument{text, core.Now(), docType})
|
||||
k := classify(text)
|
||||
err = i.i.Index(id, &document{text, k, time.Now()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -86,12 +98,20 @@ func (i *Index) Search(search string) (results []core.Result, err error) {
|
|||
continue
|
||||
}
|
||||
for fragField, frags := range hit.Fragments {
|
||||
if fragField == "text" || fragField == "type" || fragField == "date" {
|
||||
if fragField == "text" || fragField == "created" {
|
||||
for _, f := range frags {
|
||||
r.HTML += template.HTML(f)
|
||||
}
|
||||
break
|
||||
}
|
||||
if fragField == "keyword" {
|
||||
var k string
|
||||
for _, f := range frags {
|
||||
k += f
|
||||
}
|
||||
r.Keyword = strings.ReplaceAll(strings.ReplaceAll(k, "<mark>", ""), "</mark>", "")
|
||||
break
|
||||
}
|
||||
}
|
||||
results = append(results, r)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue