golang学习之go连接ElasticSearch

一、ElasticSearch是什么

ElasticSearch(ES)是一个基于Lucene构建的开源、分布式、restful接口全文搜索引擎,一个分布式文档数据库。

二、ElasticSearch的基本概念

ES与MySql概念类比对照

ES MySQL
索引 数据库
类型
文档 记录

三、ElasticSearch可以用来做什么

1、网站内部搜索引擎
2、结合FileBeat、Logstash 、ElasticSearch、Kibana 可以实现日志的搜集、存储、查询

四、ElasticSearch安装部署

1、home路径下新建es-cluster目录

mkdir es-cluster

2、新建docker-compose.yml内容如下

version: '2.2'
services:
  es01:
    image: elasticsearch:7.12.1
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic
  es02:
    image: elasticsearch:7.12.1
    container_name: es02
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - data02:/usr/share/elasticsearch/data
    ports:
      - 9201:9200
    networks:
      - elastic
 es03:
    image: elasticsearch:7.12.1
    container_name: es03
    environment:
      - node.name=es03
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - data03:/usr/share/elasticsearch/data
    networks:
      - elastic
    ports:
      - 9202:9200
volumes:
  data01:
    driver: local
  data02:
    driver: local
  data03:
    driver: local

networks:
  elastic:
    driver: bridge

3、设置虚拟地址空间数量,不然es启动报错
3.1、错误信息:

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

3.2、打开配置文件:

sudo vim /etc/sysctl.conf

3.3、增加如下配置:

vm.max_map_count=262144

3.4、刷新最新配置:

sudo sysctl -p

4、启动es

docker-compose up -d 

5、查看日志

docker logs -f es01

6、停止es集群

docker-compose stop

7、浏览器访问http://localhost:9200/、http://localhost:9201/、http://localhost:9202/

{
    
    
  "name" : "es01",
  "cluster_name" : "es-docker-cluster",
  "cluster_uuid" : "xvagkyFNSXCfty78yDZwUg",
  "version" : {
    
    
    "number" : "7.12.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "3186837139b9c6b6d23c3200870651f10d3343b7",
    "build_date" : "2021-04-20T20:56:39.040728659Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

五、go连接ElasticSearch

1、go es客户端安装

go get github.com/elastic/go-elasticsearch/v7

2、github demo

package main

import (
	"bytes"
	"context"
	"encoding/json"
	"log"
	"strconv"
	"strings"
	"sync"

	"github.com/elastic/go-elasticsearch/v7"
	"github.com/elastic/go-elasticsearch/v7/esapi"
)

func main() {
    
    
	log.SetFlags(0)

	var (
		r  map[string]interface{
    
    }
		wg sync.WaitGroup
	)

	// Initialize a client with the default settings.
	//
	// An `ELASTICSEARCH_URL` environment variable will be used when exported.
	// 配置es地址
	cfg := elasticsearch.Config{
    
    
		Addresses: []string{
    
    "http://localhost:9200", "http://localhost:9201", "http://localhost:9202"},
	}
	es, err := elasticsearch.NewClient(cfg)
	if err != nil {
    
    
		log.Fatalf("Error creating the client: %s", err)
	}

	// 1. Get cluster info
	// 查询集群信息
	res, err := es.Info()
	if err != nil {
    
    
		log.Fatalf("Error getting response: %s", err)
	}
	// Check response status
	if res.IsError() {
    
    
		log.Fatalf("Error: %s", res.String())
	}
	// Deserialize the response into a map.
	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
    
    
		log.Fatalf("Error parsing the response body: %s", err)
	}
	// Print client and server version numbers.
	log.Printf("Client: %s", elasticsearch.Version)
	log.Printf("Server: %s", r["version"].(map[string]interface{
    
    })["number"])
	log.Println(strings.Repeat("~", 37))

	// 2. Index documents concurrently
	// 创建索引
	for i, title := range []string{
    
    "Test One", "Test Two"} {
    
    
		wg.Add(1)

		go func(i int, title string) {
    
    
			defer wg.Done()

			// Build the request body.
			var b strings.Builder
			b.WriteString(`{"title" : "`)
			b.WriteString(title)
			b.WriteString(`"}`)

			// Set up the request object.
			req := esapi.IndexRequest{
    
    
				Index:      "test",
				DocumentID: strconv.Itoa(i + 1),
				Body:       strings.NewReader(b.String()),
				Refresh:    "true",
			}

			// Perform the request with the client.
			res, err := req.Do(context.Background(), es)
			if err != nil {
    
    
				log.Fatalf("Error getting response: %s", err)
			}
			defer res.Body.Close()

			if res.IsError() {
    
    
				log.Printf("[%s] Error indexing document ID=%d", res.Status(), i+1)
			} else {
    
    
				// Deserialize the response into a map.
				var r map[string]interface{
    
    }
				if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
    
    
					log.Printf("Error parsing the response body: %s", err)
				} else {
    
    
					// Print the response status and indexed document version.
					log.Printf("[%s] %s; version=%d", res.Status(), r["result"], int(r["_version"].(float64)))
				}
			}
		}(i, title)
	}
	wg.Wait()

	log.Println(strings.Repeat("-", 37))

	// 3. Search for the indexed documents
	// 查询信息
	// Build the request body.
	var buf bytes.Buffer
	query := map[string]interface{
    
    }{
    
    
		"query": map[string]interface{
    
    }{
    
    
			"match": map[string]interface{
    
    }{
    
    
				"title": "test",
			},
		},
	}
	if err := json.NewEncoder(&buf).Encode(query); err != nil {
    
    
		log.Fatalf("Error encoding query: %s", err)
	}

	// Perform the search request.执行查询
	res, err = es.Search(
		es.Search.WithContext(context.Background()),
		es.Search.WithIndex("test"),
		es.Search.WithBody(&buf),
		es.Search.WithTrackTotalHits(true),
		es.Search.WithPretty(),
	)
	if err != nil {
    
    
		log.Fatalf("Error getting response: %s", err)
	}
	defer res.Body.Close()

	if res.IsError() {
    
    
		var e map[string]interface{
    
    }
		if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
    
    
			log.Fatalf("Error parsing the response body: %s", err)
		} else {
    
    
			// Print the response status and error information.
			log.Fatalf("[%s] %s: %s",
				res.Status(),
				e["error"].(map[string]interface{
    
    })["type"],
				e["error"].(map[string]interface{
    
    })["reason"],
			)
		}
	}

	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
    
    
		log.Fatalf("Error parsing the response body: %s", err)
	}
	// Print the response status, number of results, and request duration.
	log.Printf(
		"[%s] %d hits; took: %dms",
		res.Status(),
		int(r["hits"].(map[string]interface{
    
    })["total"].(map[string]interface{
    
    })["value"].(float64)),
		int(r["took"].(float64)),
	)
	// Print the ID and document source for each hit.
	for _, hit := range r["hits"].(map[string]interface{
    
    })["hits"].([]interface{
    
    }) {
    
    
		log.Printf(" * ID=%s, %s", hit.(map[string]interface{
    
    })["_id"], hit.(map[string]interface{
    
    })["_source"])
	}

	log.Println(strings.Repeat("=", 37))
}

猜你喜欢

转载自blog.csdn.net/weixin_56349119/article/details/127961478
今日推荐