go 操作elasticsearch
olivere/elastic 包
github.com/olivere/elastic
doc:
https://pkg.go.dev/github.com/olivere/elastic?utm_source=godoc
初使货es
package elasticsearch
import (
"github.com/olivere/elastic"
"go-admin/common/log"
"go-admin/tools"
)
var host = "http://127.0.0.1:9200"
var ElaticClent *elastic.Client
//基本参考:
//https://blog.csdn.net/chen_peng7/article/details/90700910
// https://blog.csdn.net/Xiao_W_u/article/details/118908282
//批量操作参考:https://zhuanlan.zhihu.com/p/238206335
func InitElasticClient() (err error) {
ElaticClent, err = elastic.NewClient(
elastic.SetURL(host),
elastic.SetSniff(false), // SetSniff启用或禁用嗅探器(默认情况下启用)。
elastic.SetBasicAuth("Username", "Password"), // 账号密码
)
if err != nil {
log.Fatal(tools.Red("elasticsearch connect failed !"), err)
return err
}
//do, i, _ := ElaticClent.Ping(host).Do(context.Background())
//fmt.Println("do:",do)
//fmt.Println("i:",i)
version, _ := ElaticClent.ElasticsearchVersion(host)
//http://172.27.0.2:9200 [dead=false,failures=0,deadSince=<nil>] <nil>
log.Info(tools.Green("elasticsearch connect success ! version:"+version))
return nil
}
package main
import (
"context"
"fmt"
"github.com/olivere/elastic"
)
创建索引:
func main(){
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
fmt.Println(Client, err)
name := "people2"
Client.CreateIndex(name).Do(context.Background())
}
插入数据
func main(){
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
fmt.Println(Client, err)
name := "people2"
data := `{
"name": "wali",
"country": "Chian",
"age": 30,
"date": "1987-03-07"
}`
_, err = Client.Index().Index(name).Type("man1").Id("1").BodyJson(data).Do(context.Background())
}
查找数据: //通过id查找
func main(){
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
fmt.Println(Client, err)
name := "people2"
get, err := Client.Get().Index(name).Type("man1").Id("1").Do(context.Background())
fmt.Println(get, err)
source, _ := get.Source.MarshalJSON()
fmt.Printf("id:%s Source:%s \n", get.Id, string(source))
}
组合查询:
func main(){
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
fmt.Println(Client, err)
query := elastic.NewBoolQuery()
ip := "106.75.96.205"
port := "8082"
app := "NGINX"
if len(ip) > 0 {
query = query.Must(elastic.NewTermQuery("ip.ip_raw", ip))
}
if len(port) > 0 {
query = query.Must(elastic.NewTermQuery("port.port_raw", port))
}
if len(app) > 0 {
query = query.Must(elastic.NewTermQuery("product.raw", app))
}
source, _ := query.Source()
marshalSource, _ := json.Marshal(source)
fmt.Println("source:--->", string(marshalSource)) //打印query语句
pageIndex:= 1
pageSize:= 10
fmt.Println("pageIndex", pageIndex) //打印query语句
sr, err := Client.Search("fofapro_subdomain", "fofapro_service").
Query(query).
From((pageIndex - 1) * pageSize).
Size(pageSize).
Do(context.Background())
fmt.Printf("err:%#v , AssetManagementSearch22----->:%#v \n", err, sr)
if err != nil {
return err
}
fmt.Printf("sr.Hits.Hits:%#v \n", sr.Hits.Hits)
countTmp := sr.TotalHits()
fmt.Printf("countTmp:%#v \n", countTmp)
count = &countTmp
var resjson string
for _, item := range sr.Hits.Hits {
b, _ := item.Source.MarshalJSON()
fmt.Println("b:--->", string(b))
resjson += string(b) + "\r\n"
}
}
//修改
func main() {
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
res, err := client.Update().
Index("megacorp").
Type("employee").
Id("2").
Doc(map[string]interface{}{"age": 88}).
Do(context.Background())
if err != nil {
println(err.Error())
}
fmt.Printf("update age %s\n", res.Result)
}
删除数据
func main(){
Client, err := elastic.NewClient(elastic.SetURL("http://192.168.7.6:9200"))
//使用结构体
e1 := Employee{"Jane", "Smith", 32, "I like to collect rock albums", []string{"music"}}
//创建
put1, err := client.Index().
Index("megacorp").
Type("employee").
Id("1").
BodyJson(e1).
Do(context.Background())
if err != nil {
panic(err)
}
//删除
get, err := Client.Get().Index("megacorp").Type("employee").Id("1").Do(context.Background())
fmt.Println(get, err)
}
参考:
https://www.cnblogs.com/-wenli/p/12737196.html
官方包
参考:
https://blog.csdn.net/Xiao_W_u/article/details/118908282
https://www.cnblogs.com/gwyy/p/13356345.html
https://github.com/elastic/go-elasticsearch 来连接ES并进行操作。
https://www.cnblogs.com/binHome/p/12345413.html
go 操作elasticsearch的更多相关文章
- 使用curl命令操作elasticsearch
使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...
- 使用Java客户端操作elasticsearch(二)
承接上文,使用Java客户端操作elasticsearch,本文主要介绍 常见的配置 和Sniffer(集群探测) 的使用. 常见的配置 前面已介绍过,RestClientBuilder支持同时提供一 ...
- java操作elasticsearch实现组合桶聚合
1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...
- java操作elasticsearch实现query String
1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...
- java操作elasticsearch实现聚合查询
1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...
- java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...
- java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)
1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...
- java操作elasticsearch实现查询删除和查询所有
后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...
- java操作elasticsearch实现批量添加数据(bulk)
java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...
- 学习用Node.js和Elasticsearch构建搜索引擎(3):使用curl命令操作elasticsearch
使用Elasticsearch不免要提到curl工具,curl是利用URL语法在命令行方式下工作的开源文件传输工具.官网地址:https://curl.haxx.se/ 因为elasticsearch ...
随机推荐
- KingbaseES V8R6 集群运维案例--备库timeline not contain minimum recovery point故障
案例现象: KingbaseES V8R6集群备库启动后,加入集群失败,sys_log日志信息提示,如下图所示: 适用版本: kingbaseES V8R6 一.问题分析 在timeline对应的 ...
- 在Ubuntu上安装MySQL
在Ubuntu上安装MySQL sudo apt update sudo apt install mysql-server 安装完成后,MySQL服务将自动启动.要验证MySQL服务器正在运行,请输入 ...
- DM数据库金融行业案例(水贴一波)
最近没遇到啥有意思的案例,都是些很简单的案例,但是又好久没写过博客了,决定水一波帖子,保持更新. 今天这个是任总老婆小王同学提供的金融SQL案例,难是不难,但是远程的时候网络卡得要命, 心累. 慢 ...
- WPF动画教程(PointAnimationUsingPath的使用)
PointAnimationUsingPath的介绍 PointAnimationUsingPath 是 WPF 中的一个类,它用于创建一个动画,该动画会沿着指定的路径移动一个点. 关于 PointA ...
- 动态规划(四)——区间dp
区间dp: 就是对于区间的一种动态规划,对于某个区间,它的合并方式可能有很多种,我们需要去枚举所有的方式,通常是去枚举区间的分割点,找到最优的方式(一般是找最少消耗). 通常都是先枚举区间长度,区间长 ...
- 重新点亮linux 基本软件————防火墙[一]
前言 简单介绍一下linux的防火墙. 正文 防火墙分类: 软件防火墙和硬件防火墙 包过:过滤防火墙和应用层防火墙 iptables 的表和链 规则表: filter nat mangle raw f ...
- 实训篇-Html-表单练习
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 通过UI自动化方式获取文章、视频信息
出于学习研究,对某账号的文章.视频分析一翻,尝试使用自动化方式看能否获取相应信息. 获取某号的文章有多重方法: 第一种是通过搜狗浏览器搜索账号(这种方式每天只能获取一篇文章,基本上没啥用.): 第二种 ...
- 基于locust全链路压测系统
2021年中旬就计划着搭建一套压测系统,大约9月份已经搭建完成,使用至今还是比较稳定了,分享一下搭建思路及过程: 为什么选择Locust呢,因为Locust可以仅需要执行命令就可以完成压测任务,并且集 ...
- huggingface vit训练CIFAR10数据集代码 ,可以改dataset训练自己的数据
上代码,使用hugging face fineturn vit模型 自己写的代码 from transformers import ViTImageProcessor, ViTForImageClas ...