前言
为什么要写这个系列? 因为目前实习的项目涉及到ES的CRUD,所以写一个记录贴,为以后的使用当作一个思路和方向。慢慢写,慢慢改。
ES相关资料
wjw465150.github.io/Elasticsear…
wjw465150.github.io/Elasticsear…
1、 ElasticSearch
Elasticsearch是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。
2、 ELK
ELK=elasticsearch+Logstash+kibana
elasticsearch:后台分布式存储以及全文检索
logstash: 日志加工、“搬运工”
kibana:数据可视化展示。
ELK架构为数据分布式存储、可视化查询和日志解析创建了一个功能强大的管理链。
三者相互配合,取长补短,共同完成分布式大数据处理工作。
3、ES优势
1)分布式实时文件存储,可将每一个字段存入索引,使其可以被检索到。
2)实时分析的分布式搜索引擎。
分布式:索引分拆成多个分片,每个分片可有零个或多个副本。集群中的每个数据节点都可承载一个或多个分片,并且协调和处理各种操作;
负载再平衡和路由在大多数情况下自动完成。
3)可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。也可以运行在单台PC上(已测试)
4)支持插件机制,分词插件、同步插件、Hadoop插件、可视化插件等。
4、ES的实际项目落地场景
几乎每个系统都会有一个搜索的功能,当搜索做到一定程度时,维护和扩展起来难度就会慢慢变大,故而搜索部分会独立出一个模块,用ElasticSearch等来实现。
ElasticSearch不仅仅局限于搜索引擎方向,已增加数据聚合分析(aggregation)和可视化的特性,如果有数百万的文档需要通过关键词进行定位时,ElasticSearch肯定是最佳选择。当然,如果你的文档是JSON的,也可以把ElasticSearch当作一种“NoSQL数据库”, 应用ElasticSearch数据聚合分析(aggregation)的特性,针对数据进行多维度的分析
golang demo
package main
import (
"context"
"fmt"
"time"
"github.com/olivere/elastic"
)
// Tweet is a structure used for serializing/deserializing data in Elasticsearch.
type Tweet struct {
User string `json:"user"`
Message string `json:"message"`
Retweets int `json:"retweets"`
Image string `json:"image,omitempty"`
Created time.Time `json:"created,omitempty"`
Tags []string `json:"tags,omitempty"`
Location string `json:"location,omitempty"`
Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
}
const mapping = `
{
"settings":{
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings":{
"tweet":{
"properties":{
"user":{
"type":"keyword"
},
"message":{
"type":"text",
"store": true,
"fielddata": true
},
"image":{
"type":"keyword"
},
"created":{
"type":"date"
},
"tags":{
"type":"keyword"
},
"location":{
"type":"geo_point"
},
"suggest_field":{
"type":"completion"
}
}
}
}
}`
func main3() {
// Starting with elastic.v5, you must pass a context to execute each service
ctx := context.Background()
// Obtain a client and connect to the default Elasticsearch installation
// on 127.0.0.1:9200. Of course you can configure your client to connect
// to other hosts and configure it in various other ways.
client, err := elastic.NewClient(
elastic.SetURL("http://10.10.10.xxx:9200"),
)
if err != nil {
// Handle error
panic(err)
}
// Ping the Elasticsearch server to get e.g. the version number
info, code, err := client.Ping("http://10.10.10.xxx:9200").Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
// Getting the ES version number is quite common, so there's a shortcut
esversion, err := client.ElasticsearchVersion("http://10.10.10.1xx:9200")
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Elasticsearch version %s\n", esversion)
// Use the IndexExists service to check if a specified index exists.
exists, err := client.IndexExists("twitter").Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if !exists {
// Create a new index.
createIndex, err := client.CreateIndex("twitter").BodyString(mapping).Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if !createIndex.Acknowledged {
// Not acknowledged
}
}
// Update a tweet by the update API of Elasticsearch.
// We just increment the number of retweets.
// update, err := client.Update().Index("twitter").Type("tweet").Id("3").
// //Script(elastic.NewScriptInline("ctx._source.retweets += params.num").Lang("painless").Param("num", 5)).
// Script(elastic.NewScriptInline("ctx._source.task_ids.remove(params.num)").Lang("painless").Param("num", 42)).
// Upsert(map[string]interface{}{"retweets": 0}).
// Do(ctx)
// if err != nil {
// // Handle error
// panic(err)
// }
// fmt.Printf("New version of tweet %q is now %d\n", update.Id, update.Version)
update, err := client.Update().Index("xxxx").Type("xxxx").Id("94593fdb158c43eca267ac13295bda76").
//Script(elastic.NewScriptInline("ctx._source.retweets += params.num").Lang("painless").Param("num", 5)).
Script(elastic.NewScriptInline("ctx._source.task_ids.remove(42)").Lang("painless")).
//Upsert(map[string]interface{}{"retweets": 0}).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("New version of tweet %q is now %d\n", update.Id, update.Version)
// ...
// Delete an index.
// deleteIndex, err := client.DeleteIndex("twitter").Do(ctx)
// if err != nil {
// // Handle error
// panic(err)
// }
// if !deleteIndex.Acknowledged {
// // Not acknowledged
// }
}