ElasticSearch 查詢語法
把英文翻譯成中文,讓我覺得很別扭,term,詞項
1、query string search
搜索全部商品:GET /ecommerce/product/_search
took:耗費了幾毫秒
timed_out:是否超時,這裏是沒有
_shards:數據拆成了5個分片,所以對於搜索請求,會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
}
}
]
}
}
query string search的由來,因為search參數都是以http請求的query string來附帶的
搜索商品名稱中包含yagao的商品,而且按照售價降序排序:GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
適用於臨時的在命令行使用一些工具,比如curl,快速的發出請求,來檢索想要的信息;但是如果查詢請求很復雜,是很難去構建的
在生產環境中,幾乎很少使用query string search
---------------------------------------------------------------------------------------------------------------------------------
2、query DSL
DSL:Domain Specified Language,特定領域的語言
http request body:請求體,可以用json的格式來構建查詢語法,比較方便,可以構建各種復雜的語法,比query string search肯定強大多了
查詢所有的商品
GET /ecommerce/product/_search
{
"query": { "match_all": {} }
}
查詢名稱包含yagao的商品,同時按照價格降序排序
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"name" : "yagao"
}
},
"sort": [
{ "price": "desc" }
]
}
分頁查詢商品,總共3條商品,假設每頁就顯示1條商品,現在顯示第2頁,所以就查出來第2個商品
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
指定要查詢出來商品的名稱和價格就可以
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}
更加適合生產環境的使用,可以構建復雜的查詢
---------------------------------------------------------------------------------------------------------------------------------
3、query filter
搜索商品名稱包含yagao,而且售價大於25元的商品
GET /ecommerce/product/_search
{
"query" : {
"bool" : {
"must" : {
"match" : {
"name" : "yagao"
}
},
"filter" : {
"range" : {
"price" : { "gt" : 25 }
}
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
4、full-text search(全文檢索)
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer" : "yagao producer"
}
}
}
盡量,無論是學什麽技術,比如說你當初學java,學linux,學shell,學javascript,學hadoop。。。。一定自己動手,特別是手工敲各種命令和代碼,切記切記,減少復制粘貼的操作。只有自己動手手工敲,學習效果才最好。
producer這個字段,會先被拆解,建立倒排索引
special 4
yagao 4
producer 1,2,3,4
gaolujie 1
zhognhua 3
jiajieshi 2
yagao producer ---> yagao和producer
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.70293105,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.70293105,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 0.25811607,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 0.1805489,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
}
]
}
}
---------------------------------------------------------------------------------------------------------------------------------
5、phrase search(短語搜索)
跟全文檢索相對應,相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引裏面去一一匹配,只要能匹配上任意一個拆解後的單詞,就可以作為結果返回
phrase search,要求輸入的搜索串,必須在指定的字段文本中,完全包含一模一樣的,才可以算匹配,才能作為結果返回
GET /ecommerce/product/_search
{
"query" : {
"match_phrase" : {
"producer" : "yagao producer"
}
}
}
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.70293105,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 0.70293105,
"_source": {
"name": "special yagao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
}
]
}
}
---------------------------------------------------------------------------------------------------------------------------------
6、highlight search(高亮搜索結果)
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer" : "producer"
}
},
"highlight": {
"fields" : {
"producer" : {}
}
}
}
ElasticSearch 查詢語法