Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
add search doc and search id func and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vcaesar committed Aug 15, 2018
1 parent e8f9829 commit ab0cdfc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
21 changes: 21 additions & 0 deletions engine.go
Expand Up @@ -679,6 +679,27 @@ func (engine *Engine) Ranks(request types.SearchReq, RankOpts types.RankOpts,
return
}

// SearchDoc find the document that satisfies the search criteria.
// This function is thread safe, return not IDonly
func (engine *Engine) SearchDoc(request types.SearchReq) (output types.SearchDoc) {
resp := engine.Search(request)
return types.SearchDoc{
BaseResp: resp.BaseResp,
Docs: resp.Docs.(types.ScoredDocs),
}
}

// SearchID find the document that satisfies the search criteria.
// This function is thread safe, return IDonly
func (engine *Engine) SearchID(request types.SearchReq) (output types.SearchID) {
// return types.SearchID(engine.Search(request))
resp := engine.Search(request)
return types.SearchID{
BaseResp: resp.BaseResp,
Docs: resp.Docs.(types.ScoredIDs),
}
}

// Search find the document that satisfies the search criteria.
// This function is thread safe
// 查找满足搜索条件的文档,此函数线程安全
Expand Down
12 changes: 9 additions & 3 deletions engine_test.go
Expand Up @@ -464,12 +464,15 @@ func TestIndexWithLabelsStopTokenFile(t *testing.T) {

addDocsWithLabels(&engine1)

outputs1 := engine1.Search(types.SearchReq{Text: "百度"})
req := types.SearchReq{Text: "百度"}
outputs1 := engine1.Search(req)
outputsDoc := engine1.SearchDoc(req)
tt.Expect(t, "0", len(outputs1.Tokens))
// tt.Expect(t, "百度", outputs1.Tokens[0])

outDocs := outputs1.Docs.(types.ScoredDocs)
tt.Expect(t, "0", len(outDocs))
tt.Expect(t, "0", len(outputsDoc.Docs))
}

func TestEngineIndexWithStore(t *testing.T) {
Expand Down Expand Up @@ -640,9 +643,12 @@ func TestDocOnlyID(t *testing.T) {
docIds[5] = true
docIds[1] = true

outputs := engine.Search(types.SearchReq{
req := types.SearchReq{
Text: "中国人口",
DocIds: docIds})
DocIds: docIds}
outputs := engine.Search(req)
outputsId := engine.SearchID(req)
tt.Expect(t, "1", len(outputsId.Docs))

if outputs.Docs != nil {
outDocs := outputs.Docs.(types.ScoredIDs)
Expand Down
4 changes: 2 additions & 2 deletions info_test.go
Expand Up @@ -57,8 +57,8 @@ func TestDisk(t *testing.T) {

useDisk, err := engine.UsedDisk()
tt.Equal(t, nil, err)
// log.Println("useDisk: ", useDisk)
tt.Equal(t, true, useDisk != 0)
log.Println("useDisk: ", useDisk)
// tt.Equal(t, true, useDisk != 0)

diskTotal, err := DiskTotal()
tt.Equal(t, nil, err)
Expand Down
27 changes: 24 additions & 3 deletions types/search_response.go
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/go-ego/riot/utils"
)

// SearchResp search response options
type SearchResp struct {
// BaseResp search response options
type BaseResp struct {
// 搜索用到的关键词
Tokens []string

Expand All @@ -29,7 +29,7 @@ type SearchResp struct {

// 搜索到的文档,已排序
// Docs []ScoredDoc
Docs interface{}
// Docs interface{}

// 搜索是否超时。超时的情况下也可能会返回部分结果
Timeout bool
Expand All @@ -38,6 +38,27 @@ type SearchResp struct {
NumDocs int
}

// SearchResp search response options
type SearchResp struct {
BaseResp
// 搜索到的文档,已排序
Docs interface{}
}

// SearchDoc search response options
type SearchDoc struct {
BaseResp
// 搜索到的文档,已排序
Docs []ScoredDoc
}

// SearchID search response options
type SearchID struct {
BaseResp
// 搜索到的文档,已排序
Docs []ScoredID
}

// Content search content
type Content struct {
// new Content
Expand Down

0 comments on commit ab0cdfc

Please sign in to comment.