本文操做基於 elasticsearch7.6.1 測試經過數據庫
指定mapping, 建立一個indexbash
PUT /test_user2 { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "name":{ "type":"keyword" } } } } 複製代碼
咱們系統運行一段時間後, 想增長一個地址的字段, 如何手動指定類型呢?markdown
在已有index mapping中添加新類型併發
PUT /test_user2/_mapping { "properties": { "address":{ "type":"text", "index": false //禁止被檢索 } } } 複製代碼
//設置模版-類型推斷爲int PUT /_template/template_1 { "index_patterns": ["user*"], "order":1, "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "numeric_detection": true } } //獲取模版信息 GET /_template/template_1 複製代碼
在mapping中咱們還能夠設置 dynamic 屬性app
dynamic 能夠分爲動態映射(dynamic mapping)和靜態(顯式)映射(explicit mapping)和精確(嚴格)映射(strict mappings),具體由dynamic屬性控制。dom
動態映射(dynamic:true) 自動建立自動索引elasticsearch
靜態映射(dynamic:false)能夠存進去 但不能檢索性能
嚴格模式(dynamic:false)若是遇到新的字段,就拋出異常測試
檢測分詞狀況 GET /_analyze { "analyzer": "standard", "text":"Waiting to Exhale (1995)" } 複製代碼
//查看test_users索引的name字段怎樣分詞, 默認爲standard GET /test_users/_analyze { "field":"name", "text":"hello world 中國" } 複製代碼
//建立一個document, 若是存在則更新(不推薦) POST /test_users/create/1 { "name":"xiaoyu", "age": 22, "address":"河北保定" } //只能新增, 不然報錯 POST /test_users/_doc/3?op_type=create { "name":"wansan", "age": 33, "address":"河北保定" } //建立或更新(推薦) PUT /test_users/_doc/2 { "name":"linlin", "age": 22, "address":"河北保定" } //更新-覆蓋掉舊數據 PUT /test_users/_doc/2 { "sex":"女" } //將sex加入到原文檔中(只更新指定的字段) POST /test_users/_update/2 { "doc":{ "sex":"女" } } 複製代碼
//查詢字段中包含關鍵詞的數據
GET /test_users/_search?q=河北
//查詢_id=1的數據
GET test_users/_doc/1
複製代碼
模糊查詢Waiting 和 Exhale優化
GET xiaoyu_movie/_search { "query": { "match": { "column2": "Waiting Exhale", } } } 複製代碼
可是這不是咱們想要的結果, 咱們想同時包含這兩個單詞
固然你會想到拆分多條件查詢
GET xiaoyu_movie/_search { "query": { "bool": { "must": [ { "match":{ "column2": "Waiting" } }, { "match":{ "column2": "Exhale" } } ] } } } 複製代碼
這裏咱們使用一種更簡便的方式
GET xiaoyu_movie/_search { "profile": "true", "explain": true, //開啓查詢分析 "query": { "match": { "column2":{ "query": "Waiting Exhale", "operator": "AND" } } } } 複製代碼
或者使用
GET xiaoyu_movie/_search { "profile": "true", "explain": true, "query": { "match": { "column2":{ "query": "Waiting Exhale", "minimum_should_match": 2 } } } } 複製代碼
優化 (使用 constant_score filter 屏蔽評分):
GET xiaoyu_movie/_search { "query": { "constant_score": { "filter": { "match": { "column2": { "query": "Waiting Exhale", "minimum_should_match": 2 } } } } } } 複製代碼
若是想要提高某個子句的排序權重, 能夠設置 boost
GET /xiaoyu_movie/_search { "query": { "match": { "column2":{ "query": "a", "boost": 2 } } } } 複製代碼
若是想對某個條件提高或下降權重, 能夠使用boost, 默認爲1.
GET /xiaoyu_movie/_search { "query": { "bool": { "should": [ { "term": { "column1": { "value": "2376" } } }, { "term": { "column1": { "value": "1" } } } ], "minimum_should_match": 1 //默認爲1,表示最少知足一個條件 } } } 複製代碼
多字段相同查詢, 匹配度20%
GET /xiaoyu_movie/_search { "query": { "multi_match": { "query": "a b c", "fields": ["column1","column2"], "minimum_should_match": "20%" } } } 複製代碼
咱們能夠對 index 起別名, 這樣咱們之後 reindex, 能夠無縫切換了, alials還能夠加過濾條件.
POST /_aliases { "actions": [ { "add": { "index": "xiaoyu_movie", "alias": "alias_yu" } } ] } //使用別名查詢 GET /alias_yu/_search 複製代碼
查找地址中含有河北的數據, 按照age從大到小排序,若是有多段查詢能夠使用 factor 參數加強某查詢語句的影響力
GET /test_score/_search { "query": { "function_score": { "query": { "match": { "address": "河北" } }, "field_value_factor": { "field": "age" }, "boost_mode":"sum", //將算分累加起來, 此處至關於 age+其餘條件分數 "max_boost": 10 //至關於最大age分數爲0 } } } 複製代碼
GET /test_score/_search { "query": { "function_score": { "random_score": { "field":"uid", "seed": 2213132 } } } } 複製代碼
故意輸錯單詞, 會給出推測提示, 可是中文目前無效果
GET /xiaoyu_movie/_search { "suggest": { "address_text": { "text": "Storyi", "term": { "field": "column2" } } } } 複製代碼
首先建立mapping
PUT /test_user { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "name":{ "type":"keyword" }, "age":{ "type": "integer" }, "address":{ "type":"completion" } } } } 複製代碼
設置type爲 completion
檢索
GET /test_user/_search { "suggest": { "complate_test": { "prefix": "河北", "completion":{ "field":"address" } } } } 複製代碼
from size對於深分頁來講是有侷限性的, 這裏咱們使用其餘方式
首先排序, 使用 search_after 指定每次順序
GET /xiaoyu_movie/_search { "query": { "match_all": {} }, "search_after":[ 100194 ], "sort": [ { "column1": { "order": "asc" } } ] } 複製代碼
GET /test_users/_search { "size": 0, "aggs": { "age_agg":{ "terms": { "field": "age", "size": 10 }, "aggs": { "constom_a":{ "sum": { "field": "age" } } } } } } 複製代碼
上面首先根據age進行分組(分桶), 在組內對age進行求和.
GET /test_users/_search { "size": 0, "aggs": { "max_age":{ "max": { "field": "age" } }, "min_age":{ "min": { "field": "age" } }, "avg_age":{ "avg": { "field": "age" } } } } 複製代碼
統計年齡的最大值 最小值和平均值
GET /test_users/_search { "size": 0, "aggs": { "age_agg": { "terms": { "field": "age" }, "aggs": { "constom_a": { "top_hits": { "size": 1, "sort": [ { "grade": { "order": "desc" } } ] } } } } } } 複製代碼
對數據進行聚合, 而後每一個分類按年級倒序取一條
區間統計查詢
GET /test_users/_search { "size": 0, "aggs": { "age_range":{ "range": { "field": "age", "ranges": [ { "key": "<=22", "to": 22 }, { "key":"range 2", "from": 23, "to": 60 }, { "key":">60", "from":61 } ] } } } 複製代碼
特定條件下聚合(對age<=15的數據進行聚合)
GET /test_users/_search { "size": 0, "query": { "range": { "age": { "lte": 15 } } }, "aggs": { "age":{ "terms": { "field": "age" } } } } 複製代碼
當數據分散在多個分片時, 有可能聚合結果是不許確的, 咱們能夠使用 shard_size 指定分片數返回, 可是可能對性能又有較大壓力.
依賴數據庫版本機制, 只能比version版本大才能繼續更新
PUT /xiaoyu_movie/_doc/XwNiNXEBn2UI19J8-2xH ?version=4001&version_type=external { "_doc":{ "column1":"text_a" } } 複製代碼
建立 index
PUT /test_user { "mappings": { "properties": { "name":{ "type":"keyword" }, "address":{ "properties": { "city":{ "type":"text" } } } } } } 複製代碼
建立數據
POST /test_user/_doc/1 { "name":"xiaoyu", "address": { "city":"北京" } } 複製代碼
GET /test_user/_search { "query": { "term": { "address.city": { "value": "北京" } } } } 複製代碼
經過上面term精確檢索失敗,mapping一經建立沒法更改, 這裏須要重建 index
PUT /test_user2 { "mappings": { "properties": { "name":{ "type":"keyword" }, "address":{ "properties": { "city":{ "type":"keyword" } } } } } } 複製代碼
執行同步
POST _reindex { "source": { "index": "test_user" }, "dest": { "index": "test_user2" } } 複製代碼