Elasticsearch
搜索引擎
zlkb部署
#### 拉取并启动 docker run -d --name elasticsearch elasticsearch ```bash cat >elasticsearch.yml<<EOF cluster.name: "docker-cluster" network.host: 0.0.0.0 http.cors.enabled: true http.cors.allow-origin: "*" xpack.security.enabled: false # minimum_master_nodes need to be explicitly set when bound on a public IP # set to 1 to allow single node clusters # Details: https://github.com/elastic/elasticsearch/pull/17288 discovery.zen.minimum_master_nodes: 1 EOF ``` #### 复制文件进入容器 docker cp ./elasticsearch.yml b854159cb2dd:/usr/share/elasticsearch/config/elasticsearch.yml #### 构建新的镜像 docker commit -a "add config" -m "dev" a404c6c174a2 es:latest #### 启动新容器 docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" es:latest #### 安装可视化工具 ```bash docker run -p 9100:9100 mobz/elasticsearch-head:5 ``` 安装logstash # 下载 logstash wget https://artifacts.elastic.co/downloads/logstash/logstash-5.4.3.tar.gz # 解压 logstash tar -zxvf logstash-5.4.3.tar.gz ```sybase cat>logstash-5.4.3/client.conf<<EOF input { beats { port => 5044 codec => "json" } } output { elasticsearch { hosts => ["127.0.0.1:9200"] index => "logstash-app-error-%{+YYYY.MM.dd}" } stdout {codec => rubydebug} } EOF>> ``` ####启动 bin/logstash -f client.conf Kibana 的安装与使用 docker run --name kibana -e ELASTICSEARCH_URL=http://127.0.0.1:9200 -p 5601:5601 -d kibana:5.6.9 安装filebeat wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.3-linux-x86_64.tar.gz tar -zxvf filebeat-5.4.3-linux-x86_64.tar.gz mv filebeat-5.4.3-linux-x86_64 filebeat cd filebeat cat>filebeat/client.yml<<EOF filebeat.prospectors: - input_type: log paths: - /var/log/*.log output.logstash: hosts: ["localhost:5044"] EOF>>
部署
####拉取镜像 docker pull docker.elastic.co/elasticsearch/elasticsearch:7.3.0 ####创建网络 docker network create esnet ####启动容器 docker run --name es -p 9200:9200 -p 9300:9300 --network esnet -e "discovery.type=single-node" bdaab402b220 ####安装图形化工具 docker run -p 9800:9800 -d --link es:demo --network esnet -e "discovery.type=single-node" containerize/elastichd
curl测试
###检查是否安装成功 GET {{host}}/_nodes/http?pretty ###服务 GET {{host}}/ ###查看将康状态 GET {{host}}_cat/health?v ### ###查看indices GET {{host}}/_cat/indices?v ### ###创建索引 PUT {{host}}/www ### ###删除索引 DELETE {{host}}/www ###检索所有用户 GET {{host}}/user/person/_search ###按条件检索 GET {{host}}/user/person/4 Content-Type: application/json { "query": { "match": { "name": "sb" } } } ###一次最多但会10条 GET {{host}}/user/person/4 Content-Type: application/json { "query": { "match": { "name": "sb", "size": 2 } } }
go操作
package main import ( "context" "fmt" "github.com/olivere/elastic" "log" "os" "reflect" ) var client *elastic.Client //var host = "http://127.0.0.1:9200/" var host = "http://47.98.55.191:9015/" type Employee struct { FirstName string `json:"first_name"` LastName string `json:"last_name"` Age int `json:"age"` About string `json:"about"` Interests []string `json:"interests"` } //初始化 func init() { errorlog := log.New(os.Stdout, "APP", log.LstdFlags) var err error fmt.Println(host) client, err = elastic.NewClient(elastic.SetErrorLog(errorlog), elastic.SetURL(host), elastic.SetSniff(false)) if err != nil { panic(err) } info, code, err := client.Ping(host).Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number) esversion, err := client.ElasticsearchVersion(host) if err != nil { panic(err) } fmt.Printf("Elasticsearch version %s\n", esversion) } /*下面是简单的CURD*/ //创建 func Create() { //使用结构体 e1 := Employee{"Jane", "Smith", 32, "I like to collect rock albums", []string{"music"}} put1, err := client.Index(). Index("megacorp"). Type("employee"). Id("1"). BodyJson(e1). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type) //使用字符串 e2 := `{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}` put2, err := client.Index(). Index("megacorp"). Type("employee"). Id("2"). BodyJson(e2). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put2.Id, put2.Index, put2.Type) e3 := `{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests":["forestry"]}` put3, err := client.Index(). Index("megacorp"). Type("employee"). Id("3"). BodyJson(e3). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put3.Id, put3.Index, put3.Type) } //删除 func Delete() { res, err := client.Delete().Index("megacorp"). Type("employee"). Id("1"). Do(context.Background()) if err != nil { println(err.Error()) return } fmt.Printf("delete result %s\n", res.Result) } //修改 func Update() { res, err := client.Update(). Index("megacorp"). Type("employee"). Id("2"). Doc(map[string]interface{}{"age": 88}). Do(context.Background()) if err != nil { println(err.Error()) } fmt.Printf("update age %s\n", res.Result) } //查找 func Gets() { //通过id查找 get1, err := client.Get().Index("megacorp").Type("employee").Id("2").Do(context.Background()) if err != nil { panic(err) } fmt.Println(get1) if get1.Found { fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type) } } //搜索 func Query() { var res *elastic.SearchResult var err error //取所有 res, err = client.Search("megacorp").Type("employee").Do(context.Background()) printEmployee(res, err) //字段相等 q := elastic.NewQueryStringQuery("last_name:Smith") res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background()) if err != nil { println(err.Error()) } printEmployee(res, err) //条件查询 //年龄大于30岁的 boolQ := elastic.NewBoolQuery() boolQ.Must(elastic.NewMatchQuery("last_name", "smith")) boolQ.Filter(elastic.NewRangeQuery("age").Gt(30)) res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background()) printEmployee(res, err) //短语搜索 搜索about字段中有 rock climbing matchPhraseQuery := elastic.NewMatchPhraseQuery("about", "rock climbing") res, err = client.Search("megacorp").Type("employee").Query(matchPhraseQuery).Do(context.Background()) printEmployee(res, err) //分析 interests aggs := elastic.NewTermsAggregation().Field("interests") res, err = client.Search("megacorp").Type("employee").Aggregation("all_interests", aggs).Do(context.Background()) printEmployee(res, err) } //简单分页 func list(size, page int) { if size < 0 || page < 1 { fmt.Printf("param error") return } res, err := client.Search("megacorp"). Type("employee"). Size(size). From((page - 1) * size). Do(context.Background()) printEmployee(res, err) } //打印查询到的Employee func printEmployee(res *elastic.SearchResult, err error) { if err != nil { print(err.Error()) return } var typ Employee for _, item := range res.Each(reflect.TypeOf(typ)) { //从搜索结果中取数据的方法 t := item.(Employee) fmt.Printf("%#v\n", t) } } //func main() { // create() // delete() // update() // gets() // query() // list(1, 3) //}
.

浙公网安备 33010602011771号