| | |
| | | package blevex |
| | | |
| | | import ( |
| | | "fmt" |
| | | "github.com/blevesearch/bleve/v2" |
| | | "github.com/blevesearch/bleve/v2/mapping" |
| | | "github.com/yanyiwu/gojieba" |
| | |
| | | } |
| | | return index, err |
| | | } |
| | | func Search(indexName string, keyword string) (ids []string, err error) { |
| | | func Search(indexName string, keyword string, from, size int) (ids []string, total uint64, err error) { |
| | | index, err := LoadIndex(indexName) |
| | | if err != nil { |
| | | return nil, err |
| | | return |
| | | } |
| | | req := bleve.NewSearchRequest(bleve.NewQueryStringQuery(keyword)) |
| | | req.From = from |
| | | req.Size = size |
| | | res, err := index.Search(req) |
| | | if err != nil { |
| | | panic(err) |
| | | return nil, 0, err |
| | | } |
| | | if res == nil || res.Total == 0 { |
| | | return ids, nil |
| | | if res == nil { |
| | | return |
| | | } |
| | | for _, ret := range dealResult(res) { |
| | | ids = append(ids, ret.Id) |
| | | } |
| | | return ids, nil |
| | | return ids, res.Total, nil |
| | | } |
| | | |
| | | func ComplexSearch(indexName string, keyword string, conditions map[string]interface{}, from, size int) (ids []string, total uint64, err error) { |
| | | index, err := LoadIndex(indexName) |
| | | if err != nil { |
| | | return nil, 0, err |
| | | } |
| | | |
| | | // Create a boolean query with a should clause for fuzzy search |
| | | boolQuery := bleve.NewBooleanQuery() |
| | | |
| | | fuzzyQuery := bleve.NewFuzzyQuery(keyword) |
| | | fuzzyQuery.SetFuzziness(2) // Set the fuzziness level as needed |
| | | |
| | | boolQuery.AddShould(fuzzyQuery) |
| | | |
| | | // Add a must clause for category filtering |
| | | for key, val := range conditions { |
| | | query := bleve.NewQueryStringQuery(fmt.Sprintf("%s:%s", key, val)) |
| | | boolQuery.AddMust(query) |
| | | } |
| | | req := bleve.NewSearchRequest(boolQuery) |
| | | req.From = from |
| | | req.Size = size |
| | | res, err := index.Search(req) |
| | | if err != nil { |
| | | return nil, 0, err |
| | | } |
| | | if res == nil { |
| | | return |
| | | } |
| | | for _, ret := range dealResult(res) { |
| | | ids = append(ids, ret.Id) |
| | | } |
| | | return ids, res.Total, nil |
| | | } |
| | | |
| | | type Result struct { |