为了账号安全,请及时绑定邮箱和手机立即绑定

golang 中的 Elasticsearch 使用 olivere/elastic

golang 中的 Elasticsearch 使用 olivere/elastic

Go
千巷猫影 2023-07-31 15:35:49
我正在尝试在我的 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")

希望有帮助。


查看完整回答
反对 回复 2023-07-31
  • 1 回答
  • 0 关注
  • 173 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信