我正在嘗試在我的 go 應用程序中實現 elasticsearch。我正在使用https://github.com/olivere/elastic庫進行 go,elasticsearch 在 docker 容器中運行。我成功連接到elasticsearch并創建索引,之后我嘗試將數據保存到elasticsearch,也成功了。當我運行查詢時我開始遇到問題我的映射看起來像這樣 "mappings":{ "item":{ "properties":{ "id":{ "type":"integer" }, "title":{ "type":"text" }, "description":{ "type":"text" }, "userid":{ "type":"integer" } } } }我試圖按這樣的標題查詢 es ,但得到空響應。如果我從 Search() 中刪除查詢,則會列出所有已保存的項目。我還嘗試與 newBoolQuery 和 newMatchPhrase 結合使用,它也返回空響應。query := elastic.NewTermQuery("title", "Hello there")searchResult, err := elasticClient.Search(). Index("items"). Query(query). Pretty(true). Do(ctx)if err != nil { return nil, err}return searchResult, nil回復 : { "id": 81, "message": "Search successfull", "data": { "took": 1, "_scroll_id": "", "hits": { "total": 0, "max_score": null, "hits": [] }, "suggest": null, "aggregations": null, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 } }}
1 回答

交互式愛情
TA貢獻1712條經驗 獲得超3個贊
我認為你應該根據你的情況進行選擇,如術語查詢NewMatchQuery
文檔中所述
避免對文本字段使用術語“查詢”。
默認情況下,Elasticsearch 會更改文本字段的值作為分析的一部分。這可能會使查找文本字段值的精確匹配變得困難。
要搜索文本字段值,請改用匹配查詢。
您沒有分享您索引的示例文檔以及您想要查找的內容,但類似下面的內容應該對您的情況有所幫助
query?:=?NewMatchQuery("title",?"Hello?there")
希望有幫助。
- 1 回答
- 0 關注
- 299 瀏覽
添加回答
舉報
0/150
提交
取消