golang中的Elasticsearch没有使用olivere/elastic package返回任何结果



我正在尝试在我的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,但得到空响应。 如果我从我的搜索((中删除查询,es列出了所有保存的项目。 我也尝试与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
}
}
}

我认为您应该使用NewMatchQuery,如术语查询文档中所述

避免对文本字段使用术语查询。

默认情况下,Elasticsearch 会更改文本字段的值作为 分析。这可以查找文本字段值的完全匹配项 难。

若要搜索文本字段值,请改用匹配查询。

您没有共享您索引的示例文档以及您要查找的内容,但如下所示的内容应该对您的情况有所帮助

query := NewMatchQuery("title", "Hello there")

希望有帮助。

最新更新