范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文

go操作elasticsearch示例

  这里我使用elasticsearch官方给的go语言包(go-elasticsearch)
  go-elasticsearch向前兼容,这意味着客户端支持与更大或同等次要版本的 Elasticsearch 通信。Elasticsearch 语言客户端仅向后兼容默认发行版,不提供任何保证。 包:https://github.com/elastic/go... Elasticsearch 权威指南:https://www.elastic.co/guide/... 环境介绍:版本 Elasticsearch:v7.15  安装
  go.mod 文件中添加 require github.com/elastic/go-elasticsearch/v8 main
  或者 git clone --branch main https://github.com/elastic/go-elasticsearch.git $GOPATH/src/github.com/elastic/go-elasticsearch示例:
  新建 es.go 存入 es目录 package es  import (     "bytes"     "context"     "encoding/json"     "fmt"     "github.com/elastic/go-elasticsearch/v8"     "github.com/elastic/go-elasticsearch/v8/esapi"     "log"     "net/http" )  var EsClient *elasticsearch.Client  func init() {      cfg := elasticsearch.Config{          Addresses: []string{             "http://localhost:9200",             },     }     var err error     EsClient, err = elasticsearch.NewClient(cfg)     if err != nil {         log.Fatalln("Failed to connect to es")     } } func failOnError(err error, msg string) {     if err != nil {         log.Fatalf("%s: %s", msg, err)     } }  // idx 为空,默认随机唯一字符串 func Index(index, idx string, doc map[string]interface{}) {     //index:="my_index_name_v1"     res, err := EsClient.Info()     fmt.Println(res, err)     if err != nil {         log.Fatalf("Error getting response: %s", err)     }     var buf bytes.Buffer     //doc := map[string]interface{}{     //    "title":   "中国",     //    "content": "中国早日统一台湾",     //    "time":    time.Now().Unix(),     //    "date":    time.Now(),     //}     if err = json.NewEncoder(&buf).Encode(doc); err != nil {         fmt.Println(err, "Error encoding doc")         return     }     res, err = EsClient.Index(         index,                              // Index name         &buf,                               // Document body         EsClient.Index.WithDocumentID(idx), // Document ID         // Document ID         EsClient.Index.WithRefresh("true"), // Refresh     )     //res, err = EsClient.Create(index, idx, &buf)     if err != nil {         fmt.Println(err, "Error create response")     }     defer res.Body.Close()     fmt.Println(res.String())     log.Println(res) }  //struct 类型允许使用更实际的方法,您可以在其中创建一个新结构,将请求配置作为字段,并使用上下文和客户端作为参数调用 Do() 方法: func IndexEspi(index, idx string, doc map[string]interface{}) {     //index:="my_index_name_v1"     res, err := EsClient.Info()     fmt.Println(res, err)     if err != nil {         log.Fatalf("Error getting response: %s", err)     }     var buf bytes.Buffer     //doc := map[string]interface{}{     //    "title":   "中国",     //    "content": "中国早日统一台湾",     //    "time":    time.Now().Unix(),     //    "date":    time.Now(),     //}     if err = json.NewEncoder(&buf).Encode(doc); err != nil {         fmt.Println(err, "Error encoding doc")         return     }      req := esapi.IndexRequest{         Index:      index,  // Index name         Body:       &buf,   // Document body         DocumentID: idx,    // Document ID         Refresh:    "true", // Refresh     }      res, err = req.Do(context.Background(), EsClient)     if err != nil {         log.Fatalf("Error getting response: %s", err)     }     defer res.Body.Close()     fmt.Println(res.String())     log.Println(res) } func Search(index string, query map[string]interface{}) {     res, err := EsClient.Info()     if err != nil {         fmt.Println(err, "Error getting response")     }     //fmt.Println(res.String())     // search - highlight     var buf bytes.Buffer     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "title": title,     //        },     //    },     //    "highlight": map[string]interface{}{     //        "pre_tags":  []string{""},     //        "post_tags": []string{""},     //        "fields": map[string]interface{}{     //            "title": map[string]interface{}{},     //        },     //    },     //}     if err := json.NewEncoder(&buf).Encode(query); err != nil {         fmt.Println(err, "Error encoding query")     }     // Perform the search request.     res, err = EsClient.Search(         EsClient.Search.WithContext(context.Background()),         EsClient.Search.WithIndex(index),         EsClient.Search.WithBody(&buf),         EsClient.Search.WithTrackTotalHits(true),         EsClient.Search.WithFrom(0),         EsClient.Search.WithSize(10),         EsClient.Search.WithSort("time:desc"),         EsClient.Search.WithPretty(),     )     if err != nil {         fmt.Println(err, "Error getting response")     }     defer res.Body.Close()     fmt.Println(res.String()) }  //删除 index 根据 索引名 id  func Delete(index, idx string) {     //index:="my_index_name_v1"     res, err := EsClient.Info()     fmt.Println(res, err)     if err != nil {         log.Fatalf("Error getting response: %s", err)     }     res, err = EsClient.Delete(         index, // Index name         idx,   // Document ID         EsClient.Delete.WithRefresh("true"),     )     if err != nil {         fmt.Println(err, "Error create response")     }     defer res.Body.Close()     fmt.Println(res.String())     log.Println(res) } func DeleteByQuery(index []string, query map[string]interface{}) {     res, err := EsClient.Info()     if err != nil {         fmt.Println(err, "Error getting response")     }     //fmt.Println(res.String())     // search - highlight     var buf bytes.Buffer     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "title": title,     //        },     //    },     //    },     //}     if err := json.NewEncoder(&buf).Encode(query); err != nil {         fmt.Println(err, "Error encoding query")     }     // Perform the search request.     res, err = EsClient.DeleteByQuery(         index,         &buf,     )     if err != nil {         fmt.Println(err, "Error getting response")     }     defer res.Body.Close()     fmt.Println(res.String())  } func SearchEsapiSql(query map[string]interface{}) {     jsonBody, _ := json.Marshal(query)     req := esapi.SQLQueryRequest{Body: bytes.NewReader(jsonBody)}     res, _ := req.Do(context.Background(), EsClient)     defer res.Body.Close()     fmt.Println(res.String()) } func SearchHttp(method, url string, query map[string]interface{}) {     jsonBody, _ := json.Marshal(query)     req, _ := http.NewRequest(method, url, bytes.NewReader(jsonBody))     req.Header.Add("Content-type", "application/json")     res, err := EsClient.Perform(req)     if err != nil {         return     }     defer res.Body.Close()     buf := new(bytes.Buffer)     buf.ReadFrom(res.Body)     fmt.Println(buf.String()) }
  新建 main.go package main import "demo/es" func main() {     index := "my_index_name_v4"     //创建索引并设置映射     //query := map[string]interface{}{     //    "mappings": map[string]interface{}{     //        "properties": map[string]interface{}{     //            "title": map[string]interface{}{     //                "type": "text",     //            },     //            "content": map[string]interface{}{     //                "type": "text",     //            },     //            "location": map[string]interface{}{     //                "type": "geo_point",     //            },     //            "time": map[string]interface{}{     //                "type": "long",     //            },     //            "date": map[string]interface{}{     //                "type": "date",     //            },     //            "age": map[string]interface{}{     //                "type": "keyword",     //            },     //        },     //    },     //}     //url := index     //注意 映射信息不能更新     //es.SearchHttp("PUT", url, query)      //添加或修改文档,没有索引创建     //doc := map[string]interface{}{     //    "title":    "你好",     //    "content":  "中国美丽的城市",     //    "location": "41.015, -75.011",     //    "time":     time.Now().Unix(),     //    "date":     time.Now(),     //    "age":      20,     //}      //es.Index(index, "", doc)     //es.IndexEspi(index, "idx5", doc)     //删除索引     //es.Delete(index, "idx3")     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "title": "vvvvv我爱你!!!!",     //        },     //    },     //}     //indexArr := []string{index}     //es.DeleteByQuery(indexArr, query)     ////搜索单个字段     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "title": "我爱你中国",     //        },     //    },     //    "highlight": map[string]interface{}{     //        "pre_tags":  []string{""},     //        "post_tags": []string{""},     //        "fields": map[string]interface{}{     //            "title": map[string]interface{}{},     //        },     //    },     //}      //搜索多个字段     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "multi_match": map[string]interface{}{     //            "query":  "中国",     //            "fields": []string{"title", "content"},     //        },     //    },     //    "highlight": map[string]interface{}{     //        "pre_tags":  []string{""},     //        "post_tags": []string{""},     //        "fields": map[string]interface{}{     //            "title": map[string]interface{}{},     //        },     //    },     //}     //提高某个字段权重,可以使用 ^ 字符语法为单个字段提升权重,在字段名称的末尾添加 ^boost ,其中 boost 是一个浮点数:     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "multi_match": map[string]interface{}{     //            "query":  "中国",     //            "fields": []string{"title", "content^2"},     //        },     //    },     //    "highlight": map[string]interface{}{     //        "pre_tags":  []string{""},     //        "post_tags": []string{""},     //        "fields": map[string]interface{}{     //            "title": map[string]interface{}{},     //        },     //    },     //}      //显示所有的     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match_all": map[string]interface{}{},     //    },     //}     //es.Search(index, query)     //地理距离过滤器( geo_distance )以给定位置为圆心画一个圆,来找出那些地理坐标落在其中的文档:     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "bool": map[string]interface{}{     //            "must": map[string]interface{}{     //                "match_all": map[string]interface{}{}, // 这里不设置其他查询条件,所以匹配全部文档     //            },     //            "filter": map[string]interface{}{     //                "geo_distance": map[string]interface{}{     //                    "distance": "100km",     //                    "location": map[string]interface{}{     //                        "lat": 40.715,     //                        "lon": -73.988,     //                    },     //                }},     //        },     //    },     //    "sort": map[string]interface{}{ // 设置排序条件     //        "_geo_distance": map[string]interface{}{ //_geo_distance代表根据距离排序     //            "location": map[string]interface{}{ //根据location存储的经纬度计算距离。     //                "lat": 40.715,  //当前纬度     //                "lon": -73.988, //当前经度     //            },     //            "order": "asc", // asc 表示升序,desc 表示降序     //        }},     //}     //es.Search(index, query)     //精确值查询     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "age": "20",     //        },     //    },      //}     //es.Search(index, query)     //范围查询     //通过range实现范围查询,类似SQL语句中的>, >=, <, <=表达式。     //gte范围参数 - 等价于>=     //lte范围参数 - 等价于 <=     //范围参数可以只写一个,例如:仅保留 "gte": 10, 则代表 FIELD字段 >= 10     //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "range": map[string]interface{}{     //            "age": map[string]interface{}{     //                "gte": "19",     //                "lte": "20",     //            },     //        },     //    },     //}     //es.Search(index, query)     //组合查询 如果需要编写类似SQL的Where语句,组合多个字段的查询条件,可以使用bool语句。     //"must": [], // must条件,类似SQL中的and, 代表必须匹配条件     //"must_not": [], // must_not条件,跟must相反,必须不匹配条件     //"should": [] // should条件,类似SQL中or, 代表匹配其中一个条件     query := map[string]interface{}{         "query": map[string]interface{}{             "bool": map[string]interface{}{                 "must": []map[string]interface{}{                     {                         "match": map[string]interface{}{                             "age": "19",                         },                     },                     {                         "match": map[string]interface{}{                             "title": "中国",                         },                     },                 },             },         },     }     es.Search(index, query)      //使用mysql的方式来请求     //query := map[string]interface{}{     //    "query": "select title from " + index + " where title like "%中国%"", //这里使用mysql的方式来请求,非常简单,符合开发习惯,简化es入门门槛,支持order,支持Limit,那么排序和分页就自己写好了     //}     //query := map[string]interface{}{     //    "query": "select title from " + index + " where title = "中国"", //这里使用mysql的方式来请求,非常简单,符合开发习惯,简化es入门门槛,支持order,支持Limit,那么排序和分页就自己写好了     //}     //es.SearchEsapiSql(query)      //测试分词     //query := map[string]interface{}{     //    "analyzer": "ik_smart", //智能分词用:ik_smart,最大化分词用:ik_max_word     //    "text":     "中国华人民",     //}     //url := index + "_analyze?pretty=true"      //query := map[string]interface{}{     //    "query": map[string]interface{}{     //        "match": map[string]interface{}{     //            "title": "我爱你中国",     //        },     //    },     //    "highlight": map[string]interface{}{     //        "pre_tags":  []string{""},     //        "post_tags": []string{""},     //        "fields": map[string]interface{}{     //            "title": map[string]interface{}{},     //        },     //    },     //}     //url := index + "/_search"      //es.SearchHttp("GET", url, query)  } 参考资料
  https://github.com/elastic/go...
  https://www.tizi365.com/archi...
  https://www.elastic.co/guide/... links目录

2022款北京奔驰GLC上市减配现象严重,顶配车型降价2。25万元日前,据梅赛德斯奔驰官方宣布,2022款北京奔驰GLC正式上市,新车售价区间为39。98万47。53万元。作为年度改款车型,新车由此前的5款车型调整为4款,配置及售价都进行了调整。皇冠陆放对比本田URV谁才是大空间SUV的首选?对于消费者来说,一款大空间SUV绝对是他们的首选。而在30万售价区间内,日系品牌凭借着出色的性价比占据了相当一部分市场。今天,我们带来了皇冠陆放和本田URV两款车型,谁更值得消费者为什么150kHz定位,距离越远接收信号越强呢?简介本文回复了参加智能车竞赛的队伍在使用150kHz电磁导航过程中遇到的奇怪的现象。通过这些分析可以看到电磁导航的作用的发挥适合将这项比赛引申到室外环境。关键词150kHz,电磁导车模足底按摩简介本文就在全国大学生智能车竞赛中同学们对于参赛车模的轮胎进行处理的方法进行总结。这些方法也凝聚了同学们在探索车模在快速赛道运行中轮胎因素的影响。最后针对咸鱼网络中有专门销售打磨后让我们一起来内卷这两天,收到了一些正在突飞猛进调试节能信标组1赛车同学们在公众号里的留言。就像在博文有的人撑死,有的人饿死,有的人吓死2里的一样,有的认为题目太难,有的认为题目太容易。很容易让人联目前电视盒子哪个最好?小米天猫泰捷全面battle谁最强目前电视盒子哪个最好?相信大家在选购电视盒子的时候都会有这样的疑问。而猎豹科技数码评测室在圈内混了将近十年,评测过多款数码产品,电视机顶盒也深度体验过近百款。一直以来,猎豹科技都有2021网络电视盒子哪个好?十年老烧明哥只推荐这5款网络电视盒子作为家用的数码产品,在使用频率以及热度上虽然比不上手机耳机这些产品,但近几年的需求量也在急剧上升。特别是随着智能电视机迎来更新换代的当下,越来越多人选择了搭配网络电视盒电视盒子什么牌子好?华少360度评测泰捷新品WE60C升级版平时就很关注数码圈,正好暑假来了,很多人都问华少电视盒子什么牌子好,刚好最近泰捷新品WE60C升级版电视盒子的风很大,很多达人大V都评测过,今天华少也要来好好测一测,我打算直接测试亮哥玩机电视盒子哪款好?泰捷WE60C电视盒子体验报告电视盒子对于普通的工薪阶层来说,应该是一个每天看电视的必备数码产品,不过电视盒子的品牌太多,对新手来说选购难度很大,所以还是很值得进行深度剖析的。亮哥从15年就开始各种评测,专注于售价7。5811。98万,配沃尔沃1。5T三缸发动机,新缤越还是小钢炮吗吉利汽车近些年来的上升速度惊人,收购了沃尔沃之后,又将马来西亚国宝级品牌宝腾汇入旗下,一系列操作下来,吉利已经成为中国自主的头部车企之一。吉利的成功不仅在于此,在其吉利精品车战略的30英寸大环屏,五门识别的云钥匙,名爵ONE能否成为领头羊?上汽名爵这些年在SUV领域并没有质的突破,推出的车型不温不火,反而像MG5MG6等车型在运动轿车领域崭露头角,名爵急需打造爆款SUV来打破僵局,于是就诞生了MGONE。MGONE作
网龙(00777。HK)收入和经营利润再创新高,连续五年增长2022年3月29日,全球领先的互联网社区创建者网龙网络控股有限公司(网龙或公司香港交易所股份代号777)今天公布2021年全年财务业绩。网龙管理层将于2022年3月30日香港时间集邦咨询预估至2025年ARM架构服务器渗透率达22,云端数据中心将率先采用据集邦咨询研究显示,近年企业对于人工智能高效能运算等数字转型需求加速,带动云端采用比例增加,全球主要云端服务业者为提升服务弹性,陆续导入ARM架构服务器,预期至2025年ARM架构裁员热潮到来该怎么办?ldsd最近有很多互联网大厂在裁员。这也是没办法的事情,现在各地的疫情还是反反复复,让人闹心。在严重的情况下就会封城,比如深圳之前的停工一星期,相继还有上海。在这种情况下,大厂都不基于springbootspringcloud的集成大屏权限管理的后台框架真正的大师,永远都怀着一颗学徒的心!开源的框架有很多,到底用哪一种有很多公司很苦恼。其实,适合的才是最好的。如果没有性能上的项目要求,一般的单体项目就能满足80的需求。今天给大家推python三个重要的内置函数(map,filter,reduce)map函数map(function,iterable,)map函数第一个参数是一个函数function,第二个参数是一个可迭代的对象iterable,他的功能是将可迭代对象iter逃离互联网的一次尝试卸载常用APP当我没有关注互联网时,我的吸引力就指向了那些散落一地的兴趣爱好。它们已经在那很久了,现在突然冒出来,就像蒙了灰后一下子擦亮。24岁的孟褚荔在远离互联网的过程中,重拾爱好。在豆瓣反技阿维塔科技完成首轮融资交割宁德时代成第二大股东上证报中国证券网讯(记者俞立严)阿维塔科技于3月29日宣布,完成首轮战略融资交割和工商信息变更。值得注意的是,经过本轮变更,阿维塔科技的注册资本由28800万元增厚至117224。439只基金年报透露基金经理今年偏好新能源军工被划重点继中邮基金率先拉开公募基金2021年年报披露大幕后,截至3月28日16时,已有至少12家基金管理人公布旗下基金战绩,绩优基金经理后续投资策略也浮出水面。另有1家基金管理人宣布推迟旗台积电刘德音算错了,失去华为订单,损失的是台积电自己台积电作为全球最大的芯片代工厂,以一己之力,占据了全球芯片代工领域50以上的产能,在其他领域,这是不敢想象的市场份额。台积电的客户遍布全球,几乎只要能排得上名号的头部科技企业,都直威马W6续航520KM花费36元随着国际油价突破9元大关。很多想要购车的消费者开始犹豫,加满一次油就要花费400多,一年下来将近两万块的油费,买车容易供车难。威马新能源汽车就可以完美解决这个问题,以威马W6为例,京东快递不再快了很少在淘宝买东西,基本都是在京东买,特别是急用的东西,更看中了京东的当日或者次日达的效率。但是今年以来,京东物流不再给力了。下单时提示下午或者第二天可以到达,付款后到达时间会变化,