我正在用 golang 编写一个函数来搜索已编入索引的 elasticsearch 文档中的字符串。我正在使用 elasticsearch golang 客户端elastic。例如考虑对象是推文,
type Tweet struct {
User string
Message string
Retweets int
}
搜索功能是
func SearchProject() error{
// Search with a term query
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index("twitter"). // search in index "twitter"
Query(&termQuery). // specify the query
Sort("user", true). // sort by "user" field, ascending
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do() // execute
if err != nil {
// Handle error
panic(err)
return err
}
// searchResult is of type SearchResult and returns hits, suggestions,
// and all kinds of other information from Elasticsearch.
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
// Each is a convenience function that iterates over hits in a search result.
// It makes sure you don't need to check for nil values in the response.
// However, it ignores errors in serialization. If you want full control
// over iterating the hits, see below.
var ttyp Tweet
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
t := item.(Tweet)
fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
}
此搜索正在打印用户“olivere”的推文。但是如果我给出“橄榄”,那么搜索就不起作用了。如何搜索属于用户/消息/转推的字符串?
有人可以帮我解决这个问题吗?谢谢
不负相思意
一只萌萌小番薯
相关分类