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的更多相关文章

  1. 使用curl命令操作elasticsearch

    使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...

  2. 使用Java客户端操作elasticsearch(二)

    承接上文,使用Java客户端操作elasticsearch,本文主要介绍 常见的配置 和Sniffer(集群探测) 的使用. 常见的配置 前面已介绍过,RestClientBuilder支持同时提供一 ...

  3. java操作elasticsearch实现组合桶聚合

    1.terms分组查询 //分组聚合 @Test public void test40() throws UnknownHostException{ //1.指定es集群 cluster.name 是 ...

  4. java操作elasticsearch实现query String

    1.CommonTersQuery: 指定字段进行模糊查询 //commonTermsQuery @Test public void test35() throws UnknownHostExcept ...

  5. java操作elasticsearch实现聚合查询

    1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...

  6. java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询

    1.前缀查询(prefix) //prefix前缀查询 @Test public void test15() throws UnknownHostException { //1.指定es集群 clus ...

  7. java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)

    1.条件match query查询 //条件查询match query @Test public void test10() throws UnknownHostException { //1.指定e ...

  8. java操作elasticsearch实现查询删除和查询所有

    后期博客本人都只给出代码,具体的说明在代码中也有注释. 1.查询删除 //查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostEx ...

  9. java操作elasticsearch实现批量添加数据(bulk)

    java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...

  10. 学习用Node.js和Elasticsearch构建搜索引擎(3):使用curl命令操作elasticsearch

    使用Elasticsearch不免要提到curl工具,curl是利用URL语法在命令行方式下工作的开源文件传输工具.官网地址:https://curl.haxx.se/ 因为elasticsearch ...

随机推荐

  1. KingbaseES V8R6 集群环境备库不结束旧事务快照将影响主库的vacuum操作

    前言 昨天同事遇到了一个有关vacuum的典型问题. V8R6读写分离集群环境,一主多备. 版本:kingbaseesv008r006c004 问题现象: 主库日常巡检发现日志大量记录: waring ...

  2. Games101 -- 作业3

    说明 本次作业主要是实现对一个obj文件表示的物体利用贴图进行渲染 rasterizer.cpp框架分析 和作业二类似,只不过颜色不再是固定值,而是通过纹理获得 //draw 函数 // Also p ...

  3. FFmpeg开发笔记(十二)Linux环境给FFmpeg集成libopus和libvpx

    ​MP4是最常见的视频封装格式,在<FFmpeg开发实战:从零基础到短视频上线>一书的"1.2.3  自行编译与安装FFmpeg"介绍了如何给FFmpeg集成x264和 ...

  4. SkipList和java中ConcurrentSkipListMap的实现

    目录 简介 SkipList ConcurrentSkipListMap SkipList的实现 concurrent的实现 总结 SkipList和java中ConcurrentSkipListMa ...

  5. OpenHarmony技术挑战课题征集

    OpenHarmony技术挑战课题征集 OpenAtom OpenHarmony(以下简称"OpenHarmony")是由开放原子开源基金会(OpenAtom Foundation ...

  6. js推送网页到扩展屏上

    需求: 电脑上有两个屏幕,想在主屏上的网页中点击一个按钮,副屏就可以显示需要推送过去的网页 实现方法: 本方法使用的是js来实现的,亲测可行,支持火狐,但是不支持谷歌 demo: 主屏网页:1.htm ...

  7. Redis和elasticsearch

    redis -----------NOSQL的对比和劣和应用场景参考好文http://www.redis.cn/articles/20181020003.html --------- -------- ...

  8. 重新整理 .net core 实践篇——— 测试控制器[四十九]

    前言 其实就是官方的例子,只是在此收录整理一下. 正文 测试控制器测试的是什么呢? 测试的是避开筛选器.路由.模型绑定,就是只测试控制器的逻辑,但是不测试器依赖项. 代码部分: https://git ...

  9. Effective Python:简介

    作者:布雷特·斯拉特金 本书的大部分范例代码都遵循Python 3.7版本的语法规范,Python 3.7发布于2018年6月.另外,书里还会给出一些采用Python 3.8语法规范所写的范例,让大家 ...

  10. 百度unit闲聊机器人

    import json import random import requests # client_id 为官网获取的AK, client_secret 为官网获取的SK client_id = & ...