1. 程式人生 > >Elasticsearch幾種查詢語法

Elasticsearch幾種查詢語法

1、query string search
2、query DSL
3、query filter
4、full-text search
5、phrase search
6、highlight search

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, # 從第1個開始,es從0開始計數的
  "size": 100  # 往後查詢100個
}

指定要查詢出來商品的名稱和價格就可以

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"
        }
    }
}

  1. producer這個欄位,會先被拆解,建立倒排索引
		special		4
		yagao		4
		producer	1,2,3,4
		gaolujie	1
		zhognhua	3
		jiajieshi	2
  1. 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" : {}
        }
    }
}