我正在用 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”的推文。但是如果我给出“橄榄”,那么搜索就不起作用了。如何搜索属于用户/消息/转推的字符串?有人可以帮我解决这个问题吗?谢谢
2 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
multiQuery := elastic.NewMultiMatchQuery(
term,
"name", "address", "location", "email", "phone_number", "place", "postcode",
).Type("phrase_prefix")
- 2 回答
- 0 关注
- 233 浏览
添加回答
举报
0/150
提交
取消