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 ...
随机推荐
- Python 潮流周刊第 44 期(摘要)+ 赠书 5 本《明解Python算法与数据结构》
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- async/await 与console(C#)
问题: 上一篇async/await 致WPF卡死问题(https://www.cnblogs.com/stephen2023/p/17725159.html),介绍主线程阻塞,async/await ...
- C++设计模式 -中介者模式(Mediator)
接口隔离模式 在组件构建过程中,某些接口之间直接的依赖常常会带来很多问题.甚至根本无法实现.采用添加一层间接(稳定)接口,来隔离本来互相紧密关联的接口是一种常见的解决方案. 典型模式 Facade P ...
- #数学期望,高斯消元#洛谷 3232 [HNOI2013]游走
题目 分析 如果计算出边的期望经过次数那就可以算出来答案 首先转换成点的期望经过次数,设\(dp[x]\)表示点\(x\)的期望经过次数 那么\(dp[x]=\sum_{y\in son}\frac{ ...
- Java基础知识:面试官必问的问题
数据类型 基本类型 byte/8 char/16 short/16 int/32 float/32 long/64 double/64 boolean/~ boolean 只有两个值:true.fal ...
- Python调用动态库,获取BSTR字符串
今天客户在用Python调用我们的动态库的时候,遇到一个问题,调用动态库中的函数,函数返回的是BSTR字符串,但是客户接收到的是一个8位长度的数字. 动态库函数原型:EXTERN_C BSTR ELO ...
- openGauss/MogDB零字节问题处理
openGauss/MogDB 零字节问题处理 问题描述:java 应用端程序调用 GZIP 压缩类对数据进行编码压缩后入库 ,然后从数据库取出进行解压,原来再 mysql 数据库中是正常的,但迁移到 ...
- docker 应用篇————docker-compose[十九]
前言 简单介绍一下docker compose. 正文 首先进行下载一下. sudo curl -L "https://github.com/docker/compose/releases/ ...
- 牛客网-SQL专项训练9
①假设有选课表course_relation(student_id, course_id),其中student_id表示学号,course_id表示课程编号,如果小易现在想获取每个学生所选课程的个数信 ...
- Docker部署Node应用简单实践
简介: 本文将从零至一,介绍如何在云服务器上通过 Docker 容器运行一个简单的Node应用. 前言 本文将从零至一,介绍如何在云服务器上通过 Docker 容器运行一个简单的Node应用.本文假设 ...