ElasticSearch初次使用记录

Es版本 7.0

注意:7.0以后es不再支持指定索引

 

Es代码如下:

main.go

package main

import (
   "context"
   "encoding/json"
   "fmt"
   "github.com/olivere/elastic/v7"
)

var client *elastic.Client
var host = "http://127.0.0.1:9200/"

type TweetUser struct {
   User    string `json:"user"`
   Message string `json:"message"`
}

//初始化
func init() {
   var err error
   // 将sniff设置为false后,便不会自动转换地址,默认开启
   client, err = elastic.NewClient(elastic.SetURL(host), elastic.SetSniff(false))
   if err != nil {
      panic(err.Error())
   }
   info, code, err := client.Ping(host).Do(context.Background())
   if err != nil {
      panic(err.Error())
   }
   fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)

   // 获取ES版本信息
   esversion, err := client.ElasticsearchVersion(host)
   if err != nil {
      panic(err.Error())
   }
   fmt.Printf("Elasticsearch version %s\n", esversion)

}

// 创建索引,并映射
func createIndex(indexName string, mapping interface{}, ctx context.Context) {
   // 检查Index是否存在,如果不存在就创建一个
   exists, err := client.IndexExists(indexName).Do(ctx)
   if err != nil {
      panic(err.Error())
   }
   if !exists {
      createIndex, err := client.CreateIndex(indexName).BodyJson(mapping).Do(ctx)
      if err != nil {
         panic(err.Error())
      }
      if !createIndex.Acknowledged { // Acknowledged=true代表成功
         panic(err.Error())
      }
      fmt.Printf("createIndex= %v", createIndex)
   }
}

// 向索引中插入数据(数据格式有多种比如:BodyJson/BodyString)
func createData(index, indexType string, bodyJson interface{}, ctx context.Context) {

   //写入json数据
   // put1, err := client.Index().Index(index).Type(indexType).BodyJson(bodyJson).Do(ctx)  // 7.0之前的用法
   put1, err := client.Index().Index(index).BodyJson(bodyJson).Do(ctx)
   if err != nil {
      panic(err.Error())
   }
   fmt.Printf("Indexed TweetUser %s to index %s, type %s\n, result %s\n", put1.Id, put1.Index, put1.Type, put1.Result)

}

// 获取指定id的数据
func getData(index, indexType, id string, ctx context.Context) {
   var twitterStruct TweetUser
   // 查询数据
   // get1,err:=client.Get().Index(index).Type(indexType).Id(id).Do(ctx)    // 7.0之前的用法
   get1, err := client.Get().Index(index).Id(id).Do(ctx)
   if err != nil {
      panic(err.Error())
   }
   if get1.Found {
      jsonByte, err := get1.Source.MarshalJSON()
      if err != nil {
         panic(err.Error())
      }
      if err = json.Unmarshal(jsonByte, &twitterStruct); err != nil {
         panic(err.Error())
      }
      fmt.Println("id=%s 的数据为:%v =", id, twitterStruct)
      fmt.Printf("Got document %s in version %d from index %s, type %s, fields %s\n", get1.Id, get1.Version, get1.Index, get1.Type, get1.Fields)
   }
}

// 删除索引index
func delIndex(index string, ctx context.Context) {
   deleteIndex, err := client.DeleteIndex(index).Do(ctx)
   if err != nil {
      panic(err)
   }
   if !deleteIndex.Acknowledged { // 说明插入成功
      // Not acknowledged
   }
}

func main() {
  // 执行方法
}

 

main_test.go

package main

import (
   "context"
   "testing"
)
// v7不在支持指定索引,默认为"_doc"
const mapping = `
{
   "settings":{
      "number_of_shards": 1,
      "number_of_replicas": 0
   },
   "mappings":{
         "properties":{
            "user":{
               "type":"keyword"
            },
            "message":{
               "type":"text",
               "store": true,
               "fielddata": true
            }
         }
   }
}`

var index="twitter_es_7x"
var indexType="_doc"

func TestCreateIndex(t *testing.T) {
   createIndex(index,mapping,context.Background())
}

func TestCreateData(t *testing.T) {
   tweet1Json := TweetUser{User: "olivere_2", Message: "Take Five_2"}
   createData(index,indexType,tweet1Json,context.Background())
}

func TestGetData(t *testing.T) {
   id:="dxWUsXIBmE9X-pmeZU0z"
   getData(index,indexType,id,context.Background())
}

func TestDelIndex(t *testing.T) {
   delIndex(index,context.Background())
}

go.mod:

module Exercise/ItemEs7x

go 1.14

require github.com/olivere/elastic/v7 v7.0.17

Kibana:

控制台界面路径:http://localhost:5601/app/kibana#/dev_tools/console

// 获取index=twitter_es_7x下的所有数据
GET twitter_es_7x/_search

POST twitter_es_7x/_doc
{
  "user":"user-test",
  "message":"插入数据,自动生成id"
}

POST twitter_es_7x/_doc/1
{
  "user":"user-test2",
  "message":"插入数据,自定义id"
}

PUT twitter_es_7x/_doc/1
{
  "user":"user-test2",
  "message":"更新id=1的数据"
}

 

 参考:

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/multi-fields.html

https://github.com/olivere/elastic

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Elasticsearch SQL 插件,您需要先确保您已经安装了 Elasticsearch 7.1.1 版本。然后,按照以下步骤进行操作: 1. 安装 Elasticsearch SQL 插件: - 打开终端或命令提示符窗口。 - 运行以下命令安装 Elasticsearch SQL 插件: ``` ./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/7.1.1.0/elasticsearch-sql-7.1.1.0.zip ``` - 安装完成后,重启 Elasticsearch 以使插件生效。 2. 使用 Elasticsearch SQL: - 打开终端或命令提示符窗口。 - 进入 Elasticsearch 的安装目录。 - 运行以下命令以启动 Elasticsearch SQL 查询: ``` ./bin/elasticsearch-sql-cli ``` - 您将进入 Elasticsearch SQL 的命令行界面。 - 现在,您可以开始使用 SQL 语法来查询 Elasticsearch 数据。 例如,您可以运行以下命令来执行一个简单的 SELECT 查询: ``` SELECT * FROM your_index_name WHERE your_field_name = 'your_value' ``` 注意:在上述查询中,`your_index_name` 是您要查询的索引名称,`your_field_name` 是您要查询的字段名称,`your_value` 是您要匹配的字段值。 这样,您就可以使用 Elasticsearch SQL 插件来执行 SQL 查询并检索 Elasticsearch 中的数据了。请记住,Elasticsearch SQL 插件提供了一种方便的方式来使用 SQL 语法进行查询,但它可能不适用于所有类型的查询,特别是复杂的查询。因此,在使用插件,请确保您了解其使用限制和适用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值