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 ...
随机推荐
- Makefile 简单学习
一.Makefile 简介 Makefile 是一种常用于编译的脚本语言.它可以更好更方便的管理你的项目的代码编译,节约编译时间(没改动的文件不编译).注意 Makefile 文件命令必须是 Make ...
- CSS浮动---float
一.标准文档流的特性 1.空白折叠 无论多少个空格.换行.tab,都会折叠为一个空格. 2.高矮不齐,底边对齐 3.自动换行,一行放不下就换行写 二.行内元素和块级元素的注意点 1.行内元素不能设置宽 ...
- #Tarjan,贪心#LOJ 3684 「COCI 2022.3」Usmjeravanje
题目传送门 分析 可以发现题目实际上求的是最小强连通分量个数. 并且每个强连通分量必然是由最多两段区间 \(a_l\sim a_r,b_L\sim b_R\) 组成的 只要存在一条路 \(b_R-&g ...
- 【福利活动】深度体验OpenHarmony对接华为云IoT
本文主要介绍基于OpenHarmony 3.0来接入IoTDA,以BearPi-HM_Nano开发板为例,使用huaweicloud_iot_link SDK对接华为云物联网平台的简单流程.文末为 ...
- Visual Studio 2022插件的安装及使用 - 编程手把手系列文章
这次开始写手把手编程系列文章,刚写到C#的Dll程序集类库的博文,就发现需要先介绍Visual Studio 2022的插件的安装及使用,因为在后面编码的时候会用到这些个插件,所以有必要先对这个内容进 ...
- Linux Ubuntu配置国内源
配置国内源 因为众所周知的原因,国外的很多网站在国内是访问不了或者访问极慢的,这其中就包括了Ubuntu的官方源. 所以,想要流畅的使用apt安装应用,就需要配置国内源的镜像. 市面上Ubuntu的国 ...
- mysql交集查询按照时间范围查询myBatis
查询 开始时间 --结束时间 <if test="searchParam.startTime != null and searchParam.endTime != null" ...
- 什么是ip协议二
前言 续前面一章. 正文 看下ip选项: 看一张图: 这个ip选项一般我们不用看,即使你去搞硬件,那么做c++或者c的人会告诉你填啥,按照他们设置即可. 那么ip是如何传输的呢? 先看这张图,这张图的 ...
- 实训篇-JavaScript-陶渊明去没去过桃花源
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- BGE M3-Embedding 模型介绍
BGE M3-Embedding来自BAAI和中国科学技术大学,是BAAI开源的模型.相关论文在https://arxiv.org/abs/2402.03216,论文提出了一种新的embedding模 ...