如何增加(或访问)Go bleve搜索引擎的结果?

huangapple go评论76阅读模式
英文:

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 (
	&quot;testing&quot;

	&quot;github.com/blevesearch/bleve/v2&quot;
)

func TestSearcher(t *testing.T) {
	mapping := bleve.NewIndexMapping()
	searcher, _ := bleve.New(&quot;search/src&quot;, mapping)
	for i := 0; i &lt; 35; i++ {
		searcher.Index(string(calc.Rand()), &quot;stuff&quot;)
	}

	query := bleve.NewMatchQuery(&quot;stuff&quot;)
	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 &lt; int(count); i += 10 /* page size */ {
        search.From = i
        searchRez, _ := searcher.Search(search)
        fmt.Println(len(searchRez.Hits)) // prints 10 10 10 5
    }

huangapple
  • 本文由 发表于 2021年9月21日 01:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/69258361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定