忽然想寫一個關於Elasticsearch
基本使用的文檔,由於Elasticsearch
入門的門檻特別低,加上本身好久沒有使用了。最近項目接入Elasticsearch
想了想仍是寫一篇關於基本使用,後面寫文章掰開揉碎了分析Elasticsearch
。
本文不會再有表情包出現,是個比較正經的小白入門手冊。 大佬就請繞道吧!!!卑微在線祈禱。php
看完本篇你對Elasticsearch仍是一無所知 哈哈哈哈哈哈哈哈哈
本篇講的官網文檔全都有 惟一的是能夠根據代碼裝個環境本身動手
1.首先你要有個dockernode
docker pull elasticsearch:6.7.2 docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.7.2 // 就已經啓動成功了 不須要再任何操做
2.其次你 會點php 事實上demo換成任何語言都很簡單
3.git clone https://github.com/hongg-coder/es.git
4.用的官方的sdk 對應連接地址packagist 務必提早看下readme
5.debug的數據源大概有1000條 根據下面走 執行腳本自動構建 數據文件在data/user_list.json
mysql
若是你習慣mysql
能夠轉變下概念mysql
-> databases
-> talbes
-> rows
-> Colums
elasticsearch
-> Inices
-> Types
-> Documents
-> Fields
git
來自elasticsearch權威指南說明
demo以簡單用戶爲例子github
user_id
用戶id 須要範圍查詢nickname
用戶暱稱 須要精確查詢tag
用戶標籤 須要多條件查詢birthday
用戶出生日 須要日期查詢mail
用戶郵箱 須要精確查詢description
用戶簡介須要分詞查詢sql
{ // 用戶id "user_id":1, // 用戶暱稱 "nickname":"洪二光", // 用戶標籤 "tag":[ "渣男", "菜鳥" ], // 出生日期 "birthday":"1994-11-30", // 郵箱 "mail":"1569498875@qq.com", // 用戶備註 "description":"oh shit" }
index
定義爲user
type
定義爲_doc
docker
// 按照命令執行 數據自動構建成功 php src/build_index.php
理解兩個概念mapping
和analysis
通俗的解釋mapping
決定你定義的文檔字段類型 analysis
決定對應的文檔字段怎麼被搜索解析json
對應的mappingapp
{ "user": { "mappings": { "_doc": { "properties": { "birthday": { "type": "date", "format": "yyyy-MM-dd" }, "description": { "type": "text", "analyzer": "standard" }, "mail": { "type": "keyword" }, "nickname": { "type": "keyword" }, "tag": { "type": "keyword" }, "user_id": { "type": "integer" } } } } } }
簡單理解keyword
和text
keyword
支持curl
不支持
text
支持
不支持
構建完
curl http://127.0.0.1:9200/user/_doc/_search // 或者 php src/all_search.php 若是有數據返回說明構建成功
查詢性能過濾語句 > 查詢語句
那麼查詢語句
經常使用語全文本搜索(簡單理解就是須要用到分詞搜索
)
查詢用戶id爲2的用戶
對標mysql
查詢select * from user where user_id=2
對應的es語法
GET /user/_doc/_search { "query" : { "term" : { "user_id" : 2 } } }
對應的demophp src/search1.php
查詢郵箱爲WcQT9YHGXb@mail.com的用戶
對標mysql
查詢select * from user where mail='WcQT9YHGXb@mail.com'
GET /user/_doc/_search { "query": { "term": { "mail": "WcQT9YHGXb@mail.com" } } }
若是查詢字符串含有中文 見下方查詢語句 1
對應的demophp src/search2.php
查詢出生日期 > 1994年11月30日的全部用戶
對標mysql
查詢select * from user where birthday > '1994-11-30'
GET /user/_doc/_search { "query": { "range": { "birthday": { "gt": "1994-11-30" } } } }
對應的demophp src/search3.php
gt : >
gte : >=
lt : c
lte : <=
查詢用戶id在1~10的區間內
對標mysql
查詢select * from user where user_id in (1,2,3,4,5,6,7,8,9,10)
GET /user/_doc/_search { "query": { "terms": { "user_id": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] } } }
php src/search4.php
查詢出標籤含有渣男
的全部用戶
對標mysql
查詢不想對標了! 若是業務涉及標籤查詢別想用mysql去設計!!
GET /user/_doc/_search { "query": { "term": { "tag": "渣男" } } }
php src/search8.php
mapping
對應爲keyword
查詢用戶暱稱爲洪2光
的用戶
對標mysql
查詢select * from user where realname = '洪2光'
GET /user/_doc/_search { "query":{ "match":{ "nickname":"洪2光" } } }
php src/search5.php
mapping
對應 text
查詢用戶描述 opportunity R.M. Nixon
相關
對標mysql
查詢對標個球球
GET /user/_doc/_search { "query":{ "match":{ "description":"opportunity R.M. Nixon" } } }
php src/search6.php
mapping
對應 text
(心裏 能不用就別用 若是真有這個場景考慮是否是不要定義爲text)精確查詢用戶描述爲Better an open enemy than a false friend.
對標mysql
查詢select * from user where description = 'Be honest rather clever.'
能夠試着用上述任何方法去查詢 會發現返回沒有任何結果。
若是mapping定義text分詞還須要同時知足精確搜索 咱們須要修改下mapping
"description": { "type": "text", "analyzer": "standard", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
修改後設置兩個field,一個是field倒排索引後的自己,還有一個就是field.keyword(description.keyword),這個字段默認是不分詞的,而且最多保留ignore_above
設定長度(字符)
php src/build_index2.php
而後
GET /user/_doc/_search { "query": { "term": { "description.keyword": "Be honest rather clever." } } }
php src/search7.php
查詢出標籤同時含有渣男
和宅男
的全部用戶
GET /user/_doc/_search { "query":{ "bool":{ "must":[ {"term":{"tag":"渣男"}}, {"term":{"tag":"宅男"}} ] } } }
php src/search9.php
查詢出標籤含有渣男
和宅男
其中一個的全部用戶
GET /user/_doc/_search { "query": { "terms": { "tag": [ "渣男", "宅男" ] } } }
php src/search10.php
查詢郵箱爲MGyY5VRs9r@mail.com
或WvJTELTX9d@mail.com
對標mysqlselect * from user where mail ='MGyY5VRs9r@mail.com' or mail = 'WvJTELTX9d@mail.com'
GET /user/_doc/_search { "query":{ "bool":{ "should":[ {"term":{"mail":"MGyY5VRs9r@mail.com"}}, {"term":{"mail":"WvJTELTX9d@mail.com"}} ] } } }
php src/search11.php
查詢用戶id 1到100內 且出生年 大於1994-11-30 且 標籤爲 渣男的用戶
GET /user/_doc/_search { "query": { "bool": { "must": [ { "range": { "birthday": { "gt": "1994-11-30" } } }, { "term":{ "tag":"渣男" } }, { "range":{ "user_id":{ "gte":1, "lte":100 } } } ] } } }
php src/search12.php
查詢用戶id 1到100內 或200到400內 且 標籤爲 渣男的用戶 且 我的簡介分詞包含 life
、trust
GET /user/_doc/_search { "query": { "bool": { "should": [ { "range": { "user_id": { "gte": 1, "lte": 100 } } }, { "range": { "user_id": { "gte": 200, "lte": 400 } } } ], "must": [ { "term": { "tag": "渣男" } }, { "match": { "description": "trust life" } } ] } } }
php src/search13.php
就寫到這了,關於es不到20%,可是寫demo手敲累了(是的,單純本身累了),剩下本身去摸索吧。
先學會用Elasticsearch
再去學原理,下面幾篇會把構建索引、解析器包括如何自定義解析器,最後在去學習一些Elasticsearch深層次的東西