首先說明ElasticSearch版本號5.5.2
目錄介紹
- bin目錄 存放es啓動腳本
- config目錄 存放es配置文件
- lib目錄 第三方庫依賴庫
- modules 模塊目錄
- plugins 存放第三方插件目錄
安裝集羣
master節點配置
cluster.name: wali
node.name: master
node.master: true
network.host: 127.0.0.1
slave1節點配置
cluster.name: wali
node.name: slave1
network.host: 127.0.0.1
http.port: 8200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
slave2節點配置
cluster.name: wali
node.name: slave2
network.host: 127.0.0.1
http.port: 8000
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
電腦配置不行可以修改jvm.options
-Xms1g
-Xmx1g
基礎概念
- 集羣和節點 集羣是一個或多個節點組成的集合
- 索引 含有相同屬性的文檔集合
- 類型 索引可以定義一個或多個類型, 文檔必須屬於一個類型
- 文檔 文檔是可以被索引的基本數據單位
- 分片 每個索引都有多個分片,每個分片是一個Lucene索引
- 備份 拷貝一份分片就完成了分片備份
基本用法
RESTful API
API基本格式 http://<ip>:<port>/<索引>/<類型><文檔id>
常用HTTP動詞 GET/PUT/POST/DELETE
創建索引
分片數默認爲5,副本數默認1(如果只有一臺機器設置爲0)
創建結果如下,mapping爲空標識 非結構化索引
創建結構化索引
{
"novel": {
"properties": {
"title": {
"type": "text"
}
}
}
}
前往查看效果如下:
通過postman創建索引
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"man": {
"properties": {
"name": {
"type": "text"
},
"country": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyy-MM--dd HH:mm:ss||yyy-MM-dd||epoch_millis"
}
}
},
"woman": {
"properties": {
"name": {
"type": "text"
},
"country": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyy-MM--dd HH:mm:ss||yyy-MM-dd||epoch_millis"
}
}
}
}
}
插入文檔
指定文檔id插入
{
"name": "張三",
"country": "China",
"age": 30,
"date": "1996-11-18"
}
自動產生文檔id插入
{
"name": "張三1",
"country": "China",
"age": 40,
"date": "1996-11-18"
}
修改文檔
直接修改文檔
127.0.0.1:9200/people/man/1/_update
{
"doc": {
"name": "張三修改"
}
}
腳本修改文檔
127.0.0.1:9200/people/man/1/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.age += params.age",
"params": {
"age": 1
}
}
}
刪除
刪除文檔
127.0.0.1:9200/people/man/1/
刪除索引
127.0.0.1:9200/test/
搜索
創建搜索
127.0.0.1:9200/books
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 0
},
"mappings": {
"novel": {
"properties": {
"word_count": {
"type": "integer"
},
"author": {
"type": "keyword"
},
"title": {
"type": "text"
},
"publish_date": {
"type": "date",
"format": "yyy-MM--dd HH:mm:ss||yyy-MM-dd||epoch_millis"
}
}
}
}
}
按照指定文檔Id查詢
搜索條件
took響應時間5ms
hits響應的結果
total總共數據11條。默認只返回10條
添加新的條件 size返回條數
查找title中有ElasticSearch,返回數據默認以_score倒排
添加自己的排序,按照publish_date降序排列
聚合查詢
group_by_word_count隨便起名字
查詢結果,1000的有5個
兩個聚合條件
查詢結果 group_by_word_count和上面一樣,另一個是 1997-01-01有2條數據
對word_count進行計算
127.0.0.1:9200/books/_search
POST
{
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
count總共11條,min最小,max最大,avg平均,sum總和
也可以直接將stats改爲min
{
"aggs": {
"grades_word_count": {
"min": {
"field": "word_count"
}
}
}
}
運行結果如下
高級查詢
子條件查詢 特定字段查詢所指特定值
Query Context
在查詢過程中,除了判斷文檔是否滿足查詢條件外,ES還會計算一個_score來表示匹配的程度,旨在判斷目標文檔和查詢條件匹配的有多好。
全文本查詢 針對文本類型數據
數據展示
模糊匹配關鍵詞match
搜索author爲瓦力
POST請求方式
127.0.0.1:9200/books/_search
{
"query": {
"match": {
"author": "瓦力"
}
}
}
把上面條件進行修改如下,它會匹配ElasticSearch和入門這兩個,所以返回上面熟的id爲8,2,3,4,9的數據(結果太長截圖不好看)
{
"query": {
"match": {
"title": "ElasticSearch入門"
}
}
}
如果非要匹配title爲ElasticSearch入門,則需要用到習語匹配,返回數據只剩下一條滿足的id爲8
{
"query": {
"match_phrase": {
"title": "ElasticSearch入門"
}
}
}
進行多個字段的匹配查詢,由於上面數據中title和author沒有公共的,隨意改一個就行
查詢代碼如下
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"multi_match": {
"query": "瓦力",
"fields": ["author","title"]
}
}
}
語法查詢
查詢結果是id爲4 和 3
如果把括號和OR Python去掉查詢結果爲4
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"query_string": {
"query": "(ElasticSearch AND 好) OR Python"
}
}
}
指定字段查詢,查詢結果Id爲8,11,4,9
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"query_string": {
"query": "ElasticSearch OR 瓦力",
"fields": ["author","title"]
}
}
}
字段級別查詢 針對結構化數據,如數字、日期等
查詢word_count爲1000的數據,查詢結果id爲10,4,1,7,11
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"term": {
"word_count": 1000
}
}
}
查詢author爲瓦力,查詢結果id爲8
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"term": {
"author": "瓦力"
}
}
}
支持範圍查詢,查詢word_count,大於等於1000小於等於2000,查詢結果id爲10,2,4,1,7,3,11
去掉e相當於少了等於
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"range": {
"word_count": {
"gte": 1000,
"lte": 2000
}
}
}
}
查詢日期,2000-01-01到2000-12-31書籍,查詢結果id爲10,1,11
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"range": {
"publish_date": {
"gte": "2000-01-01",
"lte": "2000-12-31"
}
}
}
}
查詢日期,2017-08-15到現在書籍,查詢結果id爲8,9
POST方式
127.0.0.1:9200/books/_search
{
"query": {
"range": {
"publish_date": {
"gte": "2017-08-15",
"lte": "now"
}
}
}
}
Filter Context
在查詢過程中,值判斷該文檔是否滿足條件,只有Yes或No
相對於query較快,es會對數據進行緩存
POST
127.0.0.1:9200/books/_search
{
"query": {
"bool": {
"filter": {
"term": {
"word_count": 10000
}
}
}
}
}
複合條件查詢 以一定的邏輯組合子條件查詢
固定分數查詢
只支持filter,返回_score爲1.0,boost指定_score的值
POST
127.0.0.1:9200/books/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"title": "ElasticSearch"
}
}
}
}
}
{
"query": {
"constant_score": {
"filter": {
"match": {
"title": "ElasticSearch"
}
},
"boost": 2
}
}
}
布爾查詢
裏面條件是或的關係,只要滿足一個就行。查詢結果id爲8,4,9
POST
127.0.0.1:9200/books/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"author": "瓦力"
}
},
{
"match": {
"title": "ElasticSearch"
}
}
]
}
}
}
查詢前添加了一條數據
裏面條件是並且的關係,查詢結果id爲8,12
POST
127.0.0.1:9200/books/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"author": "瓦力"
}
},
{
"match": {
"title": "ElasticSearch"
}
}
]
}
}
}
上面條件再加一個word_count條件,查詢結果id爲12
POST
127.0.0.1:9200/books/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"author": "瓦力"
}
},
{
"match": {
"title": "ElasticSearch"
}
}
],
"filter": [
{
"term": {
"word_count": 1000
}
}
]
}
}
}
mast_not不滿足條件的結果,查詢結果id爲5,9,10,2,4,6,1,7,3,11
POST
127.0.0.1:9200/books/_search
{
"query": {
"bool": {
"must_not": [
{
"term": {
"author": "瓦力"
}
}
]
}
}
}