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变更表结构表重写问题

    在实际项目使用数据库的过程中修改字段类型这类需求比较常见. 一.修改表字段类型需要知道: 1.修改表结构可能会导致表进行重写(表OID发生变化). 2.修改表结构带有索引或者字段类型长度或者精度操作时 ...

  2. Scala变量和常量的声明

    标示符的命名规则 1. 字母或下划线开头 2. 以操作符开头,且只包含操作符(+ - * / # !等) 3. 用反引号`....`包括的任意字符串,即使是 Scala 关键字(39 个)也可以• p ...

  3. sqoop安装配置以及简单使用

    一.下载 链接:https://pan.baidu.com/s/1pc7t4e7GyDcZNJHURADE_w 提取码:420s 二.上传安装包到虚拟机的指定路径并解压 tar -zxvf ( 压缩包 ...

  4. Python爬取imdb电影数据并存储到mysql数据库

    数据获取方式:微信搜索关注[靠谱杨阅读人生]回复[电影].整理不易,资源付费,谢谢支持. Python爬虫代码: 1 import re 2 import time 3 import tracebac ...

  5. 访问数据库 与 Java框架各层级

    目录 访问数据库流程 Java框架各层级 4 层 对象的调用流程 耦合性与分层 项目地址:https://github.com/aijisjtu/Bot-Battle 访问数据库流程 flowchar ...

  6. mupdf实用操作demo,C++操作PDF文件

    前文: 最近有个项目,需要读写PDF,本来想着挺简单的,读写PDF有那么多的库可以使用,唰唰的就完成了. 忘记了我写C++的,还是在国产系统上开发的. 所以一般的东西还不好使,因为项目需要在多个架构的 ...

  7. HarmonyOS线性容器特性及使用场景

      线性容器实现能按顺序访问的数据结构,其底层主要通过数组实现,包括ArrayList.Vector.List.LinkedList.Deque.Queue.Stack七种. 线性容器,充分考虑了数据 ...

  8. 实验1产品原型设计-YHealth健康APP

    一.实验题目:原型设计 二.实验目的:掌握产品原型设计方法和相应工具使用. 三.实验要求: (1)对比分析墨刀.Axure.Mockplus等原型设计工具的各自的适用领域及优缺点 --墨刀 适用领域: ...

  9. 详解K8s 镜像缓存管理kube-fledged

    本文分享自华为云社区<K8s 镜像缓存管理 kube-fledged 认知>,作者: 山河已无恙. 我们知道 k8s 上的容器调度需要在调度的节点行拉取当前容器的镜像,在一些特殊场景中, ...

  10. 面向切面编程AOP[三](java AnnotationAwareAspectJAutoProxyCreator实现了什么功能)

    前言 要查看一个类实现了什么功能,那么查看它继承的接口或者class即可知道,那么其到底继承了什么? 正文 AnnotationAwareAspectJAutoProxyCreator extends ...