要构造 SearchRequest 结构体,你可以按照以下步骤进行操作:

首先,导入所需的包:

import (
    'github.com/elastic/go-elasticsearch/v7/esapi'
    'github.com/elastic/go-elasticsearch/v7/esutil'
)

然后,创建一个新的 SearchRequest 结构体,并设置所需的字段:

request := esapi.SearchRequest{
    Index: []string{'your_index'},
    Body: esutil.NewJSONReader(map[string]interface{} {
        'query': map[string]interface{} {
            'match_all': map[string]interface{} {},
        },
    }),
}

在上面的例子中,我们设置了 Index 字段,指定要搜索的索引。并且我们使用 esutil.NewJSONReader 函数创建了一个 JSON reader,并在 Body 字段中设置了查询。

你可以根据自己的需求修改 Body 字段中的查询内容。

最后,你可以使用该 SearchRequest 结构体来执行搜索请求。

Go Elasticsearch v7: 使用 esapi 构造 SearchRequest 结构体

原文地址: https://www.cveoy.top/t/topic/p3sI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录