search

package module
v0.0.0-...-92f02a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2018 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// WordMaxLength 最大词长度
	WordMaxLength = 6
)

Functions

func Group

func Group(str string) []string

Group 句子分组

Example
fmt.Println(Group("太阳当空照"))
Output:
[太阳 太阳当 太阳当空 太阳当空照 阳当 阳当空 阳当空照 当空 当空照 空照]

func IsNaturalSplit

func IsNaturalSplit(str string) bool

IsNaturalSplit 自然分割的词

func Position

func Position(text, str string) []int

Position str 在 text 中所有位置

Example
fmt.Println(Position("abc cba aac", "a"))
Output:
[0 6 8 9]

func Split

func Split(str string) []string

Split 字符串拆分

Example
fmt.Println(Split("中国China山东省,威海市 环翠区。火炬"))
Output:
[中国 山东省 china 威海市 环翠区 火炬]

Types

type DB

type DB interface {
	// Close 数据库关闭
	Close() error
	// Get 获取
	Get(key []byte) ([]byte, error)
	// Put 写入
	Put(key, value []byte) error
	// Has 是否包含
	Has(key []byte) (bool, error)
	// Delete 删除
	Delete(key []byte) error
	// Iterator 迭代数据
	Iterator(prefix []byte, f func(key, value []byte)) error
	// IteratorKey 迭代Key
	IteratorKey(prefix []byte, f func(key []byte)) error
}

DB 数据库接口

type Document

type Document struct {
	Key      []byte    `json:"key"`               // 主键
	Title    string    `json:"title,omitempty"`   // 标题
	Summary  string    `json:"summary,omitempty"` // 摘要
	Content  string    `json:"content"`           // 内容
	Modified time.Time `json:"modified"`          // 修改时间
}

Document 文档

Example
doc := Document{
	Key:     []byte("123"),
	Title:   "标题",
	Summary: "简介",
	Content: "内容",
}
j, _ := json.Marshal(doc)

fmt.Println(string(j))
Output:
{"key":"MTIz","title":"标题","summary":"简介","content":"内容","modified":"0001-01-01T00:00:00Z"}

func (*Document) Inverted

func (doc *Document) Inverted(str string) (bool, []int)

Inverted 生成到排索引

Example
doc := Document{
	Key:     []byte("123"),
	Title:   "abca",
	Summary: "",
	Content: "cca",
}

fmt.Println(doc.Inverted("a"))
Output:
true [0 3 6]

func (*Document) Match

func (doc *Document) Match(str string) bool

Match 文档匹配

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine 引擎

func NewLevelEngine

func NewLevelEngine(path string) (*Engine, error)

NewLevelEngine leveldb 搜索引擎

Example
e, err := NewLevelEngine("/tmp/search_db")
defer e.Close()

fmt.Println(err)
Output:
<nil>

func (*Engine) Close

func (e *Engine) Close() error

Close 关闭引擎

func (*Engine) Delete

func (e *Engine) Delete(key []byte) error

Delete 删除文档

func (*Engine) Get

func (e *Engine) Get(key []byte) (doc *Document, err error)

Get 获取文档

func (*Engine) Has

func (e *Engine) Has(key []byte) (bool, error)

Has 文档是否索引过

Example
e, _ := NewLevelEngine("/tmp/search_db")
defer e.Close()

ok, err := e.Has([]byte("asd"))
fmt.Println(ok, err)
Output:
false <nil>

func (*Engine) IndexKeys

func (e *Engine) IndexKeys(f func(key []byte)) int

IndexKeys 获取索引文件Key

func (*Engine) IndexNum

func (e *Engine) IndexNum() int

IndexNum 索引文件数

func (*Engine) Put

func (e *Engine) Put(doc *Document)

Put 更新文档

Example
key := []byte("11")
e, _ := NewLevelEngine("/tmp/search_db")
defer e.Close()
doc := &Document{
	Key:     key,
	Title:   "不生气",
	Summary: "生气引发的疾病",
	Content: "关于生气有,我有些话要说",
}
e.Put(doc)

ok, err := e.Has(key)
fmt.Println(ok, err)

nd, err := e.Get(key)
fmt.Println(nd.Title == doc.Title, err)
Output:
true <nil>
true <nil>

func (*Engine) Search

func (e *Engine) Search(str string) []*Document

Search 搜索文档

Example
e, _ := NewLevelEngine("/tmp/search_db")
defer e.Close()

doc := &Document{
	Key:     []byte("11"),
	Title:   "不生气",
	Summary: "生气引发的疾病",
	Content: "关于生气,我有些话要说",
}
e.Put(doc)

fmt.Println(len(e.Search("生气")))
fmt.Println(len(e.Search("高兴")))
Output:
1
0

func (*Engine) SearchKey

func (e *Engine) SearchKey(str string) [][]byte

SearchKey 搜索文档主键

Example
e, _ := NewLevelEngine(fmt.Sprintf("/tmp/%s", utils.NewID('t')))
defer e.Close()

doc := &Document{
	Key:     []byte("11"),
	Title:   "不生气",
	Summary: "生气引发的疾病",
	Content: "关于生气,我有些话要说",
}
e.Put(doc)

keys := e.SearchKey("疾病")
fmt.Println(keys)

keys = e.SearchKey("发病")
fmt.Println(keys)
Output:
[[49 49]]
[]

type Leveldb

type Leveldb struct {
	// contains filtered or unexported fields
}

Leveldb 数据库支持

func (*Leveldb) Close

func (l *Leveldb) Close() error

Close 关闭数据库

func (*Leveldb) Delete

func (l *Leveldb) Delete(key []byte) error

Delete 删除

func (*Leveldb) Get

func (l *Leveldb) Get(key []byte) ([]byte, error)

Get 获取

func (*Leveldb) Has

func (l *Leveldb) Has(key []byte) (bool, error)

Has 包含

func (*Leveldb) Iterator

func (l *Leveldb) Iterator(prefix []byte, f func(key, value []byte)) error

Iterator 迭代获取数据

func (*Leveldb) IteratorKey

func (l *Leveldb) IteratorKey(prefix []byte, f func(key []byte)) error

IteratorKey 迭代Key

func (*Leveldb) Put

func (l *Leveldb) Put(key, value []byte) error

Put 修改

type Posting

type Posting struct {
	DocID utils.ID      // 文件标识
	Pos   sort.IntSlice // 位置信息
}

Posting 倒排索引项

func (*Posting) TF

func (p *Posting) TF() int

TF 出现次数

type Word

type Word []Posting

Word 词索引

func (*Word) Add

func (w *Word) Add(p Posting)

Add 增加倒排索引项

func (Word) Has

func (w Word) Has(id utils.ID) bool

Has 包含DocID

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址