ES+kibana
ES 是一款开源的分布式搜索引擎
下载,解压,修改config/elasticsearch.yml里面根security两个地方的选项为false,运行bin/elasticsearch,测试9200端口
https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
curl -XGET 'http://localhost:9200/' -H 'Content-Type: application/json'
{
"name" : "tddeMacBook-Air.local",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "2uSPQGtXRu-xIIzQVLVcAw",
"version" : {
"number" : "8.4.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "f56126089ca4db89b631901ad7cce0a8e10e2fe5",
"build_date" : "2022-08-19T19:23:42.954591481Z",
"build_snapshot" : false,
"lucene_version" : "9.3.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
这样就说明成功了
概念
es本质上是一个分布式数据库,一台机器上一个实例为一个node
Index:
ES会把数据分词后建立索引,使用反向索引来搜索数据
查看所有索引
curl -X GET 'http://localhost:9200/_cat/indices?v'
Document:
index里面的单条记录被称为文档,多个文档组成一个index,同一个index下的document最好拥有相同构造以保证搜索效率
Type:
Type是对index里面document的虚拟分组
基础操作
https://blog.csdn.net/UbuntuTouch/article/details/98871531
增:索引会自动创建
curl -XPUT 'http://localhost:9200/twitter/_doc/1?pretty' -H 'Content-Type: application/json' -d '
{
"user": "kimchy",
"post_date": "2009-11-15T13:12:00",
"message": "Trying out Elasticsearch, so far so good?"
}'
curl -XPUT 'http://localhost:9200/twitter/_doc/2?pretty' -H 'Content-Type: application/json' -d '
{
"user": "kimchy",
"post_date": "2009-11-15T14:12:12",
"message": "Another tweet, will it be indexed?"
}'
curl -XPUT 'http://localhost:9200/twitter/_doc/3?pretty' -H 'Content-Type: application/json' -d '
{
"user": "elastic",
"post_date": "2010-01-15T01:46:38",
"message": "Building the site, should be kewl"
}'
查
精准:
curl -XGET 'http://localhost:9200/twitter/_doc/1?pretty=true'
{
"_index" : "twitter",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T13:12:00",
"message" : "Trying out Elasticsearch, so far so good?"
}
}
搜索
curl -XGET 'http://localhost:9200/twitter/_search?q=user:kimchy&pretty=true'
{
"took" : 69,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.4700036,
"hits" : [
{
"_index" : "twitter",
"_id" : "1",
"_score" : 0.4700036,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T13:12:00",
"message" : "Trying out Elasticsearch, so far so good?"
}
},
{
"_index" : "twitter",
"_id" : "2",
"_score" : 0.4700036,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "Another tweet, will it be indexed?"
}
}
]
}
}
JSON格式的查询
curl -XGET 'http://localhost:9200/twitter/_search?pretty=true' -H 'Content-Type: application/json' -d '
{
"query" : {
"match" : { "user": "kimchy" }
}
}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.4700036,
"hits" : [
{
"_index" : "twitter",
"_id" : "1",
"_score" : 0.4700036,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T13:12:00",
"message" : "Trying out Elasticsearch, so far so good?"
}
},
{
"_index" : "twitter",
"_id" : "2",
"_score" : 0.4700036,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "Another tweet, will it be indexed?"
}
}
]
}
}
删除
curl -X DELETE 'localhost:9200/index_name'
kibana
kibana是es配套的一个可视化工具,需要下载和es相同版本号的kibana才能连上
下载解压,修改config/kibana.yml,改下连的es的地址,另外有个语言选项可改可不改
bin/kibana启动,访问5601端口,配置索引
/elastic/v7
go get -u github.com/olivere/elastic/v7
这是个第三方库,据说官方库不咋好用。应该下载对应版本的,但是我es下高了....不过好在经过测试,基础的一些功能还是兼容的
package EScli
import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
)
type Twinfo struct {
User string `json:"user"`
Post_date string `json:"post_date"`
Message string `json:"message"`
}
func Connes() {
cli, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"), elastic.SetSniff(false)) // 这里有很多可选项,按需选择
if err != nil {
logrus.Error("Failed to connect ES", err)
}
var msg = Twinfo{User: "zhangsan", Post_date: "2022-08-22T17:12:00", Message: "Vice versa"}
put, err := cli.Index().Index("twitter").BodyJson(msg).Do(context.Background())
if err != nil {
logrus.Error("Failed to inject ES")
}
fmt.Printf("index:%s\ntype:%s\nid:%s\n", put.Index, put.Type, put.Id)
}
评论已关闭