英文:
How to increase (or access) Go bleve search engine results?
问题
当我尝试使用bleve进行搜索查询时,我总是只得到10个或更少的结果。我该如何增加这个限制或者在所有数据中进行搜索?
以下是代码示例,我期望得到35个结果,但实际上只得到了10个(注意:calc.Rand()
函数会生成随机字节):
package search
import (
"testing"
"github.com/blevesearch/bleve/v2"
)
func TestSearcher(t *testing.T) {
mapping := bleve.NewIndexMapping()
searcher, _ := bleve.New("search/src", mapping)
for i := 0; i < 35; i++ {
searcher.Index(string(calc.Rand()), "stuff")
}
query := bleve.NewMatchQuery("stuff")
search := bleve.NewSearchRequest(query)
searchRez, _ := searcher.Search(search)
t.Error(len(searchRez.Hits))
}
我得到的结果是:
--- FAIL: TestSearcher (2.33s)
/Users/ ... /search/search_test.go:86: 10
我期望得到的结果是:
--- FAIL: TestSearcher (2.33s)
/Users/ ... /search/search_test.go:86: 35
我该如何访问由该索引存储的所有35个值?
英文:
When I try to make a search query with bleve I always get 10 or less results. How can I increase this limit or make a search between all the data?
Here is code example, where I expect to get 35 results, but getting 10 (ps calc.Rand()
function is giving random bytes):
package search
import (
"testing"
"github.com/blevesearch/bleve/v2"
)
func TestSearcher(t *testing.T) {
mapping := bleve.NewIndexMapping()
searcher, _ := bleve.New("search/src", mapping)
for i := 0; i < 35; i++ {
searcher.Index(string(calc.Rand()), "stuff")
}
query := bleve.NewMatchQuery("stuff")
search := bleve.NewSearchRequest(query)
searchRez, _ := searcher.Search(search)
t.Error(len(searchRez.Hits))
}
Result I get:
--- FAIL: TestSearcher (2.33s)
/Users/ ... /search/search_test.go:86: 10
Result I expect:
--- FAIL: TestSearcher (2.33s)
/Users/ ... /search/search_test.go:86: 35
How do I access all the 35 values, that are stored by this index?
答案1
得分: 1
在SearchRequest
上设置字段Size
:
Size
/From
描述要返回结果集的数量和哪个部分。
search := bleve.NewSearchRequest(query)
search.Size = 35 // 或者你想要的页面大小
searchRez, _ := searcher.Search(search)
fmt.Println(len(searchRez.Hits)) // 输出 35
或者使用分页,根据页面大小逐步设置字段From
。一个假设的示例:
count, _ := searcher.DocCount()
for i := 0; i < int(count); i += 10 /* 页面大小 */ {
search.From = i
searchRez, _ := searcher.Search(search)
fmt.Println(len(searchRez.Hits)) // 输出 10 10 10 5
}
英文:
Set the field Size
on SearchRequest
:
> Size/From describe how much and which part of the result set to return.
search := bleve.NewSearchRequest(query)
search.Size = 35 // or the page size you want
searchRez, _ := searcher.Search(search)
fmt.Println(len(searchRez.Hits)) // prints 35
or with pagination, you set the field From
incrementally based on page size. A contrived example:
count, _ := searcher.DocCount()
for i := 0; i < int(count); i += 10 /* page size */ {
search.From = i
searchRez, _ := searcher.Search(search)
fmt.Println(len(searchRez.Hits)) // prints 10 10 10 5
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论