上面第一、2點未知足,又捨不得去買雲服務器的小夥伴,就不要往下面看了,看了也白看,ElasticSearch對電腦配置要求較高,前置條件未知足的話,服務是起不來的。html
我演示的時候,是用的mac系統,上面裝了個虛擬機,虛擬機版本Centos6.5,jdk用的13,ElasticSearch用的版本是 7.8.1。這些我使用的包我下面也會提供,爲了學習的話,儘可能和我使用的版本一致,這樣你們碰到的問題都同樣,安裝過程當中,我也猜了很多坑,都總結出來了,仔細閱讀文檔就能夠搗鼓出來。java
經常使用的搜索網站:百度、谷歌node
指具備固定格式或有限長度的數據,如數據庫,元數據等。對於結構化數據,咱們通常都是能夠經過關係型數據庫(mysql、oracle)的table的方法存儲和搜索,也能夠創建索引。經過b-tree等數據結構快速搜索數據mysql
全文數據,指不定長或無固定格式的數據,如郵件,word等。對於非結構化數據,也即對全文數據的搜索主要有兩種方式:順序掃描法,全文搜索法linux
咱們能夠了解它的大概搜索方式,就是按照順序掃描的方式查找特定的關鍵字。好比讓你在一篇籃球新聞中,找出「科比」這個名字在那些段落出現過。那你確定須要從頭至尾把文章閱讀一遍,而後標出關鍵字在哪些地方出現過git
這種方式毋庸置疑是最低效的,若是文章很長,有幾萬字,等你閱讀完這篇新聞找到「科比」這個關鍵字,那得花多少時間github
對非結構化數據進行順序掃描很慢,咱們是否能夠進行優化?把非結構化數據想辦法弄得有必定結構不就行了嘛?將非結構化數據中的一部分信息提取出來,從新組織,使其變得有必定結構,而後對這些有必定結構的數據進行搜索,從而達到搜索相對較快的目的。這種方式就構成了全文搜索的基本思路。這部分從非結構化數據提取出的而後從新組織的信息,就是索引。正則表達式
根據百度百科中的定義,全文搜索引擎是目前普遍應用的主流搜索引擎。它的工做原理是計算機索引程序經過掃描文章中的每一個詞,對每一個詞創建一個索引,指明該詞在文章中出現的次數和位置,當用戶查詢時,檢索程序就根據事先創建的索引進行查找,並將查找的結果反饋給用戶。算法
注意,我使用的linux搭建的,固然Window(極度不推薦,坑太多)也能搭建,ElasticSearch安裝前須要先安裝jdk,這裏我使用的是jdk13,由於linux自帶jdk版本,須要先將以前的jdk版本卸載(點我直達),在安裝指定的jdk版本!!!sql
開發環境,建議關閉防火牆,避免沒必要要的麻煩!!!!生產環境,視狀況開啓端口號!!!!
service iptables stop 命令關閉防火牆,可是系統重啓後會開啓
chkconfig iptables off--關閉防火牆開機自啓動
ElasticSearch是強依賴jdk環境的,因此必定要安裝對應的jdk版本,並配置好相關的環境變量,好比ES7.X版本要裝jdk8以上的版本,並且是要官方來源的jdk。啓動的時候有可能會提示要裝jdk11,由於ES7以上官方都是建議使用jdk11,可是通常只是提示信息,不影響啓動。
ES官網推薦JDK版本兼容地址:點我直達
ES強依賴JVM,也很吃內存,因此必定要保證你的機器至少空閒出2G以上內存。推薦使用Linux,能夠本地搭建虛擬機。
啓動必定要使用非root帳戶!!!!這是ES強制規定的。ElasticSearch爲了安全考慮,不讓使用root啓動,解決辦法是新建一個用戶,用此用戶進行相關的操做。若是你用root啓動,會報錯。若是是使用root帳戶安裝ES,首先給安裝包受權,好比chown -R 777 安裝包路徑。而後再使用非root帳戶啓動,具體的權限配置,根據本身想要的配置。
高版本的ElasticSearch自帶jdk版本的,Linux中我安裝的是jdk13,沒用ElasticSearch自帶的jdk,有興趣的小夥伴能夠去研究下。
官網地址:點我直達
連接: https://pan.baidu.com/s/1jjNEErHtBu93HmvxKCT5Sw 密碼: kbcs
一、修改elasticsearch-x.x.x/config/elasticsearch.yml,主要修改爲如下內容
cluster.name: my-application
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["127.0.0.1", "[::1]"]
cluster.initial_master_nodes: ["node-1"]
bootstrap.system_call_filter: false
http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization
http.cors.allow-credentials: true
二、來到elasticsearch-x.x.x/bin下,執行:sh elasticsearch啓動,報錯,修改配置文件elasticsearch-env
三、設置用戶和組
groupadd elsearch
#添加用戶組,語法:groupadd 組名
useradd elsearch -g elsearch -p elasticsearch
#添加用戶,並將用戶添加到組中,語法:useradd 用戶名 -p 密碼 -g 組名
chown -R elsearch:elsearch elasticsearch-6.3.0
# 給用戶組受權,語法:chown -R 用戶:組名 es安裝完整路徑
注意=================以上root用戶操做===============
注意=================如下es用戶操做================
注意:若es用戶密碼登陸不上,在回到root用戶下,修改es用戶的密碼,語法:passwd 要修改用戶名
四、登陸到es用戶下,繼續啓動ElasticSearch,執行:sh elasticsearch
報錯以下:
java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
緣由:我用的Centos6.5,其linux內核版本爲2.6。而Elasticsearch的插件要求至少3.5以上版本。
解決方案:禁用這個插件便可
修改elasticsearch.yml文件,在最下面添加以下配置:
bootstrap.system_call_filter: false
5.繼續啓動ElasticSearch,執行:sh elasticsearch
修改一下內容須要使用root權限
報錯以下4條:
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
==========分割線===============
解決辦法以下
一、vim /etc/security/limits.conf文件,添加
* soft nofile 65535
* hard nofile 65535
二、vim /etc/security/limits.conf文件,添加
* soft nproc 4096
* hard nproc 4096
三、vim /etc/sysctl.conf 文件,添加
vm.max_map_count=262144
四、vim /var/soft/es7.8.1/elasticsearch-7.8.1/config/elasticsearch.yml 文件,添加
cluster.initial_master_nodes: ["node-1"]
修改完以後,必定要重啓,重啓,重啓,重要的事兒說三遍!!!!!
上面第2條問題,線程數修改不了,能夠嘗試使用這個方法修改線程數
Elasticsearch7.8.1 [1]: max number of threads [1024] for user [es] is too low, increase to at least [4096]異常
根據linux系統差別,有時候須要來點終極解決方案
新建: /etc/security/limits.d/test-limits.conf
cat>>test-limits.conf
而後加下內容:
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096
ctrl+d保存便可;
而後重啓服務器便可;
一、第一次配置過程當中,踩了很多坑,我踩過的坑,都在上面記錄了
二、若是照我上面哪一個方法還解決不了,自行根據ElasticSearch日誌,百度去找答案叭····
進入軟件的安裝目錄,進入到bin
執行:sh elasticsearch
進入軟件的安裝目錄,進入到bin
執行:sh elasticsearch -d -p pid
打開瀏覽器輸入:127.0.0.1:9200
類型 | 描述 | 默認位置 | 設置 |
bin
|
⼆進制腳本包含啓動節點的elasticsearch
|
{path.home}/bin
|
|
conf
|
配置⽂件包含elasticsearch.yml
|
{path.home}/confifig
|
path.conf
|
data
|
在節點上申請的每一個index/shard的數據⽂件的位置。
可容納多個位置
|
{path.home}/data
|
path.data
|
logs
|
⽇志⽂件位置
|
{path.home}/logs
|
path.logs
|
plugins
|
插件⽂件位置。每一個插件將包含在⼀個⼦⽬錄中。
|
{path.home}/plugins
|
path.plugins
|
傳統數據庫查詢數據的操做步驟是這樣的:創建數據庫->建表->插入數據->查詢
一個索引能夠理解成一個關係型數據庫
一個type就像一類表,好比user表、order表
注意
一、ES 5.X中一個index能夠有多種type
二、ES 6.X中一個index只能有一種type
三、ES 7.X之後已經移除type這個概念
mapping定義了每一個字段的類型等信息。至關於關係型數據庫中的表結構
一個document至關於關係型數據庫中的一行記錄
至關於關係型數據庫表的字段
集羣由一個或多個節點組成,一個集羣由一個默認名稱「elasticsearch」
集羣的節點,一臺機器或者一個進程
action | 描述 |
HEAD | 只獲取某個資源的頭部信息 |
GET | 獲取資源 |
POST | 建立或更新資源 |
PUT | 建立或更新資源 |
DELETE | 刪除資源 |
GET /user:列出全部的⽤戶
POST /user:新建⼀個⽤戶
PUT /user:更新某個指定⽤戶的信息
DELETE /user/ID:刪除指定⽤戶
獲取elasticcsearch狀態
curl -X GET "http://localhost:9200"
新建一個文檔
curl -X PUT "localhost:9200/xdclass/_doc/1" -H 'Content-Type:
application/json' -d' {
"user" : "louis",
"message" : "louis is good"
}
刪除一個文檔
curl -X DELETE "localhost:9200/xdclass/_doc/1"
此時再次查詢cba時,返回json會多一行
關閉索引標記消失
定義索引的結構,以前定義一個nba索引,可是沒有定義他的結構,咱們如今開始創建mapping;
type="keyword":是一個關鍵字,不會被分詞
type="text":會被分詞,使用的是全文索引
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "keyword"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
}
}
}
{
"persistent": {
"action.auto_create_index": "false"
}
}
當auto_create_index=false時,指定一個不存在的索引,新增文檔
{
"name":"楊超越",
"team_name":"夢之隊",
"position":"組織後衛",
"play_year":"0",
"jerse_no":"18"
}
PUT請求:ip:port/xxx/_doc/1?op_type=create
{
"docs": [{
"_index": "nba",
"_type": "_doc",
"_id": "1"
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2"
}
]
}
{
"script": "ctx._source.age = 18"
}
{
"script": "ctx._source.remove(\"age\")"
}
upsert當指定的文檔不存在時,upsert參數包含的內容將會被插入到索引中,做爲一個新文檔;若是指定的文檔存在,ElasticSearch引擎將會執行指定的更新邏輯。
並指定mapping
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
}
}
}
}
192.168.199.170:9200/nba/_doc/1
{
"name": "哈登",
"team_name": "⽕箭",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "13"
}
192.168.199.170:9200/nba/_doc/2
{
"name": "庫⾥",
"team_name": "勇⼠",
"position": "控球后衛",
"play_year": 10,
"jerse_no": "30"
}
192.168.199.170:9200/nba/_doc/3
{
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前鋒",
"play_year": 15,
"jerse_no": "23"
}
詞條查詢不會分析查詢條件,只有當詞條和查詢字符串徹底匹配時,才匹配搜索。
{
"query": {
"term": {
"jerse_no": "23"
}
}
}
{
"query": {
"terms": {
"jerse_no": [
"23",
"13"
]
}
}
}
ElasticSearch引擎會先分析查詢字符串,將其拆分紅多個分詞,只要已分析的字段中包含詞條的任意一個,或所有包含,就匹配查詢條件,返回該文檔;若是不包含任意一個分詞,表示沒有任何問的那個匹配查詢條件
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
{
"query": {
"match": {
"position":"後衛"
}
},
"from": 0,
"size": 10
}
{
"query": {
"multi_match": {
"query": "shooter",
"fields": ["title", "name"]
}
}
}
post 192.168.199.170:9200/nba/_update/2
{
"doc": {
"name": "庫⾥",
"team_name": "勇⼠",
"position": "控球后衛",
"play_year": 10,
"jerse_no": "30",
"title": "the best shooter"
}
}
相似於詞條查詢,精準查詢
前綴匹配
{
"query": {
"match_phrase_prefix": {
"title": "the best s"
}
}
}
post 192.168.199.170:9200/nba/_update/3
{
"doc": {
"name": "詹姆斯",
"team_name": "湖⼈",
"position": "⼩前鋒",
"play_year": 15,
"jerse_no": "23",
"title": "the best small forward"
}
}
標準分析器是默認分詞器,若是未指定,則使用該分詞器
{
"analyzer": "standard",
"text": "The best 3-points shooter is Curry!"
}
simple分析器當他遇到只要不是字母的字符,就將文本解析成term,並且全部的term都是小寫的
whitespace分析器,當他遇到空白字符時,就將文本解析成terms
stop分析器和simple分析器很想,惟一不一樣的是,stop分析器增長了對刪除中止詞的支持,默認使用了english中止詞
stopwords預約義的中止詞列表,好比(ths,a,an,this,of,at)等等
用正則表達式將文本分割成sterms,默認的正則表達式是\W+
put 192.168.199.170:9200/my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "whitespace"
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
{
"name": "庫⾥",
"team_name": "勇⼠",
"position": "控球后衛",
"play_year": 10,
"jerse_no": "30",
"title": "The best 3-points shooter is Curry!"
}
{
"query": {
"match": {
"title": "Curry!"
}
}
}
{
"analyzer": "standard",
"text": "⽕箭明年總冠軍"
}
安裝後重啓
{
"analyzer": "smartcn",
"text": "⽕箭明年總冠軍"
}
sh elasticsearch-plugin remove analysis-smartcn
下載地址:點我直達
安裝,解壓到plugins目錄
而後重啓
因爲json類型沒有date類型,因此es經過識別字符串是否符合format定義的格式來判斷是否爲date類型
format默認爲:strict_date_optional_time || epoch_millis
格式
從開始紀元(1970年1月1日0點)開始的毫秒數
PUT 192.168.199.170:9200/nba/_mapping
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text"
},
"date": {
"type": "date"
}
}
}
POST 192.168.199.170:9200/nba/_doc/4
{
"name": "蔡x坤",
"team_name": "勇⼠",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "31",
"title": "打球最帥的明星",
"date": "2020-01-01"
}
POST 192.168.199.170:9200/nba/_doc/5
{
"name": "楊超越",
"team_name": "猴急",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "32",
"title": "打球最可愛的明星",
"date": 1610350870
}
POST 192.168.199.170:9200/nba/_doc/6
{
"name": "吳亦凡",
"team_name": "湖⼈",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "33",
"title": "最會說唱的明星",
"date": 1641886870000
}
POST 192.168.199.170:9200/nba/_doc/8
{
"name": "吳亦凡",
"team_name": "湖⼈",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "33",
"title": "最會說唱的明星",
"date": "1641886870",
"array": [
"one",
"two"
],
"address": {
"region": "China",
"location": {
"province": "GuangDong",
"city": "GuangZhou"
}
}
}
索引方式
"address.region": "China",
"address.location.province": "GuangDong",
"address.location.city": "GuangZhou"
POST 192.168.199.170:9200/nba/_search
{
"query": {
"match": {
"address.region": "china"
}
}
}
IP類型的字段用於存儲IPv4和IPv6的地址,本質上是一個長整形字段
POST 192.168.199.170:9200/nba/_mapping
{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "long"
},
"jerse_no": {
"type": "keyword"
},
"title": {
"type": "text"
},
"date": {
"type": "date"
},
"ip_addr": {
"type": "ip"
}
}
}
PUT 192.168.199.170:9200/nba/_doc/9
{
"name": "吳亦凡",
"team_name": "湖⼈",
"position": "得分後衛",
"play_year": 10,
"jerse_no": "33",
"title": "最會說唱的明星",
"ip_addr": "192.168.1.1"
}
POST 192.168.199.170:9200/nba/_search
{
"query": {
"term": {
"ip_addr": "192.168.0.0/16"
}
}
}
可視化工具kibana的安裝和使用
賦權限
chown -R es:es781g /var/soft/kibana-7.8.1-linux-x86_64
# 給用戶組受權,語法:chown -R 用戶:組名 es安裝完整路徑
kibana.yml
server.port: 5601 #kibana端口
server.host: "10.0.0.169" #綁定的主機IP地址
elasticsearch.hosts: ["http://10.0.0.169:9200"] #elasticsearch的主機IP
kibana.index: ".kibana" #開啓此選項
i18n.locale: "zh-CN" #kibana默認文字是英文,變動成中文
ip:5601
後面示例,會大量使用該工具
手把手教你批量導入數據
ES提供了一個叫bulk的API來進行批量操做
數據
{"index": {"_index": "book", "_type": "_doc", "_id": 1}}
{"name": "權⼒的遊戲"} {"index": {"_index": "book", "_type": "_doc", "_id": 2}}
{"name": "瘋狂的⽯頭"}
curl -X POST "192.168.199.170:9200/_bulk" -H 'Content-Type: application/json' --data-binary @test
{"mappings":{"properties":{"birthDay":{"type":"date"},"birthDayStr": {"type":"keyword"},"age":{"type":"integer"},"code": {"type":"text"},"country":{"type":"text"},"countryEn": {"type":"text"},"displayAffiliation":{"type":"text"},"displayName": {"type":"text"},"displayNameEn":{"type":"text"},"draft": {"type":"long"},"heightValue":{"type":"float"},"jerseyNo": {"type":"text"},"playYear":{"type":"long"},"playerId": {"type":"keyword"},"position":{"type":"text"},"schoolType": {"type":"text"},"teamCity":{"type":"text"},"teamCityEn": {"type":"text"},"teamConference": {"type":"keyword"},"teamConferenceEn":{"type":"keyword"},"teamName": {"type":"keyword"},"teamNameEn":{"type":"keyword"},"weight": {"type":"text"}}}}
POST nba/_search
{
"query": {
"term": {
"jerseyNo": "23"
}
},
"from": 0,
"size": 20
}
Exsit Query在特定的字段中查找非空值的文檔(查找隊名非空的球員)
Prefix Query查找包含帶有指定前綴term的文檔(查找隊名爲Rock開頭的球員)
Wildcard Query支持通配符查詢,*表示任意字符,?表示任意單個字符(查找火箭隊的球員)
Regexp Query正則表達式查詢(查找火箭隊的球員)
Ids Query(查找id爲1和2的球員)
查詢指定字段在指定範圍內包含值(日期、數字或字符串)的文檔
查找在nba打球在2年到10年之內的球員
POST nba/_search
{
"query": {
"range": {
"playYear": {
"gte": 2,
"lte": 10
}
}
},
"from": 0,
"size": 20
}
查找1999年到2020年出生的球員
POST nba/_search
{
"query": {
"range": {
"birthDay": {
"gte": "01/01/1999",
"lte": "2020",
"format": "dd/MM/yyyy||yyyy"
}
}
},
"from": 0,
"size": 20
}
type | description |
must | 必須出如今匹配文檔中 |
filter | 必須出如今文檔中,可是不打分 |
must_not | 不能出如今文檔中 |
should | 應該出如今文檔中 |
POST nba/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 20
}
POST nba/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"displayNameEn": "james"
}
}
]
}
},
"from": 0,
"size": 20
}
POST nba/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "Eastern"
}
}
}
]
}
},
"from": 0,
"size": 20
}
組合起來含義:必定不在東部的james
即便匹配不到也返回,只是評分不一樣
POST nba/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "Eastern"
}
}
}
],
"should": [
{
"range": {
"playYear": {
"gte": 11,
"lte": 20
}
}
}
]
}
},
"from": 0,
"size": 20
}
若是minimum_should_match=1,則變成要查出名字叫作James的打球時間在11年到20年西部球員
POST nba/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"displayNameEn": "james"
}
}
],
"must_not": [
{
"term": {
"teamConferenceEn": {
"value": "Eastern"
}
}
}
],
"should": [
{
"range": {
"playYear": {
"gte": 11,
"lte": 20
}
}
}
],
"minimum_should_match": 1
}
},
"from": 0,
"size": 20
}
minimum_should_match表明了最小匹配經度,若是設置minimum_should_match=1,那麼should語句中至少須要有一個條件知足
POST nba/_search
{
"query": {
"match": {
"teamNameEn": "Rockets"
}
},
"sort": [
{
"playYear": {
"order": "desc"
}
}
],
"from": 0,
"size": 20
}
火箭隊中按打球時間從大到小,若是年齡相同則按照身高從高到低排序的球員
POST nba/_search
{
"query": {
"match": {
"teamNameEn": "Rockets"
}
},
"sort": [
{
"playYear": {
"order": "desc"
}
},{
"heightValue": {
"order": "asc"
}
}
],
"from": 0,
"size": 20
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"countPlayerYear": {
"value_count": {
"field": "playYear"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
}
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"countAget": {
"cardinality": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"statsAge": {
"stats": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"extendStatsAge": {
"extended_stats": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"pecentAge": {
"percentiles": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"pecentAge": {
"percentiles": {
"field": "age",
"percents": [
20,
50,
75
]
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"aggsAge": {
"terms": {
"field": "age",
"size": 10
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"aggsAge": {
"terms": {
"field": "age",
"size": 10,
"order": {
"_key": "desc"
}
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"aggsAge": {
"terms": {
"field": "age",
"size": 10,
"order": {
"_count": "desc"
}
}
}
},
"size": 0
}
POST /nba/_search
{
"query": {
"term": {
"teamNameEn": {
"value": "Rockets"
}
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
},
"size": 0
}
POST /nba/_search
{
"aggs": {
"aggsTeamName": {
"terms": {
"field": "teamNameEn",
"include": [
"Lakers",
"Rockets",
"Warriors"
],
"exclude": [
"Warriors"
],
"size": 30,
"order": {
"avgAge": "desc"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
POST /nba/_search
{
"aggs": {
"aggsTeamName": {
"terms": {
"field": "teamNameEn",
"include": "Lakers|Ro.*|Warriors.*",
"exclude": "Warriors",
"size": 30,
"order": {
"avgAge": "desc"
}
},
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
},
"size": 0
}
POST /nba/_search
{
"aggs": {
"ageRange": {
"range": {
"field": "age",
"ranges": [
{
"to": 20
},
{
"from": 20,
"to": 35
},
{
"to": 35
}
]
}
}
},
"size": 0
}
POST /nba/_search
{
"aggs": {
"birthDayRange": {
"date_range": {
"field": "birthDay",
"format": "MM-yyy",
"ranges": [
{
"to": "01-1989"
},
{
"from": "01-1989",
"to": "01-1999"
},
{
"from": "01-1999",
"to": "01-2009"
},
{
"from": "01-2009"
}
]
}
}
},
"size": 0
}
POST /nba/_search
{
"aggs": {
"birthday_aggs": {
"date_histogram": {
"field": "birthDay",
"format": "yyyy",
"interval": "year"
}
}
},
"size": 0
}
query_string查詢,若是熟悉lucene的查詢語法,咱們能夠直接用lucene查詢語法寫一個查詢串進行查詢,ES中接到請求後,經過查詢解析器,解析查詢串生成對應的查詢。
POST /nba/_search
{
"query": {
"query_string": {
"default_field": "displayNameEn",
"query": "james OR curry"
}
},
"size": 100
}
POST /nba/_search
{
"query": {
"query_string": {
"default_field": "displayNameEn",
"query": "james AND harden"
}
},
"size": 100
}
在開發中,隨着業務需求的迭代,較老的業務邏輯就要面臨更新甚至是重構,而對於es來講,爲了適應新的業務邏輯,可能就要對原有的索引作一些修改,好比對某字段作調整,甚至是重構索引。而作這些操做的時候,可能會對業務形成影響,甚至是停機調整等問題。由此,es提供了索引別名來解決這些問題。索引別名就像一個快捷方式或軟鏈接,能夠指向一個或多個索引,也能夠給任意一個須要索引名的API來使用。別名的應用爲程序提供了極大地靈活性。
GET /nba/_alias
GET /_alias
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
方式一
POST /_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
}
]
}
方式二
DELETE /nba/_alias/nba_v1.0
POST /_aliases
{
"actions": [
{
"remove": {
"index": "nba",
"alias": "nba_v1.0"
}
},
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
}
]
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
},{
"add": {
"index": "cba",
"alias": "cba_v2.0"
}
}
]
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_v2.0"
}
},{
"add": {
"index": "nba",
"alias": "cba_v2.2"
}
}
]
}
GET /nba_v2.2
POST /nba_v2.0/_doc/566
{
"countryEn": "Croatia",
"teamName": "快船",
"birthDay": 858661200000,
"country": "克羅地亞",
"teamCityEn": "LA",
"code": "ivica_zubac",
"displayAffiliation": "Croatia",
"displayName": "伊維察 祖巴茨哥哥",
"schoolType": "",
"teamConference": "⻄部",
"teamConferenceEn": "Western",
"weight": "108.9 公⽄",
"teamCity": "洛杉磯",
"playYear": 3,
"jerseyNo": "40",
"teamNameEn": "Clippers",
"draft": 2016,
"displayNameEn": "Ivica Zubac",
"heightValue": 2.16,
"birthDayStr": "1997-03-18",
"position": "中鋒",
"age": 22,
"playerId": "1627826"
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "national_player",
"is_write_index": true
}
},
{
"add": {
"index": "cba",
"alias": "national_player"
}
}
]
}
POST /national_player/_doc/566
{
"countryEn": "Croatia",
"teamName": "快船",
"birthDay": 858661200000,
"country": "克羅地亞",
"teamCityEn": "LA",
"code": "ivica_zubac",
"displayAffiliation": "Croatia",
"displayName": "伊維察 祖巴茨妹妹",
"schoolType": "",
"teamConference": "⻄部",
"teamConferenceEn": "Western",
"weight": "108.9 公⽄",
"teamCity": "洛杉磯",
"playYear": 3,
"jerseyNo": "40",
"teamNameEn": "Clippers",
"draft": 2016,
"displayNameEn": "Ivica Zubac",
"heightValue": 2.16,
"birthDayStr": "1997-03-18",
"position": "中鋒",
"age": 22,
"playerId": "1627826"
}
ElasticSearch是一個實時的分佈式搜索引擎,爲用戶提供搜索服務,當咱們決定存儲某種數據時,在建立索引的時候須要將數據結構完整肯定下來,於此同時索引的設定和不少固定配置將不能修改。當須要改變數據結構時,就須要從新創建索引,爲此,Elastic團隊提供了不少輔助工具幫助開發人員進行重建索引
PUT /nba_20220810
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"code": {
"type": "text"
},
"country": {
"type": "keyword"
},
"countryEn": {
"type": "keyword"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "keyword"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
POST /_reindex
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220810"
}
}
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20220810"
}
}
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba_20220810",
"alias": "nba_latest"
}
},
{
"remove": {
"index": "nba",
"alias": "nba_latest"
}
}
]
}
DELETE /nba
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
}
}
新的數據一添加到索引中立馬就能搜索到,可是真實狀況不是這樣的
咱們使用鏈式命令請求,先添加一個文檔,再馬上搜索
curl -X PUT 192.168.199.170:9200/star/_doc/888 -H 'Content-Type:
application/json' -d '{ "displayName": "蔡徐坤" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
強制刷新
curl -X PUT 192.168.199.170:9200/star/_doc/666?refresh -H 'Content-Type:
application/json' -d '{ "displayName": "楊超越" }'
curl -X GET localhost:9200/star/_doc/_search?pretty
修改默認更新時間(默認時間是1s)
PUT /star/_settings
{
"index": {
"refresh_interval": "5s"
}
}
將refresh關閉
PUT /star/_settings
{
"index": {
"refresh_interval": "-1"
}
}
若是返回的結果集中不少符合條件的結果,那怎麼能一眼就能看到咱們想要的那個結果呢?好比下面網站所示的那樣,咱們搜索「科比」,在結果集中,將全部「科比」高亮顯示?
POST /nba_latest/_search
{
"query": {
"match": {
"displayNameEn": "james"
}
},
"highlight": {
"fields": {
"displayNameEn": {}
}
}
}
POST /nba_latest/_search { "query": { "match": { "displayNameEn": "james" } }, "highlight": { "fields": { "displayNameEn": { "pre_tags": [ "<h1>" ], "post_tags": [ "</h1>" ] } } } }
查詢建議:是爲了給用戶提供更好的搜索體驗。包括:詞條檢查,自動補全
text | 指定搜索文本 |
field | 獲取建議詞的搜索字段 |
analyzer | 指定分詞器 |
size | 每一個詞返回的最大建議詞數 |
sort | 如何對建議詞進行排序,可用選項: score:先按評分排序、再按文檔頻率排、term順序 frequency:先按文檔頻率排,再按評分,term順序排 |
suggest_mode | 建議模式,控制提供建議詞的方式: missing:僅在搜索的詞項在索引中不存在時才提供建議詞,默認值; popular:僅建議文檔頻率比搜索詞項高的詞 always:老是提供匹配的建議詞 |
term詞條建議器,對給輸入的文本進行分詞,爲每一個分詞提供詞項建議
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse hardne",
"term": {
"suggest_mode": "missing",
"field": "displayNameEn"
}
}
}
}
phrase短語建議,在term的基礎上,會考量多個term之間的關係,好比是否同時出如今索引的原文裏,相鄰成都,以及詞頻等
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "jamse harden",
"phrase": {
"field": "displayNameEn"
}
}
}
}
Completion完成建議
POST /nba_latest/_search
{
"suggest": {
"my-suggestion": {
"text": "Miam",
"completion": {
"field": "teamCityEn"
}
}
}
}