ElasticSearch的各種查詢
ElasticSearch多種搜尋方式
1、query string search
2、query DSL
3、queryfilter
4、full-tex search
5、phrase search
6、highlight search
1、query string search
搜尋全部資訊
格式:GET /index/type/_search
GET /employee/user/_search
took:耗時幾毫秒 timed_out:是否超時,這裡是沒有
_shareds:資料拆分了5個分片,所以對於搜尋請求,會打到所有的primary shard(或者是它的某個replica shard也可以) hits.total:查詢結果的數量,2表示document
hits.max_score:score的含義,就是document對於一個search相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜尋的document的詳細資料
結果:
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "employee", "_type": "user", "_id": "2", "_score": 1, "_source": { "name": "lisi", "sez": "M", "china": 90, "addr": "beijing", "join": [ "chengdu", "shenzhen", "wuhan" ] } }, { "_index": "employee", "_type": "user", "_id": "3", "_score": 1, "_source": { "name": "wangwu", "sez": "M", "china": 60, "addr": "xian", "join": [ "xiamen", "nanning", "changsha" ] } } ] } }
這個查詢還可以帶上一些引數,例如,搜尋名字是"lisi"的人,而且按照語文成績降序排序
GET /employee/user/_search?q=name:lisi&sort=china:desc
這種操作在生產使用比較少,因為複雜的查詢很難去構建請求
2、query DSL
DSL:Domain Specified Language,特定領域的語言
http request body:請求體,可以用json的格式來構建查詢語法,比較方便,可以構建各種複雜的語法
查詢所有的同學
GET /employee/user/_search { "query": { "match_all": {} } }
結果
{
"took": 91,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
........
}
}
]
}
}
查詢名字包含lisi的同學,同時按照語文成績降序排序
GET /employee/user/_search
{
"query": {
"match": {
"name": "lisi"
}
},
"sort": [
{
"china": {
"order": "desc"
}
}
]
}
分頁查詢同學,總共3個同學,假設每頁就顯示1條同學,現在顯示第2頁,所以就查出來第2個同學
GET /employee/user/_search
{
"query": {
"match_all": {}
},
"from": 1
, "size": 1
}
指定要查詢指定同學名稱和成績就可以
GET /employee/user/_search
{
"query": {"match_all": {}},
"_source": ["name","china"]
}
3、query filter
對資料進行過濾
查詢所有學生中名字是lisi的,成績大於60的記錄
must是必須匹配的
GET /employee/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "lisi"
}
}
],
"filter": {
"range": {
"china": {
"gt": 60
}
}
}
}
}
}
4、full-text search(全文檢索)
先構建倒排索引,在用查詢的值拆詞以後去匹配,score表示相似度,值越大,相似度越高
GET /employee/user/_search
{
"query": {
"match": {
"join": "shenzhen beijing"
}
}
}
結果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "employee",
"_type": "user",
"_id": "5",
"_score": 0.5753642,
"_source": {
"name": "lisi",
"sez": "F",
"china": 60,
"addr": "beijing",
"join": [
"chengdu",
"shenzhen",
"beijing"
]
}
},
{
"_index": "employee",
"_type": "user",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "lisi",
"sez": "F",
"china": 70,
"addr": "beijing",
"join": [
"chengdu",
"shenzhen",
"wuhan"
]
}
}
]
}
}
5、phrase search(短語搜尋)
跟全文檢索相對應,相反,全文檢索會將輸入的搜尋串拆解開來,去倒排索引裡面去一一匹配,只要能匹配上任意一個拆解後的單詞,就可以作為結果返回
phrase search,要求輸入的搜尋串,必須在指定的欄位文字中,完全包含一模一樣的,才可以算匹配,才能作為結果返回
GET /ecommerce/product/_search
{
"query" : {
"match_phrase" : {
"producer" : "yagao producer"
}
}
}
6、highlight search(高亮搜尋結果)
GET /employee/user/_search
{
"query": {
"match": {
"name": "lisi"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}