亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在golang的elasticsearch文檔(索引)中搜索字符串?

如何在golang的elasticsearch文檔(索引)中搜索字符串?

Go
郎朗坤 2021-09-21 22:15:51
我正在用 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貢獻1777條經驗 獲得超10個贊

您搜索和查找數據的方式取決于您的分析器 - 從您的代碼來看,很可能正在使用標準分析器(即您沒有在映射中指定替代方案)。

標準分析儀只會檢索完整的單詞。因此,要將“olive”與“olivere”相匹配,您可以:

  1. 更改搜索過程

例如,從術語查詢切換到前綴查詢或使用帶有通配符的查詢字符串查詢。

  1. 更改索引過程

如果您想在較大的字符串中查找字符串,請查看在分析器中使用nGramsEdge nGrams。


查看完整回答
反對 回復 2021-09-21
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

multiQuery := elastic.NewMultiMatchQuery(

    term,

    "name", "address", "location", "email", "phone_number", "place", "postcode",

).Type("phrase_prefix")


查看完整回答
反對 回復 2021-09-21
  • 2 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號