Es提供了基於JSON的完整查詢DSL(Domain Specific Language 特定域的語言)來定義查詢。將查詢DSL視為查詢的AST(抽象語法樹)。它由兩種子句組成:
葉子查詢子句,在特定域中尋找特定的值,如match、term或range查詢
複合查詢子句包裝其他葉子查詢或複合查詢,並用於以邏輯方式組合多個查詢。如bool、dis_max、constant_score查詢
POST /索引名稱/_search
{
"query":{
"match_all": {}
}
}
查詢結果範例:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test-demo1",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "百度3",
"job" : "運營",
"amt" : "3000.34",
"logo" : "http://www.lgstatic.com/ttasdf2",
"createTime" : "20220303230000"
}
}
...省略2條資料
]
}
}
全文搜尋能夠搜尋已分析的文字欄位,如電子郵件正文、商品描述等。
先造一些測試資料:
PUT /item
{
"settings": {},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword"
},
"price": {
"type": "float"
}
}
}
}
POST /item/_doc/
{
"title": "小米電視4A",
"images": "https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202206/12479122ahwuydsoin1.jpg",
"price": 4288
}
POST /item/_doc/
{
"title": "小米手機",
"images": "https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202206/12479122ahwuydsoin1.jpg",
"price": 2688
}
POST /item/_doc/
{
"title": "蘋果手機",
"images": "https://s3.ap-northeast-1.wasabisys.com/img.tw511.com/202206/12479122ahwuydsoin1.jpg",
"price": 5699
}
match型別的查詢,會把查詢條件分詞,多個詞條之間是or的關係。如下面的例子,會根據小米和手機分別去搜尋,能搜出3條資料。
POST /item/_search
{
"query":{
"match": {
"title": "小米手機"
}
}
}
POST /item/_search
{
"query":{
"match": {
"title": {
"query": "小米手機",
"operator":"and"
}
}
}
}
match_phrase查詢用來對一個欄位進行短語查詢,可以指定analyzer、slop移動因子
POST /item/_search
{
"query":{
"match_phrase": {
"title": "小米手機"
}
}
}
帶slop:
POST /item/_search
{
"query":{
"match_phrase": {
"title": {
"query": "手機小米",
"slop":2
}
}
}
}
slop引數告訴match_phrase查詢詞條能夠相隔多遠時仍然將檔案視為匹配。相隔多遠的意思是,你需要移動一個詞條多少次來讓查詢和檔案匹配
query string提供了無需指定某欄位而對檔案全文進行匹配查詢的一個高階查詢,同時可以指定在哪些欄位上進行匹配。
GET /item/_search
{
"query": {
"query_string": {
"query": "2688"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "price",
"query": "2688"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "手機 OR 小米"
}
}
}
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "手機 and 小米"
}
}
}
#模糊查詢
GET /item/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "小米~1"
}
}
}
#多欄位支援
GET /item/_search
{
"query": {
"query_string": {
"fields": ["title","price"],
"query": "2699"
}
}
}
如果你需要在多個欄位上進行文字搜尋,可用multi_match。
GET /item/_search
{
"query": {
"multi_match": {
"query": "2688",
"fields": ["title","price"]
}
}
}
#還可以使用*設定
GET /item/_search
{
"query": {
"multi_match": {
"query": "2688",
"fields": ["title","pri*"]
}
}
}
可以使用term-level queries根據結構化資料中的精確值查詢檔案。term-level queries不分析搜尋詞。搜尋詞與儲存在欄位中的詞需要完全匹配
用於查詢指定欄位包含某個搜尋詞的檔案
POST /item/_search
{
"query": {
"term": {
"title":"小米"
}
}
}
POST /item/_search
{
"query": {
"terms": {
"title": ["小米","電視"]
}
}
}
POST /item/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 3000
}
}
}
}
#日期範圍
POST /item/_search
{
"query": {
"range": {
"createTime": {
"gte": "2022-01-01",
"lte": "2022-02-01",
"format": "yyyy-MM-dd"
}
}
}
}
GET /item/_search
{
"query": {
"exists": {
"field": "price"
}
}
}
GET /item/_search
{
"query": {
"prefix": {
"title": {
"value": "小米"
}
}
}
}
GET /item/_search
{
"query": {
"wildcard": {
"title":"小*"
}
}
}
GET /item/_search
{
"query": {
"regexp": {
"title":"小米[a-z0-9]"
}
}
}
GET /item/_search
{
"query": {
"fuzzy": {
"title": "手機"
}
}
}
#錯別字糾正
GET /item/_search
{
"query": {
"fuzzy": {
"title": {
"value": "大米",
"fuzziness": 1
}
}
}
}
GET /item/_search
{
"query": {
"ids": {
"values": ["t76YgYEB9TD2fYkcLzha","tb6XgYEB9TD2fYkc6zhx"]
}
}
}
GET /item/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"title": "小米"
}
},
"boost": 1.2
}
}
}
POST /item/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
}
], "filter": {
"term": {
"title": "電視"
}
},"must_not": [
{
"range": {
"price": {
"gte": 4200,
"lte": 4300
}
}
}
]
,"minimum_should_match": 0
}
}
}
minimum_should_match代表了最小匹配精度,如果設定為1,代表should語句中至少需要有一個條件滿足。
預設情況下,返回的結果是按照相關性進行排序的。預設排序是_score降序
# 按照評分升序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"_score":{
"order":"asc"
}
}
]
}
#根據欄位值排序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"price":{
"order":"asc"
}
}
]
}
#多個欄位的排序
GET /item/_search
{
"query": {
"match_all": {}
},
"sort":[{
"price":{
"order":"asc"
}
},{
"createTime": {
"order":"desc"
}
}
]
}
size:每頁顯示多少條
from:當前頁起始索引
POST /item/_search
{
"query": {
"match_all": {}
}
,"size": 2,
"from": 0
}
POST /item/_search
{
"query": {
"match": {
"title": "小米"
}
},
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [{"title":{}}]
}
}
不同的索引
GET /_mget
{
"docs":[
{
"_index":"item",
"_id":"tb6XgYEB9TD2fYkc6zhx"
},
{
"_index":"test-location",
"_id":1
}
]
}
相同的索引
POST /test-location/_search
{
"query": {
"ids": {
"values": ["1","2"]
}
}
}
語法:
POST /_bulk
{"action": {"metadata"}}
{"data"}
範例:
POST /_bulk
{"delete":{"_index":"item","_id":"tb6XgYEB9TD2fYkc6zhx"}}
{"create":{"_index":"item","_id":"1"}}
{"title":"華為電腦","price":2333}
{"update":{"_index":"item","_id":2}}
{"doc":{"title":"冰箱"}}
格式:每個json不能換行,相鄰json必須換行
隔離:每個操作互不影響,操作失敗的行會返回其失敗資訊
實際用法:bulk請求一次不要太大,否則一下積壓到記憶體中,效能會下降。所以,一次請求幾千個操作、大小在幾M正好。bulk會將要處理的資料載入記憶體中,所以資料量是有限的,最佳的資料量不是一個確定的資料,它取決於你的硬體,你的檔案大小以及複雜性,你的索引以及搜尋的負載。一般建議是1000-5000個檔案,大小建議是5-15MB,預設不能超過100M,可以在es的組態檔(ES的config下的elasticsearch.yml)中設定。
http.max_content_length: 10mb