go-elasticsearch
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了go-elasticsearch,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2993字,纯文字阅读大概需要5分钟。
内容图文
![go-elasticsearch](/upload/InfoBanner/zyjiaocheng/1001/9ddf080c5a0042b79150a0c39d6aae1f.jpg)
go 操作 elasticsearch官方包:https://github.com/elastic/go-elasticsearch/
1、安装go-elasticsearch
go get github.com/elastic/go-elasticsearch/v6
2、es工具类,返回一个es连接
package util
import (
"github.com/elastic/go-elasticsearch/v6"
"log"
)
func Esutil() *elasticsearch.Client {
addresses := []string{"http://127.0.0.1:9200"}
config := elasticsearch.Config{
Addresses: addresses,
Username: "",
Password: "",
CloudID: "",
APIKey: "",
}
es, err := elasticsearch.NewClient(config)
log.SetFlags(0)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
return es
}
3、导入util并使用go-elasticsearch
以go-kit为例,如果在service层访问es。
1、导入项目名/util模块,然后定义一个全局变量作为es连接客户端
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"search/util"
"strconv"
)
var es = util.Esutil()
2、构造query
在mysearch函数里实现一个查询:需要status=1,并且商品价格在指定区间;同时物品标题或者内容可以匹配到给定的keyword。得到满足查询条件的商品id切片。
func mysearch(page int,rows int,keyword string, min_price int,max_price int)(result []int){
all_result :=make([]int,0)
log.SetFlags(0)
var (
r map[string]interface{}
)
var buf bytes.Buffer
query := map[string]interface{}{
"_source": "item_id", // 返回物品id
"size": rows, // 每页返回数量
"from": page, // 从第几页开始
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{ // 如果must中包含多个独立条件,则使用[]map[string]interface{}
{
"term": map[string]interface{}{ // term类型的条件放到一个map里面。
"status": 1,
},
},
{
"range":map[string]interface{}{
"item_price":map[string]interface{}{ //同理,range类型的条件放到一个map里面
"gte": min_price,
"lte": max_price,
},
},
},
},
"should": []map[string]interface{}{ //同理,如果should有多个独立条件,使用[]map[string]interface{}
{
"match": map[string]interface{}{
"item_title": map[string]interface{}{
"query": keyword,
"minimum_should_match": "90%",
},
},
},
{
"match": map[string]interface{}{
"item_content": map[string]interface{}{
"query": keyword,
"minimum_should_match": "90%",
},
},
},
},
"minimum_should_match": 1, //代表当must匹配完成之后,还需要至少匹配一个should里面的条件。
},
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil{
log.Fatalf("Error encoding query: %s", err)
}
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex("es表名"),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close() // 最后关闭Body
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
//log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"])
b,error := strconv.Atoi(hit.(map[string]interface{})["_id"].(string))
if error != nil{
fmt.Println("字符串转换成整数失败")
}
all_result = append(all_result,b)
}
return all_result
}
内容总结
以上是互联网集市为您收集整理的go-elasticsearch全部内容,希望文章能够帮你解决go-elasticsearch所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。