【ElasticSearch】7.x版本的部署和使用

ElasticSearch的简介网上一搜一大堆,这里就不赘述了

ES的简单应用:

一、es的部署,和插件kibana的安装:

去官网下载:https://www.elastic.co/cn/downloads/elasticsearch

最新的是7.x版本,需要jdk1.8及以上才可以

下载完打开这个bat文件即可启动服务

【ElasticSearch】7.x版本的部署和使用_第1张图片

 127.0.0.1:9200  输入网址测试一下

【ElasticSearch】7.x版本的部署和使用_第2张图片

出现这个标志就算成功了

二、kibana插件的使用和安装

安装地址:https://www.elastic.co/cn/downloads/kibana

这里需要选择与es版本一样的kibana,下载好的加载包解压到es文件下即可

【ElasticSearch】7.x版本的部署和使用_第3张图片

 kibana-7.7.1-windows-x86_64/bin下的kibana.bat文件是启动文件

【ElasticSearch】7.x版本的部署和使用_第4张图片

 启动后输入网址   http://localhost:5601 

【ElasticSearch】7.x版本的部署和使用_第5张图片

成功!

我的kibana是汉化的,汉化方法如下:

 打开kibana-7.7.1-windows-x86_64\config文件下的kibana.yml文件,这个是配置文件

输入下面这个指令并保存,然后重启kibana就可以了

i18n.locale: "zh-CN"

【ElasticSearch】7.x版本的部署和使用_第6张图片 

三、接下来是使用es,这里是go的代码

/*7.x*/
const Mapping = `{
	"settings":{
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings":{
			"properties":{
				"user":{
					"type":"keyword"
				},
				"message":{
					"type":"text",
					"store": true,
					"fielddata": true
				}
			}
	}
}`

/*6.x*/
const testMapping = `{
	"settings":{
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings":{
		"_doc":{
			"properties":{
				"user":{
					"type":"keyword"
				},
				"message":{
					"type":"text",
					"store": true,
					"fielddata": true
				}
			}
		}
	}
}`

创建索引的时候,7.x版本和6.x版本是不一样的,7.x版本没有 [_doc]的

根据官方文档,创建索引的操作如下

exists, err := esClient.IndexExists(index).Do(ctx)
	if err != nil {
		log.Error( "ESCreatIndex exists failed!")
		return err
	}

	if !exists {
		createIndex, err := esClient.CreateIndex("test_index").BodyString(Mapping).Do(ctx)
		if err != nil {
			return err
		}

		if !createIndex.Acknowledged {
            //dosomething
		}
    }

put和get数据的操作是:

test1 := testType{
		User:    "test1",
		Message: "hello test1",
	}

	put1, err := esClient.Index().Index("test_index").Id("1").BodyJson(test1).Do(ctx)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}

	get1, err := esClient.Get().
		Index("test_index").
		Id("1").
		Do(ctx)
	if err != nil {
		// Handle error
		panic(err)
	}

	val, err := get1.Source.MarshalJSON()
	if err != nil {
		t.Log(err)
		t.FailNow()
	}
	//打印val

 

你可能感兴趣的:(Golang,elasticsearch,kibana)