新增
PUT /index-name/type-name/id
{
"json數(shù)據(jù)"
}
PUT /consultant_company_list_test1/_doc/1
{
"polar_abbreviation": "企查查",
"consultant_company_position": "北京市",
"consultant_company_admin_id": 3,
"channel_operation_id": "",
"good_at_industry_ids": "",
"is_cancel": 0,
"recommend_count": 0,
"consultant_company_name": "測試公司",
"industry": [ "金融", "銀行" ]
}
// 新增的時候是不需要先創(chuàng)建索引的,創(chuàng)建數(shù)據(jù)的時候,如果索引不存在也會同時把索引創(chuàng)建了。
// 但是這個時候默認創(chuàng)建出來的索引, mapping 是 ES 默認給設(shè)置的,有可能不合適,所以不建議用這種方式創(chuàng)建索引。
// 如果對同一個index/type/id 使用 PUT,哪怕字段不一致,則后面的數(shù)據(jù)會覆蓋前面的數(shù)據(jù)。
// 創(chuàng)建成功后的返回結(jié)果說明:
{
"_index": "consultant_company_list_test1", // 創(chuàng)建的document所屬的index
"_type": "_doc", // 創(chuàng)建的document所屬的type
"_id": "1", // 創(chuàng)建的document的id
"_version": 1, // 創(chuàng)建的document的版本號
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
// 如果我們想要確保創(chuàng)建的 document 是新的,不會覆蓋原來的 document
PUT /consultant_company_list_test1/_doc/1/_create
{
"polar_abbreviation": "企查查",
"consultant_company_position": "北京市",
"consultant_company_admin_id": 3,
"channel_operation_id": "",
"good_at_industry_ids": "",
"is_cancel": 0,
"recommend_count": 0,
"consultant_company_name": "測試公司",
"industry": [ "金融", "銀行" ]
}
刪除
// 刪除索引中的單條數(shù)據(jù)
DELETE /index_one/product/1
// 刪除單個索引
DELETE /index_one
// 刪除多個索引
DELETE /index_one,index_two
DELETE /index_*
// 危險操作,刪除全部索引
DELETE /_all
DELETE /*
刪除全部索引操作非常危險,禁止措施
elasticsearch.yml 做如下配置:
action.destructive_requires_name: true
修改
POST /ecommerce/product/1/_update
{
"doc": {
"name": "jiaqiangban gaolujie yagao"
}
}
1. 修改的過程是 ES 獲取某個文檔中的所有字段,修改指定字段的內(nèi)容,然后把老的 document 標記為deleted;
再重新創(chuàng)建一個新的document
2. update 內(nèi)部會執(zhí)行樂觀鎖的并發(fā)控制策略,每次更新時會更新版本號。
3. 如果遇到并發(fā)更新失敗的時候,會有 retry(重試) 策略
(1)再次獲取 document 數(shù)據(jù)和最新版本號;
(2)基于最新版本號再次去更新,如果成功那么就結(jié)束了;
如果失敗,就重復(fù) 1 和 2 兩個步驟,最多可以重復(fù) retry_on_conflict 參數(shù)指定的次數(shù)
批量增刪改
POST /_bulk
{"delete":{"_index":"test-index", "_type":"test-type", "_id":"1"}}
{"create":{"_index":"test-index", "_type":"test-type", "_id":"2"}}
{"test_field":"test2"}
{"index":{"_index":"test-index", "_type":"test-type", "_id":"1"}}
{"test_field":"test1"}
{"update":{"_index":"test-index", "_type":"test-type", "_id":"3", "_retry_on_conflict":"3"}}
{"doc":{"test_field":"bulk filed 3"}}
// 有哪些類型的操作可以執(zhí)行呢?
(1)delete:刪除一個文檔,只要1個json串就可以了
(2)create:PUT /index/type/id/_create;只創(chuàng)建新文檔
(3)index:普通的put操作,可以是創(chuàng)建文檔,也可以是全量替換文檔
(4)update:執(zhí)行的partial update操作,即 post 更新
// bulk操作中,任意一個操作失敗,是不會影響其他的操作的,但是在返回結(jié)果里,會告訴你異常日志
// bulk size最佳大小: 如果太大的話,性能反而會下降;
// 一般從1000~5000條數(shù)據(jù)開始,嘗試逐漸增加。另外,如果看大小的話,最好是在5~15MB之間;
查詢
查詢指定索引下全部數(shù)據(jù)
GET /consultant_company_list_test1/_search
{
"took": 0, // 耗時
"timed_out": false, // 是否超時
"_shards": {
"total": 1, // 搜索了幾個分片
"successful": 1, // 成功了幾個
"skipped": 0, // 跳過了幾個
"failed": 0 // 失敗了幾個
},
"hits": {
"total": {
"value": 1, // 查詢到的數(shù)據(jù)的總量
"relation": "eq" // 平常索引數(shù)據(jù)量不大的情況下這個數(shù)值沒問題,// 但是當超出 10000 條數(shù)據(jù)的時候,
// 這個 value 就固定為 10000。想要精確匹配數(shù)量,就需要通過 track-total-hits 參數(shù)來設(shè)置
},
"max_score": 1, // 查詢到的數(shù)據(jù)中匹配度分數(shù)最大值
"hits": [ // 查詢到的結(jié)果
{
"_index": "consultant_company_list_test1",
"_type": "_doc",
"_id": "1",
"_score": 1, // 匹配度分數(shù)
"_source": {
"polar_abbreviation": "企查查",
"consultant_company_position": "北京市",
"consultant_company_admin_id": 3,
"channel_operation_id": "",
"good_at_industry_ids": "",
"is_cancel": 0,
"recommend_count": 0,
"consultant_company_name": "測試公司"
}
}
]
}
}
"relation" : "gte"
"relation" : "eq"
gte 表示 total.value 是查詢匹配總命中數(shù)的下限(大于等于)。eq 則表示 total.value 是準確計數(shù)。
查詢指定索引下指定id的數(shù)據(jù)
GET /consultant_company_list_test1/_doc/1
{
"_index": "consultant_company_list_test1",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true, // 查找成功為true,不成功為false
"_source": { // 搜索到的數(shù)據(jù)
"polar_abbreviation": "企查查",
"consultant_company_position": "北京市",
"consultant_company_admin_id": 3,
"channel_operation_id": "",
"good_at_industry_ids": "",
"is_cancel": 0,
"recommend_count": 0,
"consultant_company_name": "測試公司",
"industry": [ "金融", "銀行" ]
}
}
查詢指定索引下全部數(shù)據(jù)
// 查詢指定索引下全部數(shù)據(jù)
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match_all": {}
}
}
查詢指定值(match)
// 查詢指定值
GET /consultant_company_list_test1/_doc/_search
{
"query" :{
"match": {
"consultant_company_name": "測試公司"
}
}
}
在多個字段中查詢指定值(multi_match)
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"multi_match": {
"query": "測試公司1",
"fields": ["consultant_company_name", "polar_abbreviation"]
}
}
}
match、multi_match是模糊匹配,匹配時會對所查找的關(guān)鍵詞進行分詞,然后按分詞匹配查找。
term 查詢
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"term": {
"consultant_company_name": "測試公司"
}
}
}
terms 查詢
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"terms": {
"consultant_company_name": [
"測試公司1",
"測試公司2"
]
}
}
}
term、terms 會直接對關(guān)鍵詞進行查找
范圍查詢
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 30
}
}
}
}
查詢并排序
默認情況下,是按照_score降序排序的
GET /consultant_company_list_test1/_doc/_search
{
"query" :{
"match": {
"consultant_company_name": "測試公司1"
}
},
"sort": {
"id": {
"order": "desc"
}
}
}
分頁查詢
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match_all": {}
},
"from": 0, // 從第幾條數(shù)據(jù)開始
"size": 1 // 查幾條數(shù)據(jù)
}
查詢指定字段
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match_all": {}
},
"_source": ["id", "consultant_company_name"]
}
// 可以使用通配符*,顯示要的字段、去除不需要的字段
GET student/_search
{
"query":{
"match_all": {}
},
"_source":{
"includes": "addr*",
"excludes": ["name","bir*"]
}
}
全文檢索
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match": {
"producer": "測試公司 1"
}
}
}
phrase search(短語搜索)
跟全文檢索相相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引里面去一一匹配,只要能匹配上任意一個拆解后的單詞,就可以作為結(jié)果返回。phrase search,要求輸入的搜索串,必須在指定的字段文本中,完全包含一模一樣的,才可以算匹配,才能作為結(jié)果返回。
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match_phrase": {
"producer": "測試公司 1"
}
}
}
highlight search(高亮搜索結(jié)果)
GET /consultant_company_list_test1/_doc/_search
{
"query": {
"match": {
"consultant_company_name": "測試公司"
}
},
"highlight": {
"fields": {
"consultant_company_name":{} // 指定高亮的字段
}
}
}
通配符查詢
* 代表0個或多個字符
? 代表任意一個字符
GET /consultant_company_list_test1/_doc/_search
{
"query":{
"wildcard":{
"consultant_company_name":"測試公司?"
}
}
}
正則表達式查詢
GET /consultant_company_list_test1/_doc/_search
{
"query":{
"regex":{
"title":{
"consultant_company_name":"測試公司[12]"
}
}
}
}
聚合搜索(很多種聚合方式,舉幾個例子)
term 分組統(tǒng)計
// 統(tǒng)計各個品牌的商品數(shù)量(只要分組就好了,自帶統(tǒng)計數(shù)量)
GET /goods/_search
{
"aggs": {
"brand_aggs" : {
"terms" : {
"field" : "brand"
}
}
}
}
// 統(tǒng)計各個品牌下的平均手機價格
GET /goods/_search
{
"size" : 0,
"aggs" : {
"brand_aggs" : {
"terms" : {
"field" : "brand"
},
"aggs":{
"avg_price": {
"avg": { // 還有其它聚合方式 sum,max,min
"field": "price"
}
}
}
}
}
}
// 統(tǒng)計各個品牌下的平均手機價格,并且按照平均價格降序排序
GET /goods/_search
{
"size" : 0,
"aggs" : {
"brand_aggs" : {
"terms" : {
"field" : "brand",
"order": {
"avg_price": "desc"
}
},
"aggs":{
"avg_price": {
"avg": { // 還有其它聚合方式 sum,max,min
"field": "price"
}
}
}
}
}
}
histogram 分組統(tǒng)計
// 按照500為一個階梯統(tǒng)計不同價位手機數(shù)量
GET /goods/_search
{
"size":0,
"aggs":{
"price_histogram":{
"histogram": {
"field": "price",
"interval": 500 // 間隔
}
}
}
}
range 分組統(tǒng)計
// 統(tǒng)計價格在4000-6000手機的數(shù)量
GET /goods/_search
{
"size": 0,
"aggs": {
"price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 4000,
"to": 6000
}
]
}
}
}
}
// 統(tǒng)計價格在4000-6000手機的數(shù)量,然后在每組內(nèi)再按照tag進行分組,最后再計算每組的平均價格
GET /goods/_search
{
"size": 0,
"aggs": {
"price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 4000,
"to": 6000
}
]
},
"aggs": {
"group_by_tag": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
日期分組統(tǒng)計
GET /cars/_search
{
"size":0,
"aggs" : {
"date" : {
"date_histogram" : {
// 需要聚合分組的字段名稱, 類型需要為date, 格式?jīng)]有要求
"field": "sold",
// 按什么時間段聚合, 這里是5分鐘;分鐘 (1m)、小時 (1h)、天 (1d)、星期 (1w)、月 (1M)、季度 (1q)、年 (1y)
"interval": "5m",
// 設(shè)置時區(qū), 這樣就相當于東八區(qū)的時間
"time_zone":"+08:00",
// 返回值格式化,HH大寫,不然不能區(qū)分上午、下午
"format": "yyyy-MM-dd HH",
// 為空的話則填充0
"min_doc_count": 0,
// 需要填充0的范圍
"extended_bounds": {
"min": 1533556800000,
"max": 1533806520000
}
}
}
}
}
組合條件查詢
bool:
must => 必須匹配 // 相當于MySQL的 and
must_not => 必須不匹配 // 相當于MySQL的 not
should => 應(yīng)該匹配,既可以有,也可以沒有 // 相當于MySQL的 or
filter => 過濾
// title必須包含elasticsearch,content可以包含elasticsearch也可以不包含,author_id必須不為111
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content": "elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 111
}
}
]
}
}
}
SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
GET /my_store/products/_search
{
"query" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
filter 與 query
1. 默認情況下,Elasticsearch 根據(jù) 相關(guān)性評分(relevance score) 對匹配到的搜索結(jié)果進行排序,
這個分數(shù)用來衡量每個文檔與查詢的匹配程度。
2. 相關(guān)性分數(shù)是一個正浮點數(shù),在 search API 返回結(jié)果的 _score 元字段中。 _score越高,文檔的相關(guān)性就越強。
3. 在 query 上下文中, 除了決定文檔是否匹配之外,查詢子句還計算文檔的相關(guān)性得分(在元字段 _score 中顯示)。
4. 在 filter 上下文中, 結(jié)果是簡單的"是"或"否"--不會計算分數(shù)。
5. Elasticearch 會自動緩存經(jīng)常使用的 filter,以提高性能。因此,filter速度要快于query
6. 一般來說,如果你是在進行搜索,需要將最匹配搜索條件的數(shù)據(jù)先返回,那么用query;
如果你只是要根據(jù)一些條件篩選出一部分數(shù)據(jù),不關(guān)注其排序,那么用filter。
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
對上面的例子分析下:
query 參數(shù)表示整個語句是處于 query context 中
bool 和 match 語句被用在 query context 中,也就是說它們會計算每個文檔的匹配度(_score)
filter 參數(shù)則表示這個子查詢處于 filter context 中
filter 語句中的 term 和 range 語句用在 filter context 中,它們只起到過濾的作用,并不會計算文檔的得分。
批量查詢
// 查詢不同index下的數(shù)據(jù)
GET /_mget
{
"docs": [
{
"_index": "test-index",
"_type": "test-type",
"_id": 1
},
{
"_index": "ecommerce",
"_type": "product",
"_id": 2
}
]
}
// 查詢的document是一個index下的不同type
GET /test_index/_mget
{
"docs" : [
{
"_type" : "test_type",
"_id" : 1
},
{
"_type" : "test_type",
"_id" : 2
}
]
}
// 查詢的數(shù)據(jù)都在同一個index下的同一個type
GET /ecommerce/product/_mget
{
"ids": [1,2]
}
scoll 滾動查詢
scoll滾動查詢就是,一批一批的查,直到所有數(shù)據(jù)都查詢完處理完。
例如:如果一次性要查出來比如10萬條數(shù)據(jù),那么性能會很差,此時使用scoll滾動搜索,可以先搜索一批數(shù)據(jù),然后下次再搜索一批數(shù)據(jù),以此類推,直到搜索出全部的數(shù)據(jù)來。
scoll搜索會在第一次搜索的時候,保存一個當時的視圖快照,之后只會基于該舊的視圖快照提供數(shù)據(jù)搜索,如果這個期間數(shù)據(jù)變更,是不會讓用戶看到的。
每次發(fā)送scroll請求,我們還需要指定一個scoll參數(shù),指定一個時間窗口,每次搜索請求只要在這個時間窗口內(nèi)能完成就可以了。
GET /test_index/test_type/_search?scroll=1m
{
"query": {
"match_all": {}
},
"sort": [ "_doc" ],
"size": 3
}
// 搜索結(jié)果會有一個scoll_id,下一次再發(fā)送scoll請求的時候,必須帶上這個scoll_id,這樣才能接著查詢下一批數(shù)據(jù)。
{
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAACxeFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAAsYBY0b25zVFlWWlRqR3ZJajlfc3BXejJ3AAAAAAAALF8WNG9uc1RZVlpUakd2SWo5X3NwV3oydwAAAAAAACxhFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAAsYhY0b25zVFlWWlRqR3ZJajlfc3BXejJ3",
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10,
"max_score": null,
"hits": [
{}
]
}
}
// 查詢下一批數(shù)據(jù)
GET /_search/scroll
{
"scroll": "1m",
"scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAACxeFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAAsYBY0b25zVFlWWlRqR3ZJajlfc3BXejJ3AAAAAAAALF8WNG9uc1RZVlpUakd2SWo5X3NwV3oydwAAAAAAACxhFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAAsYhY0b25zVFlWWlRqR3ZJajlfc3BXejJ3"
}
scroll + bulk 可以實現(xiàn)快速重建索引
multi-index 和 multi-type 搜索模式
GET /_search:所有索引,所有type下的所有數(shù)據(jù)都搜索出來
GET /index1/_search:指定一個index,搜索其下所有type的數(shù)據(jù)
GET /index1,index2/_search:同時搜索兩個index下的數(shù)據(jù)
GET /*1,*2/_search:按照通配符去匹配多個索引
GET /index1/type1/_search:搜索一個index下指定的type的數(shù)據(jù)
GET /index1/type1,type2/_search:可以搜索一個index下多個type的數(shù)據(jù)
GET /index1,index2/type1,type2/_search:搜索多個index下的多個type的數(shù)據(jù)
GET /_all/type1,type2/_search:_all,可以搜索所有index下的指定type的數(shù)據(jù)
索引別名
索引別名可以指向一個或多個索引,并且可以在任何需要索引名稱的API中使用。
索引別名允許我們執(zhí)行以下操作:
1)索引重建時,可以把正在運行的索引和新的索引進行無縫切換,無需停機更新;
2)對多個索引進行分組組合(例如,last_three_months的索引別名:是過去3個月索引 logstash_201903, logstash_201904, logstash_201905的組合);
單個設(shè)置索引別名的方式
PUT /index1/_alias/index
一次設(shè)置多個索引別名的方式
POST /_aliases
{
"actions": [
{
"add": {
"index": "index1",
"alias": "index",
"is_write_index": true // 控制是否可以通過索引別名寫入數(shù)據(jù)
}
},
{
"add": {
"index": "index2",
"alias": "index"
}
}
]
}
刪除別名
POST /_aliases
{
"actions": [
{
"remove": {
"index": "index2",
"alias": "index"
}
}
]
}
查詢指定索引的別名
GET /index1/_alias
查詢所有索引的別名
GET /_alias
假設(shè)沒有別名,如何處理多索引檢索?
GET /index1,index2/_search
有了別名后,多索引檢索操作就變得簡單了
1. 先為不同的索引設(shè)置相同的別名
POST /_aliases
{
"actions": [
{
"add": {
"index": "index1",
"alias": "index"
}
},
{
"add": {
"index": "index2",
"alias": "index"
}
}
]
}
2. 使用別名檢索
GET /index/_search
校驗查詢語句是否合法
// 語法: GET /index/type/_validate/query?explain
// 一般用在那種特別復(fù)雜龐大的搜索下,比如你一下子寫了上百行的搜索,這個時候可以先用validate api去驗證一下,語法是否合法
GET /test_index/test_type/_validate/query?explain
{
"query": {
"math": {
"test_field": "test"
}
}
}
ES其它一些常用語句
查看索引
查看所有索引
GET /_cat/indices
查看某個索引的狀態(tài)
GET /_cat/indices/index-name
查看某個索引的 mapping
GET /index-name/_mapping
查看、修改索引設(shè)置文章來源:http://www.zghlxwxcb.cn/news/detail-779319.html
GET index-name/_settings
PUT index-name/_settings
{
"number_of_replicas": 1
}
開啟、關(guān)閉索引文章來源地址http://www.zghlxwxcb.cn/news/detail-779319.html
關(guān)閉一個索引(關(guān)閉索引后不能在讀寫 索引中的文檔)
POST index-name/_close
開啟一個索引(將一個關(guān)閉的索引從新開啟)
POST index-name/_open
到了這里,關(guān)于Elasticsearch 常用的增加、刪除、修改、查詢語句的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!