ES是什麼?
用於全文搜索、結構化查詢、分析計算或三者結合
開源,構建於Apache Lucence之上
提供RESTful API[Office2] 和Java API
面向文檔,JSON格式
ES中重要概念?
集羣
節點
分片:將索引分成多個塊,每塊叫做一個分片,分片帶來規模上(數據水平切分[Office3] )和性能上(並行執行)的提升
複製分片:對主分片的複製,可以提供節點失敗時的高可用能力,同時提升搜索時的併發性能
ES中概念與mysql對應關係?
Index:索引,對應關係型數據庫中的數據庫,通過名字在集羣內唯一標識(必須全部小寫)
Type:類別,對應關係型數據庫中的表,通過名字在Index內唯一標識
Document:文檔,通過id在Type內的唯一標識
Mapping:映射,對應關係型數據庫的schema,確定Type及字段類型
數據類型
1.文本類型:text,當一個字段需要用於全文搜索(會被分詞),應該用text類型
2.關鍵字類型:keyword,當一個字段需要按照精確值進行過濾、排序、聚合等操作時,就應該使用keyword類型
3.數據類型: byte:字節類型 short:短整型 integer:整型 long:長整型 float:浮點型
4.時間類型:date
5.布爾類型:boolean
6.二進制型:binary
7.範圍類型:range
8.地理點類型:geo_point
等
操作之Index
操作之Document
-XPUT 代表如果有這條數則update,如果沒有這條數則添加
操作之查詢Query DSL
Query DSL包含兩部分:
Filter:實現文檔的yes/no邏輯,結果爲符合條件的列表
Query:需要計算文檔的相關性,結果爲按相關性排序後的列表
由於排序步驟存在,一般而言Query比Filter性能稍差
Filter常用:
term完全匹配,適用於number、date、boolean或者未分析的精確字符串字段
exists判斷某個字段是否存在
range範圍匹配,gt、gte、lt、lte
bool結合不同query的邏輯或運算,操作符must、must not、should
Query常用
match_all匹配所有文檔
match用來匹配某字段,會進行分詞
match_phrase精確匹配
multi_match對多個字段使用相同的match
sort對搜索結果進行排序
查看有哪些索引:
curl -X GET "http://es-1:9200/_cat/indices?v"
創建索引和mapping:
curl -H "Content-Type: application/json" -XPUT 'http://es-1:10200/esindex?pretty=true' -d '{"mappings":{"estype":{"properties":{"activity_id":{"type":"integer"},"bi_type":{"type":"keyword"},"channel_id":{"type":"integer"}}}}}'
刪除索引
curl -XDELETE 'http://es-1:9200/esindex'
查看mapping:
curl -X GET 'http://es-1:10200/index/_mapping/type'
示例1查詢,當index只有一個type時,可省略type,size表示希望展示的數據條數:
curl 'localhost:9200/index/_search' -d '
{
"query": {
"term": {
"user_id": "8544100"
}
},
"size":100
}'
示例2查詢,pretty表示查詢結果以json格式展示,當遇到elasticsearch6.x {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported"錯誤時,需要加上header -H "Content-Type: application/json"
curl 'http://localhost:9200/index/type/_search?pretty' -H 'Content-Type:application/json' -d '{
"query":{
"bool":{
"must":[
{
"match":{
"user_id":"933611"
}
}
]
}
}
}'
示例3查詢多個條件
curl 'http://localhost: 9200/index/type/_search' -H 'Content-Type:application/json' -d '{
"query":{
"bool":{
"must":[
{
"match":{
"city_id":"1101"
}
},
{
"match":{
"polygon_id":"812,-3007,[email protected]"
}
},
{
"match":{
"spark_common_count_day":"20181204"
}
}
]
}
}
}'
示例4:查詢總條數
curl 'http://localhost:9200/index/type/_count?pretty'
示例5:插入數據
curl -XPUT 'http://localhost:9200/index/type/id/' -d '
{
"unid":"933611||201812",
"user_id":933611,
"finish_cnt":30,
"order_amt":70500,
"pay_amt":72900,
"drawback_amt":2400,
"deduct_amt":3600,
"recharge_amt":2500,
"re_award_amt":50000,
"etl_time":"2018-12-31T13:18:04+08:00",
"order_cnt":42,
"dt_ym":"201812"
}
示例6:刪除數據
curl -XDELETE 'localhost:9200/index/type/id/'
示例7:查詢後排序
curl 'http://ip:port/index/type/_search' -H 'Content-Type:application/json' -d '{
"query":{
"bool":{
"must":[
{
"match":{
"city_id":"2301"
}
},
{
"range":{
"day_not_receive_orders":{
"gte":3
}
}
}
]
}
},
"sort":[
{
"reception_probability":"asc"
}
],
"size":20
}'
其它
一、elasticsearch-sql 是 NLPChina 開源的項目,旨在使用SQL查詢ES.
二、kopf 是一個與elasticsearch集羣交互的插件,可以查看shard 分佈,集羣狀態等信息
三、ElasticSearch Hive
- ElasticSearch-Hadoop 是 ElasticSearch 官方維護的一個子項目,用於連接ElasticSearch 和 Hadoop 兩套系統,支持與Mapreduce、Hive、Pig、Spark等系統的結合
- ElasticSearch 將數據存儲在本地,並不能直接利用HDFS中的數據,需要Hive將已經存在的表導入到ES中。數據的導入需要三步:
- 設定ES mapping
- Hive建表
- Insert數據
1、在ES中建立mapping,disable_all建議不要省略
PUT gal_dw
{
"mappings": {
"dwv_tkt_ride": {
"_all": {
"enabled": false
},
"properties": {
"line_id_m": {
"type": "long"
},
"depart_stop_id": {
"type": "long"
},
"start_loc": {
"type": "string"
}
}
}
}
}
2、在hive cli 中建立與要導入表對應的外部表
- add jar 命令將es與hive交互相關jar加入到classpath
- create 開始爲建表語句,其中STORED BY爲固定寫法
- TBLPROPERTIES內則是一些參數設置項,es.nodes指定es服務器列表,es.resource指定index/type,es.mapping.names則建立hive表明與es filed 名的映射關係(一一映射可省略)
add jar file://elasticsearch-hadoop-2.2.0/dist/elasticsearch-hadoop-hive-2.2.0.jar;
CREATE EXTERNAL TABLE es_dwv_tkt_ride(
line_id bigint,
start_loc string,
...
actual_pay double,
year string,
month string,
day string )
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES(
'es.nodes' = 'bigdata-dp-es001.bh:9200',
'es.resource' = 'gal_dw/dwv_tkt_ride',
'es.mapping.names' = 'line_id:line_id_m'
) ;
3、在Hive cli 中就可以使用insert語句向ES導入數據
- SET命令關閉不需要的Hive推測執行
- Insert 開始則將源表數據導入到上步建立的外部表中
SET hive.mapred.reduce.tasks.speculative.execution = false;
SET mapreduce.map.speculative = false;
SET mapreduce.reduce.speculative = false;
INSERT overwrite TABLE es_dwv_tkt_ride
select line_id, start_loc,
...,
actual_pay, year, monty, day from gal_dw.dwv_tkt_ride where concat(year,month,day)='20151231';
[Office1]近實時搜索:文檔在被索引和可搜索間有延遲,大約1s
[Office2]通過一套統一的接口爲各種不同前端設備提供服務
URL定位資源,用HTTP動詞(GET,PUT,POST,DELETE)描述操作
[Office3]數據切分:將存在同一數據庫中的數據分散存放到多個數據庫中,以達到分散單臺設備負載的效果
垂直切分:按照不同的表來切分到不同的數據庫,適用於業務耦合度底的情況
水平切分:同一表中的不同數據拆分到不同數據庫中,常見按日期