什么是ElasticSearch
- 基于Apache Lucene構(gòu)建的開源搜索引擎
- 采用Java編寫,提供簡(jiǎn)單易用的RESTFul API
- 輕松的橫向擴(kuò)展猪半,可支持BP級(jí)的結(jié)構(gòu)化和非結(jié)構(gòu)化的數(shù)據(jù)處理
可應(yīng)用場(chǎng)景
- 海量數(shù)據(jù)分析引擎
- 站內(nèi)搜索引擎
- 數(shù)據(jù)倉庫
一線公司實(shí)際應(yīng)用場(chǎng)景
- 英國衛(wèi)報(bào) - 實(shí)時(shí)分析公眾對(duì)文章的回應(yīng)
- 維基百科染服、GitHub-站內(nèi)實(shí)時(shí)搜索
- 百度 - 實(shí)時(shí)日志監(jiān)控平臺(tái)
安裝
Windows系統(tǒng)下Elasticsearch安裝與Elasticsearch-head插件安裝
分布式安裝
- 修改D:\Program Files\elasticsearch-5.5.2\config\elasticsearch.yml文件
在文件末尾加入:
cluster.name: fly #集群名
node.name: master #節(jié)點(diǎn)名
node.master: true #是否為主節(jié)點(diǎn)
network.host: 127.0.0.1 #ip地址
- 將elasticsearch-5.5.2.zip解壓為elasticsearch-1,修改elasticsearch-1\config\elasticsearch.yml文件,在文件末尾加入:
http.cors.enabled: true #跨域
http.cors.allow-origin: "*" #跨域
cluster.name: fly #集群名
node.name: slave1 #隨從節(jié)點(diǎn)名
network.host: 127.0.0.1 #IP地址
http.port: 8200 #端口號(hào)
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] #設(shè)置尋找主節(jié)點(diǎn)
- 將elasticsearch-5.5.2.zip解壓為elasticsearch-2,修改elasticsearch-2\config\elasticsearch.yml文件棚菊,在文件末尾加入:
http.cors.enabled: true #跨域
http.cors.allow-origin: "*" #跨域
cluster.name: fly #集群名
node.name: slave2 #隨從節(jié)點(diǎn)名
network.host: 127.0.0.1 #IP地址
http.port: 7200 #端口號(hào)
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] #設(shè)置尋找主節(jié)點(diǎn)
訪問localhost:9100如下圖:
localhost:9100
基礎(chǔ)概念
- 集群和節(jié)點(diǎn):一個(gè)集群是由多個(gè)節(jié)點(diǎn)組成的集合
- 索引:含有相同屬性的文檔集合
- 類型:索引可以包含一個(gè)或多個(gè)類型显押,文檔必須屬于一個(gè)類型
- 文檔:文檔是可以被索引的基本數(shù)據(jù)單元
- 分片:每個(gè)索引都有多個(gè)分片链韭,每個(gè)分片都是Lucene索引
- 備份:拷貝一個(gè)分片就完成了分片的索引
索引創(chuàng)建
- API基本格式:http://<ip>:<port>/<索引>/<類型>/<文檔id>
創(chuàng)建索引:訪問localhost:9100,點(diǎn)擊索引->創(chuàng)建索引煮落,輸入索引名book(小寫)敞峭,這樣創(chuàng)建的索引為非結(jié)構(gòu)化索引。
使用postman創(chuàng)建索引: - 選擇方式為PUT蝉仇,輸入地址:127.0.0.1:9200/people
- 點(diǎn)擊Body->row旋讹,文件格式選擇json,輸入內(nèi)容:
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
如下圖:
image
點(diǎn)擊send轿衔,返回內(nèi)容:
{
"acknowledged": true,
"shards_acknowledged": true
}
訪問127.0.0.1:9100沉迹,將會(huì)看到以添加people節(jié)點(diǎn)
文檔插入
文檔插入又分為:
- 指定文檔id插入:選擇PUT,地址輸入:127.0.0.1:9200/people/man/1害驹,內(nèi)容輸入:
{
"name":"石頭",
"age":"28",
"countory":"Chian",
"date":"1990-11-21"
}
點(diǎn)擊send鞭呕,返回內(nèi)容:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
- 自動(dòng)產(chǎn)生文檔id插入:選擇POST,地址輸入:127.0.0.1:9200/people/man/宛官,內(nèi)容輸入:
{
"name":"瘋狂的石頭",
"age":"29",
"countory":"Chian",
"date":"1990-11-22"
}
點(diǎn)擊send葫松,返回內(nèi)容
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
刷新localhost:9100,看到people索引的docs的數(shù)量為2
修改文檔
- 直接修改文檔: 選擇POST底洗,地址輸入127.0.0.1:9200/people/man/1/_update腋么,內(nèi)容輸入:
{
"doc":{
"age":30
}
}
訪問localhost:9100,點(diǎn)擊數(shù)據(jù)瀏覽,刷新亥揖,將會(huì)看到age已經(jīng)變?yōu)?0了
- 通過腳本修改文檔:選擇POST珊擂,地址輸入127.0.0.1:9200/people/man/1/_update,內(nèi)容輸入:
{
"script":{
"lang":"painless",
"inline":"ctx._source.age += 10"
}
}
訪問localhost:9100,點(diǎn)擊數(shù)據(jù)瀏覽,刷新费变,將會(huì)看到age已經(jīng)變?yōu)?0了
文檔查詢
- 簡(jiǎn)單查詢:
GET localhost:9200/people/man/1
返回結(jié)果
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"name": "石頭",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
- 條件查詢:
POST 127.0.0.1:9200/people/man/_search
body輸入內(nèi)容:
{
"query":{
"match_all":{}
}
}
返回結(jié)果
{
"took": 7, #耗時(shí)
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_score": 1,
"_source": {
"name": "瘋狂的石頭",
"age": "29",
"countory": "Chian",
"date": "1990-11-22"
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "石頭",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
]
}
}
指定文檔起始位置與大小查詢
POST 127.0.0.1:9200/people/man/_search
body輸入內(nèi)容:
{
"query":{
"match_all":{}
},
"from": 1,
"size": 1
}
從第一個(gè)文檔查詢摧扇,查詢大小為1,返回結(jié)果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "石頭",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
]
}
}
按條件查詢
POST 127.0.0.1:9200/people/man/_search
body輸入內(nèi)容:
{
"query":{
"match":{
"name":"石頭"
}
},
"sort":{
"age":{
"order":"desc"
}
}
}
sort:排序
返回內(nèi)容:
{
"took": 148,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": null,
"_source": {
"name": "石頭",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
},
"sort": [
40
]
},
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_score": null,
"_source": {
"name": "瘋狂的石頭",
"age": "29",
"countory": "Chian",
"date": "1990-11-22"
},
"sort": [
29
]
}
]
}
}
- 聚合查詢