gpt4 book ai didi

go - Golang中的Elasticsearch使用Olivere/Elastic包未返回任何结果

转载 作者:行者123 更新时间:2023-12-01 22:08:54 26 4
gpt4 key购买 nike

我正在尝试在我的go应用程序中实现elasticsearch。我正在使用https://github.com/olivere/elastic库运行,elasticsearch在docker容器中运行。

我成功连接到elasticsearch并创建了索引,此后,我尝试将数据保存到也成功的elasticsearch中。我在运行查询时开始遇到问题

我的映射看起来像这样

    "mappings":{
"item":{
"properties":{
"id":{
"type":"integer"
},
"title":{
"type":"text"
},
"description":{
"type":"text"
},
"userid":{
"type":"integer"
}
}
}
}


我正在尝试按这样的标题查询es,并且得到空响应。
如果我从Search()中删除查询,则列出所有已保存的项目。
我还尝试与newBool​​Query和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
}
}
}

最佳答案

我认为您应该按照自己的情况使用NewMatchQuery,如term query的文档中所述

Avoid using the term query for text fields.

By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.

To search text field values, use the match query instead.



您没有共享被索引的示例文档以及您要查找的内容,但是下面的内容对您有帮助
query := NewMatchQuery("title", "Hello there")

希望能有所帮助。

关于go - Golang中的Elasticsearch使用Olivere/Elastic包未返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com