elasticsearch系列四:搜索詳解(搜索API、Query DSL)

1、搜索API

 

1. 搜索API 端點地址

從索引tweet裏面搜索字段user爲kimchy的記錄html

GET /twitter/_search?q=user:kimchy

從索引tweet,user裏面搜索字段user爲kimchy的記錄java

GET /twitter/tweet,user/_search?q=user:kimchy
GET /kimchy,elasticsearch/_search?q=tag:wow

從全部索引裏面搜索字段tag爲wow的記錄node

GET /_all/_search?q=tag:wow
GET /_search?q=tag:wow

說明:搜索的端點地址能夠是多索引多mapping type的。搜索的參數可做爲URI請求參數給出,也可用 request body 給出spring

2. URI Search

URI 搜索方式經過URI參數來指定查詢相關參數。讓咱們能夠快速作一個查詢。數據庫

GET /twitter/_search?q=user:kimchy

可用的參數請參考: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.htmlapi

3. 查詢結果說明

5. 特殊的查詢參數用法

 若是咱們只想知道有多少文檔匹配某個查詢,能夠這樣用參數:數組

GET /bank/_search?q=city:b*&size=0

 

 

 

 若是咱們只想知道有沒有文檔匹配某個查詢,能夠這樣用參數:緩存

GET /bank/_search?q=city:b*&size=0&terminate_after=1

 

 

 

 比較兩個查詢的結果能夠知道第一個查詢返回全部的命中文檔數,第二個查詢因爲只須要知道有沒有文檔,因此只要有文檔就當即返回併發

 6. Request body Search

 Request body 搜索方式以JSON格式在請求體中定義查詢 query。請求方式能夠是 GET 、POST 。app

GET /twitter/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

可用的參數:

timeout:請求超時時長,限定在指定時長內響應(即便沒查完);
from: 分頁的起始行,默認0;
size:分頁大小;
request_cache:是否緩存請求結果,默認true。
terminate_after:限定每一個分片取幾個文檔。若是設置,則響應將有一個布爾型字段terminated_early來指示查詢執行是否實際已經terminate_early。缺省爲no terminate_after;
search_type:查詢的執行方式,可選值dfs_query_then_fetch or query_then_fetch ,默認: query_then_fetch ;
batched_reduce_size:一次在協調節點上應該減小的分片結果的數量。若是請求中的潛在分片數量可能很大,則應將此值用做保護機制以減小每一個搜索請求的內存開銷。

6.1 query 元素定義查詢

query 元素用Query DSL 來定義查詢。

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

6.2 指定返回哪些內容

6.2.1 source filter  對_source字段進行選擇

GET /_search
{
    "_source": false,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

通配符查詢

GET /_search
{
    "_source": [ "obj1.*", "obj2.*" ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
GET /_search { "_source": "obj.*", "query" : { "term" : { "user" : "kimchy" } } }

包含什麼不包含什麼

GET /_search
{
    "_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

6.2.2 stored_fields 來指定返回哪些stored字段

GET /_search
{
    "stored_fields" : ["user", "postDate"],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

說明:* 可用來指定返回全部存儲字段

6.2.3 docValue Field 返回存儲了docValue的字段值

GET /_search
{
    "query" : {
        "match_all": {}
    },
    "docvalue_fields" : ["test1", "test2"]
}

6.2.4 version 來指定返回文檔的版本字段

GET /_search
{
    "version": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

6.2.5 explain 返回文檔的評分解釋

GET /_search
{
    "explain": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

6.2.6 Script Field 用腳原本對命中的每一個文檔的字段進行運算後返回

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "doc['balance'].value * 2"
      }
    },
    "test2": {
      "script": {
        "lang": "painless",
        <!--  doc指文檔-->
        "source": "doc['age'].value * params.factor",
        "params": {
          "factor": 2
        }
      }
    } }}

搜索結果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "fields": {
          "test1": [
            81080
          ],
          "test2": [
            78
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "test1": [
            68974
          ],
          "test2": [
            74
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "fields": {
          "test1": [
            94318
          ],
          "test2": [
            78
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "fields": {
          "test1": [
            98444
          ],
          "test2": [
            56
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "fields": {
          "test1": [
            7214
          ],
          "test2": [
            78
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "fields": {
          "test1": [
            94812
          ],
          "test2": [
            64
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "fields": {
          "test1": [
            28446
          ],
          "test2": [
            52
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "fields": {
          "test1": [
            6300
          ],
          "test2": [
            60
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "fields": {
          "test1": [
            81520
          ],
          "test2": [
            52
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "fields": {
          "test1": [
            29528
          ],
          "test2": [
            72
          ]
        }
      }
    ]
  }
}
View Code
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ffx": {
      "script": {
        "lang": "painless",
        "source": "doc['age'].value * doc['balance'].value"
      }
    },
    "balance*2": {
      "script": {
        "lang": "painless",
        "source": "params['_source'].balance*2"
      }
    }
  }
}

說明:

params  _source 取 _source字段值

官方推薦使用doc,理由是用doc效率比取_source 高

搜索結果:

{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "fields": {
          "balance*2": [
            81080
          ],
          "ffx": [
            1581060
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "balance*2": [
            68974
          ],
          "ffx": [
            1276019
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "fields": {
          "balance*2": [
            94318
          ],
          "ffx": [
            1839201
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "fields": {
          "balance*2": [
            98444
          ],
          "ffx": [
            1378216
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "fields": {
          "balance*2": [
            7214
          ],
          "ffx": [
            140673
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "fields": {
          "balance*2": [
            94812
          ],
          "ffx": [
            1516992
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "fields": {
          "balance*2": [
            28446
          ],
          "ffx": [
            369798
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "fields": {
          "balance*2": [
            6300
          ],
          "ffx": [
            94500
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "fields": {
          "balance*2": [
            81520
          ],
          "ffx": [
            1059760
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "fields": {
          "balance*2": [
            29528
          ],
          "ffx": [
            531504
          ]
        }
      }
    ]
  }
}
View Code

6.2.7 min_score  限制最低評分得分

GET /_search
{
    "min_score": 0.5,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

6.2.8 post_filter  後置過濾:在查詢命中文檔、完成聚合後,再對命中的文檔進行過濾。

如:要在一次查詢中查詢品牌爲gucci且顏色爲紅色的shirts,同時還要獲得gucci品牌各顏色的shirts的分面統計。

建立索引並指定mappping:

PUT /shirts
{
    "mappings": {
        "_doc": {
            "properties": {
                "brand": { "type": "keyword"},
                "color": { "type": "keyword"},
                "model": { "type": "keyword"}
            }
        }
    }
}

往索引裏面放入文檔即相似數據庫裏面的向表插入一行數據,並當即刷新

PUT /shirts/_doc/1?refresh
{
    "brand": "gucci",
    "color": "red",
    "model": "slim"
}
PUT /shirts/_doc/2?refresh
{
    "brand": "gucci",
    "color": "green",
    "model": "seec"
}

執行查詢:

GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } 
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } 
    }
  },
  "post_filter": { 
    "term": { "color": "red" }
  }
}

查詢結果

{
  "took": 109,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "shirts",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "brand": "gucci",
          "color": "red",
          "model": "slim"
        }
      }
    ]
  },
  "aggregations": {
    "colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "green",
          "doc_count": 1
        },
        {
          "key": "red",
          "doc_count": 1
        }
      ]
    }
  }
}

6.2.9 sort  排序

能夠指定按一個或多個字段排序。也可經過_score指定按評分值排序,_doc 按索引順序排序。默認是按相關性評分從高到低排序。

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [ { "age": { "order": "desc" } }, { "balance": { "order": "asc" } }, "_score" ]
}

說明:

order 值:asc、desc。若是不給定,默認是asc,_score默認是desc

查詢結果:

{
  "took": 181,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "549",
        "_score": 1,
        "_source": {
          "account_number": 549,
          "balance": 1932,
          "firstname": "Jacqueline",
          "lastname": "Maxwell",
          "age": 40,
          "gender": "M",
          "address": "444 Schenck Place",
          "employer": "Fuelworks",
          "email": "jacquelinemaxwell@fuelworks.com",
          "city": "Oretta",
          "state": "OR"
        },
        "sort": [
          40,
          1932,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "306",
        "_score": 1,
        "_source": {
          "account_number": 306,
          "balance": 2171,
          "firstname": "Hensley",
          "lastname": "Hardin",
          "age": 40,
          "gender": "M",
          "address": "196 Maujer Street",
          "employer": "Neocent",
          "email": "hensleyhardin@neocent.com",
          "city": "Reinerton",
          "state": "HI"
        },
        "sort": [
          40,
          2171,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "960",
        "_score": 1,
        "_source": {
          "account_number": 960,
          "balance": 2905,
          "firstname": "Curry",
          "lastname": "Vargas",
          "age": 40,
          "gender": "M",
          "address": "242 Blake Avenue",
          "employer": "Pearlesex",
          "email": "curryvargas@pearlesex.com",
          "city": "Henrietta",
          "state": "NH"
        },
        "sort": [
          40,
          2905,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "584",
        "_score": 1,
        "_source": {
          "account_number": 584,
          "balance": 5346,
          "firstname": "Pearson",
          "lastname": "Bryant",
          "age": 40,
          "gender": "F",
          "address": "971 Heyward Street",
          "employer": "Anacho",
          "email": "pearsonbryant@anacho.com",
          "city": "Bluffview",
          "state": "MN"
        },
        "sort": [
          40,
          5346,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "567",
        "_score": 1,
        "_source": {
          "account_number": 567,
          "balance": 6507,
          "firstname": "Diana",
          "lastname": "Dominguez",
          "age": 40,
          "gender": "M",
          "address": "419 Albany Avenue",
          "employer": "Ohmnet",
          "email": "dianadominguez@ohmnet.com",
          "city": "Wildwood",
          "state": "TX"
        },
        "sort": [
          40,
          6507,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "938",
        "_score": 1,
        "_source": {
          "account_number": 938,
          "balance": 9597,
          "firstname": "Sharron",
          "lastname": "Santos",
          "age": 40,
          "gender": "F",
          "address": "215 Matthews Place",
          "employer": "Zenco",
          "email": "sharronsantos@zenco.com",
          "city": "Wattsville",
          "state": "VT"
        },
        "sort": [
          40,
          9597,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "810",
        "_score": 1,
        "_source": {
          "account_number": 810,
          "balance": 10563,
          "firstname": "Alyssa",
          "lastname": "Ortega",
          "age": 40,
          "gender": "M",
          "address": "977 Clymer Street",
          "employer": "Eventage",
          "email": "alyssaortega@eventage.com",
          "city": "Convent",
          "state": "SC"
        },
        "sort": [
          40,
          10563,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "302",
        "_score": 1,
        "_source": {
          "account_number": 302,
          "balance": 11298,
          "firstname": "Isabella",
          "lastname": "Hewitt",
          "age": 40,
          "gender": "M",
          "address": "455 Bedford Avenue",
          "employer": "Cincyr",
          "email": "isabellahewitt@cincyr.com",
          "city": "Blanford",
          "state": "IN"
        },
        "sort": [
          40,
          11298,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "792",
        "_score": 1,
        "_source": {
          "account_number": 792,
          "balance": 13109,
          "firstname": "Becky",
          "lastname": "Jimenez",
          "age": 40,
          "gender": "F",
          "address": "539 Front Street",
          "employer": "Isologia",
          "email": "beckyjimenez@isologia.com",
          "city": "Summertown",
          "state": "MI"
        },
        "sort": [
          40,
          13109,
          1
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "495",
        "_score": 1,
        "_source": {
          "account_number": 495,
          "balance": 13478,
          "firstname": "Abigail",
          "lastname": "Nichols",
          "age": 40,
          "gender": "F",
          "address": "887 President Street",
          "employer": "Enquility",
          "email": "abigailnichols@enquility.com",
          "city": "Bagtown",
          "state": "NM"
        },
        "sort": [
          40,
          13478,
          1
        ]
      }
    ]
  }
}
View Code

結果中每一個文檔會有排序字段值給出

 "hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "549",
        "_score": 1,
        "_source": {
          "account_number": 549,
          "balance": 1932, "age": 40, "state": "OR"
        },
        "sort": [ 40, 1932, 1 ]    }

 

多值字段排序

對於值是數組或多值的字段,也可進行排序,經過mode參數指定按多值的:

PUT /my_index/_doc/1?refresh
{
   "product": "chocolate",
   "price": [20, 4]
}

POST /_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
      {"price" : {"order" : "asc", "mode" : "avg"}}
   ]
}

 Missing values  缺失該字段的文檔

missing 的值能夠是 _last, _first

GET /_search
{
    "sort" : [
        { "price" : {"missing" : "_last"} }
    ],
    "query" : {
        "term" : { "product" : "chocolate" }
    }
}

 地理空間距離排序

官方文檔:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#geo-sorting

GET /_search
{
    "sort" : [ { "_geo_distance" : { "pin.location" : [-70, 40], "order" : "asc", "unit" : "km", "mode" : "min", "distance_type" : "arc" } } ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

參數說明:

_geo_distance 距離排序關鍵字
pin.location是 geo_point 類型的字段
distance_type:距離計算方式 arc球面 、plane 平面。
unit: 距離單位 km 、m 默認m

Script Based Sorting 基於腳本計算的排序

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    },
    "sort" : {
        "_script" : {
            "type" : "number",
            "script" : {
                "lang": "painless",
                "source": "doc['field_name'].value * params.factor",
                "params" : {
                    "factor" : 1.1
                }
            },
            "order" : "asc"
        }
    }
}

 6.3.0 摺疊 

 用 collapse指定根據某個字段對命中結果進行摺疊

GET /bank/_search
{
    "query": {
        "match_all": {}
    },
    "collapse" : { "field" : "age" },
    "sort": ["balance"] 
}

 查詢結果:

{
  "took": 56,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "820",
        "_score": null,
        "_source": {
          "account_number": 820,
          "balance": 1011,
          "firstname": "Shepard",
          "lastname": "Ramsey",
          "age": 24,
          "gender": "F",
          "address": "806 Village Court",
          "employer": "Mantro",
          "email": "shepardramsey@mantro.com",
          "city": "Tibbie",
          "state": "NV"
        },
        "fields": {
          "age": [
            24
          ]
        },
        "sort": [
          1011
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "894",
        "_score": null,
        "_source": {
          "account_number": 894,
          "balance": 1031,
          "firstname": "Tyler",
          "lastname": "Fitzgerald",
          "age": 32,
          "gender": "M",
          "address": "787 Meserole Street",
          "employer": "Jetsilk",
          "email": "tylerfitzgerald@jetsilk.com",
          "city": "Woodlands",
          "state": "WV"
        },
        "fields": {
          "age": [
            32
          ]
        },
        "sort": [
          1031
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "953",
        "_score": null,
        "_source": {
          "account_number": 953,
          "balance": 1110,
          "firstname": "Baxter",
          "lastname": "Black",
          "age": 27,
          "gender": "M",
          "address": "720 Stillwell Avenue",
          "employer": "Uplinx",
          "email": "baxterblack@uplinx.com",
          "city": "Drummond",
          "state": "MN"
        },
        "fields": {
          "age": [
            27
          ]
        },
        "sort": [
          1110
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "87",
        "_score": null,
        "_source": {
          "account_number": 87,
          "balance": 1133,
          "firstname": "Hewitt",
          "lastname": "Kidd",
          "age": 22,
          "gender": "M",
          "address": "446 Halleck Street",
          "employer": "Isologics",
          "email": "hewittkidd@isologics.com",
          "city": "Coalmont",
          "state": "ME"
        },
        "fields": {
          "age": [
            22
          ]
        },
        "sort": [
          1133
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "749",
        "_score": null,
        "_source": {
          "account_number": 749,
          "balance": 1249,
          "firstname": "Rush",
          "lastname": "Boyle",
          "age": 36,
          "gender": "M",
          "address": "310 Argyle Road",
          "employer": "Sportan",
          "email": "rushboyle@sportan.com",
          "city": "Brady",
          "state": "WA"
        },
        "fields": {
          "age": [
            36
          ]
        },
        "sort": [
          1249
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "315",
        "_score": null,
        "_source": {
          "account_number": 315,
          "balance": 1314,
          "firstname": "Clare",
          "lastname": "Morrow",
          "age": 33,
          "gender": "F",
          "address": "728 Madeline Court",
          "employer": "Gaptec",
          "email": "claremorrow@gaptec.com",
          "city": "Mapletown",
          "state": "PA"
        },
        "fields": {
          "age": [
            33
          ]
        },
        "sort": [
          1314
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "348",
        "_score": null,
        "_source": {
          "account_number": 348,
          "balance": 1360,
          "firstname": "Karina",
          "lastname": "Russell",
          "age": 37,
          "gender": "M",
          "address": "797 Moffat Street",
          "employer": "Limozen",
          "email": "karinarussell@limozen.com",
          "city": "Riegelwood",
          "state": "RI"
        },
        "fields": {
          "age": [
            37
          ]
        },
        "sort": [
          1360
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "490",
        "_score": null,
        "_source": {
          "account_number": 490,
          "balance": 1447,
          "firstname": "Strong",
          "lastname": "Hendrix",
          "age": 26,
          "gender": "F",
          "address": "134 Beach Place",
          "employer": "Duoflex",
          "email": "stronghendrix@duoflex.com",
          "city": "Allentown",
          "state": "ND"
        },
        "fields": {
          "age": [
            26
          ]
        },
        "sort": [
          1447
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "174",
        "_score": null,
        "_source": {
          "account_number": 174,
          "balance": 1464,
          "firstname": "Gamble",
          "lastname": "Pierce",
          "age": 23,
          "gender": "F",
          "address": "650 Eagle Street",
          "employer": "Matrixity",
          "email": "gamblepierce@matrixity.com",
          "city": "Abiquiu",
          "state": "OR"
        },
        "fields": {
          "age": [
            23
          ]
        },
        "sort": [
          1464
        ]
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "111",
        "_score": null,
        "_source": {
          "account_number": 111,
          "balance": 1481,
          "firstname": "Traci",
          "lastname": "Allison",
          "age": 35,
          "gender": "M",
          "address": "922 Bryant Street",
          "employer": "Enjola",
          "email": "traciallison@enjola.com",
          "city": "Robinette",
          "state": "OR"
        },
        "fields": {
          "age": [
            35
          ]
        },
        "sort": [
          1481
        ]
      }
    ]
  }
}
View Code

 高級摺疊

GET /bank/_search
{
    "query": {
        "match_all": {}
    },
    "collapse" : {
        "field" : "age" ,
        <!--指定inner_hits來解釋摺疊 -->
        "inner_hits": {
            "name": "details", <!-- 自命名 -->
            "size": 5,   <!-- 指定每組取幾個文檔 -->
            "sort": [{ "balance": "asc" }] <!-- 組內排序 -->
        },
        "max_concurrent_group_searches": 4 <!-- 指定組查詢的併發數 -->
    },
    "sort": ["balance"] 
}

 查詢結果:

{
  "took": 60,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "820",
        "_score": null,
        "_source": {
          "account_number": 820,
          "balance": 1011,
          "firstname": "Shepard",
          "lastname": "Ramsey",
          "age": 24,
          "gender": "F",
          "address": "806 Village Court",
          "employer": "Mantro",
          "email": "shepardramsey@mantro.com",
          "city": "Tibbie",
          "state": "NV"
        },
        "fields": {
          "age": [
            24
          ]
        },
        "sort": [
          1011
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 42,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "820",
                  "_score": null,
                  "_source": {
                    "account_number": 820,
                    "balance": 1011,
                    "firstname": "Shepard",
                    "lastname": "Ramsey",
                    "age": 24,
                    "gender": "F",
                    "address": "806 Village Court",
                    "employer": "Mantro",
                    "email": "shepardramsey@mantro.com",
                    "city": "Tibbie",
                    "state": "NV"
                  },
                  "sort": [
                    1011
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "924",
                  "_score": null,
                  "_source": {
                    "account_number": 924,
                    "balance": 3811,
                    "firstname": "Hilary",
                    "lastname": "Leonard",
                    "age": 24,
                    "gender": "M",
                    "address": "235 Hegeman Avenue",
                    "employer": "Metroz",
                    "email": "hilaryleonard@metroz.com",
                    "city": "Roosevelt",
                    "state": "ME"
                  },
                  "sort": [
                    3811
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "819",
                  "_score": null,
                  "_source": {
                    "account_number": 819,
                    "balance": 3971,
                    "firstname": "Karyn",
                    "lastname": "Medina",
                    "age": 24,
                    "gender": "F",
                    "address": "417 Utica Avenue",
                    "employer": "Qnekt",
                    "email": "karynmedina@qnekt.com",
                    "city": "Kerby",
                    "state": "WY"
                  },
                  "sort": [
                    3971
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "77",
                  "_score": null,
                  "_source": {
                    "account_number": 77,
                    "balance": 5724,
                    "firstname": "Byrd",
                    "lastname": "Conley",
                    "age": 24,
                    "gender": "F",
                    "address": "698 Belmont Avenue",
                    "employer": "Zidox",
                    "email": "byrdconley@zidox.com",
                    "city": "Rockbridge",
                    "state": "SC"
                  },
                  "sort": [
                    5724
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "493",
                  "_score": null,
                  "_source": {
                    "account_number": 493,
                    "balance": 5871,
                    "firstname": "Campbell",
                    "lastname": "Best",
                    "age": 24,
                    "gender": "M",
                    "address": "297 Friel Place",
                    "employer": "Fanfare",
                    "email": "campbellbest@fanfare.com",
                    "city": "Kidder",
                    "state": "GA"
                  },
                  "sort": [
                    5871
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "894",
        "_score": null,
        "_source": {
          "account_number": 894,
          "balance": 1031,
          "firstname": "Tyler",
          "lastname": "Fitzgerald",
          "age": 32,
          "gender": "M",
          "address": "787 Meserole Street",
          "employer": "Jetsilk",
          "email": "tylerfitzgerald@jetsilk.com",
          "city": "Woodlands",
          "state": "WV"
        },
        "fields": {
          "age": [
            32
          ]
        },
        "sort": [
          1031
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 52,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "894",
                  "_score": null,
                  "_source": {
                    "account_number": 894,
                    "balance": 1031,
                    "firstname": "Tyler",
                    "lastname": "Fitzgerald",
                    "age": 32,
                    "gender": "M",
                    "address": "787 Meserole Street",
                    "employer": "Jetsilk",
                    "email": "tylerfitzgerald@jetsilk.com",
                    "city": "Woodlands",
                    "state": "WV"
                  },
                  "sort": [
                    1031
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "402",
                  "_score": null,
                  "_source": {
                    "account_number": 402,
                    "balance": 1282,
                    "firstname": "Pacheco",
                    "lastname": "Rosales",
                    "age": 32,
                    "gender": "M",
                    "address": "538 Pershing Loop",
                    "employer": "Circum",
                    "email": "pachecorosales@circum.com",
                    "city": "Elbert",
                    "state": "ID"
                  },
                  "sort": [
                    1282
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "735",
                  "_score": null,
                  "_source": {
                    "account_number": 735,
                    "balance": 3984,
                    "firstname": "Loraine",
                    "lastname": "Willis",
                    "age": 32,
                    "gender": "F",
                    "address": "928 Grove Street",
                    "employer": "Gadtron",
                    "email": "lorainewillis@gadtron.com",
                    "city": "Lowgap",
                    "state": "NY"
                  },
                  "sort": [
                    3984
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "745",
                  "_score": null,
                  "_source": {
                    "account_number": 745,
                    "balance": 4572,
                    "firstname": "Jacobs",
                    "lastname": "Sweeney",
                    "age": 32,
                    "gender": "M",
                    "address": "189 Lott Place",
                    "employer": "Comtent",
                    "email": "jacobssweeney@comtent.com",
                    "city": "Advance",
                    "state": "NJ"
                  },
                  "sort": [
                    4572
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "173",
                  "_score": null,
                  "_source": {
                    "account_number": 173,
                    "balance": 5989,
                    "firstname": "Whitley",
                    "lastname": "Blevins",
                    "age": 32,
                    "gender": "M",
                    "address": "127 Brooklyn Avenue",
                    "employer": "Pawnagra",
                    "email": "whitleyblevins@pawnagra.com",
                    "city": "Rodanthe",
                    "state": "ND"
                  },
                  "sort": [
                    5989
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "953",
        "_score": null,
        "_source": {
          "account_number": 953,
          "balance": 1110,
          "firstname": "Baxter",
          "lastname": "Black",
          "age": 27,
          "gender": "M",
          "address": "720 Stillwell Avenue",
          "employer": "Uplinx",
          "email": "baxterblack@uplinx.com",
          "city": "Drummond",
          "state": "MN"
        },
        "fields": {
          "age": [
            27
          ]
        },
        "sort": [
          1110
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 39,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "953",
                  "_score": null,
                  "_source": {
                    "account_number": 953,
                    "balance": 1110,
                    "firstname": "Baxter",
                    "lastname": "Black",
                    "age": 27,
                    "gender": "M",
                    "address": "720 Stillwell Avenue",
                    "employer": "Uplinx",
                    "email": "baxterblack@uplinx.com",
                    "city": "Drummond",
                    "state": "MN"
                  },
                  "sort": [
                    1110
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "123",
                  "_score": null,
                  "_source": {
                    "account_number": 123,
                    "balance": 3079,
                    "firstname": "Cleo",
                    "lastname": "Beach",
                    "age": 27,
                    "gender": "F",
                    "address": "653 Haring Street",
                    "employer": "Proxsoft",
                    "email": "cleobeach@proxsoft.com",
                    "city": "Greensburg",
                    "state": "ME"
                  },
                  "sort": [
                    3079
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "637",
                  "_score": null,
                  "_source": {
                    "account_number": 637,
                    "balance": 3169,
                    "firstname": "Kathy",
                    "lastname": "Carter",
                    "age": 27,
                    "gender": "F",
                    "address": "410 Jamison Lane",
                    "employer": "Limage",
                    "email": "kathycarter@limage.com",
                    "city": "Ernstville",
                    "state": "WA"
                  },
                  "sort": [
                    3169
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "528",
                  "_score": null,
                  "_source": {
                    "account_number": 528,
                    "balance": 4071,
                    "firstname": "Thompson",
                    "lastname": "Hoover",
                    "age": 27,
                    "gender": "F",
                    "address": "580 Garden Street",
                    "employer": "Portalis",
                    "email": "thompsonhoover@portalis.com",
                    "city": "Knowlton",
                    "state": "AL"
                  },
                  "sort": [
                    4071
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "142",
                  "_score": null,
                  "_source": {
                    "account_number": 142,
                    "balance": 4544,
                    "firstname": "Vang",
                    "lastname": "Hughes",
                    "age": 27,
                    "gender": "M",
                    "address": "357 Landis Court",
                    "employer": "Bolax",
                    "email": "vanghughes@bolax.com",
                    "city": "Emerald",
                    "state": "WY"
                  },
                  "sort": [
                    4544
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "87",
        "_score": null,
        "_source": {
          "account_number": 87,
          "balance": 1133,
          "firstname": "Hewitt",
          "lastname": "Kidd",
          "age": 22,
          "gender": "M",
          "address": "446 Halleck Street",
          "employer": "Isologics",
          "email": "hewittkidd@isologics.com",
          "city": "Coalmont",
          "state": "ME"
        },
        "fields": {
          "age": [
            22
          ]
        },
        "sort": [
          1133
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 51,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "87",
                  "_score": null,
                  "_source": {
                    "account_number": 87,
                    "balance": 1133,
                    "firstname": "Hewitt",
                    "lastname": "Kidd",
                    "age": 22,
                    "gender": "M",
                    "address": "446 Halleck Street",
                    "employer": "Isologics",
                    "email": "hewittkidd@isologics.com",
                    "city": "Coalmont",
                    "state": "ME"
                  },
                  "sort": [
                    1133
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "411",
                  "_score": null,
                  "_source": {
                    "account_number": 411,
                    "balance": 1172,
                    "firstname": "Guzman",
                    "lastname": "Whitfield",
                    "age": 22,
                    "gender": "M",
                    "address": "181 Perry Terrace",
                    "employer": "Springbee",
                    "email": "guzmanwhitfield@springbee.com",
                    "city": "Balm",
                    "state": "IN"
                  },
                  "sort": [
                    1172
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "159",
                  "_score": null,
                  "_source": {
                    "account_number": 159,
                    "balance": 1696,
                    "firstname": "Alvarez",
                    "lastname": "Mack",
                    "age": 22,
                    "gender": "F",
                    "address": "897 Manor Court",
                    "employer": "Snorus",
                    "email": "alvarezmack@snorus.com",
                    "city": "Rosedale",
                    "state": "CA"
                  },
                  "sort": [
                    1696
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "220",
                  "_score": null,
                  "_source": {
                    "account_number": 220,
                    "balance": 3086,
                    "firstname": "Tania",
                    "lastname": "Middleton",
                    "age": 22,
                    "gender": "F",
                    "address": "541 Gunther Place",
                    "employer": "Zerology",
                    "email": "taniamiddleton@zerology.com",
                    "city": "Linwood",
                    "state": "IN"
                  },
                  "sort": [
                    3086
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "350",
                  "_score": null,
                  "_source": {
                    "account_number": 350,
                    "balance": 4267,
                    "firstname": "Wyatt",
                    "lastname": "Wise",
                    "age": 22,
                    "gender": "F",
                    "address": "896 Bleecker Street",
                    "employer": "Rockyard",
                    "email": "wyattwise@rockyard.com",
                    "city": "Joes",
                    "state": "MS"
                  },
                  "sort": [
                    4267
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "749",
        "_score": null,
        "_source": {
          "account_number": 749,
          "balance": 1249,
          "firstname": "Rush",
          "lastname": "Boyle",
          "age": 36,
          "gender": "M",
          "address": "310 Argyle Road",
          "employer": "Sportan",
          "email": "rushboyle@sportan.com",
          "city": "Brady",
          "state": "WA"
        },
        "fields": {
          "age": [
            36
          ]
        },
        "sort": [
          1249
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 52,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "749",
                  "_score": null,
                  "_source": {
                    "account_number": 749,
                    "balance": 1249,
                    "firstname": "Rush",
                    "lastname": "Boyle",
                    "age": 36,
                    "gender": "M",
                    "address": "310 Argyle Road",
                    "employer": "Sportan",
                    "email": "rushboyle@sportan.com",
                    "city": "Brady",
                    "state": "WA"
                  },
                  "sort": [
                    1249
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "427",
                  "_score": null,
                  "_source": {
                    "account_number": 427,
                    "balance": 1463,
                    "firstname": "Rebekah",
                    "lastname": "Garrison",
                    "age": 36,
                    "gender": "F",
                    "address": "837 Hampton Avenue",
                    "employer": "Niquent",
                    "email": "rebekahgarrison@niquent.com",
                    "city": "Zarephath",
                    "state": "NY"
                  },
                  "sort": [
                    1463
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "782",
                  "_score": null,
                  "_source": {
                    "account_number": 782,
                    "balance": 3960,
                    "firstname": "Maldonado",
                    "lastname": "Craig",
                    "age": 36,
                    "gender": "F",
                    "address": "345 Myrtle Avenue",
                    "employer": "Zilencio",
                    "email": "maldonadocraig@zilencio.com",
                    "city": "Yukon",
                    "state": "ID"
                  },
                  "sort": [
                    3960
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "6",
                  "_score": null,
                  "_source": {
                    "account_number": 6,
                    "balance": 5686,
                    "firstname": "Hattie",
                    "lastname": "Bond",
                    "age": 36,
                    "gender": "M",
                    "address": "671 Bristol Street",
                    "employer": "Netagy",
                    "email": "hattiebond@netagy.com",
                    "city": "Dante",
                    "state": "TN"
                  },
                  "sort": [
                    5686
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "170",
                  "_score": null,
                  "_source": {
                    "account_number": 170,
                    "balance": 6025,
                    "firstname": "Mann",
                    "lastname": "Madden",
                    "age": 36,
                    "gender": "F",
                    "address": "161 Radde Place",
                    "employer": "Farmex",
                    "email": "mannmadden@farmex.com",
                    "city": "Thermal",
                    "state": "LA"
                  },
                  "sort": [
                    6025
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "315",
        "_score": null,
        "_source": {
          "account_number": 315,
          "balance": 1314,
          "firstname": "Clare",
          "lastname": "Morrow",
          "age": 33,
          "gender": "F",
          "address": "728 Madeline Court",
          "employer": "Gaptec",
          "email": "claremorrow@gaptec.com",
          "city": "Mapletown",
          "state": "PA"
        },
        "fields": {
          "age": [
            33
          ]
        },
        "sort": [
          1314
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 50,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "315",
                  "_score": null,
                  "_source": {
                    "account_number": 315,
                    "balance": 1314,
                    "firstname": "Clare",
                    "lastname": "Morrow",
                    "age": 33,
                    "gender": "F",
                    "address": "728 Madeline Court",
                    "employer": "Gaptec",
                    "email": "claremorrow@gaptec.com",
                    "city": "Mapletown",
                    "state": "PA"
                  },
                  "sort": [
                    1314
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "118",
                  "_score": null,
                  "_source": {
                    "account_number": 118,
                    "balance": 2223,
                    "firstname": "Ballard",
                    "lastname": "Vasquez",
                    "age": 33,
                    "gender": "F",
                    "address": "101 Bush Street",
                    "employer": "Intergeek",
                    "email": "ballardvasquez@intergeek.com",
                    "city": "Century",
                    "state": "MN"
                  },
                  "sort": [
                    2223
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "786",
                  "_score": null,
                  "_source": {
                    "account_number": 786,
                    "balance": 3024,
                    "firstname": "Rene",
                    "lastname": "Vang",
                    "age": 33,
                    "gender": "M",
                    "address": "506 Randolph Street",
                    "employer": "Isopop",
                    "email": "renevang@isopop.com",
                    "city": "Vienna",
                    "state": "NJ"
                  },
                  "sort": [
                    3024
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "932",
                  "_score": null,
                  "_source": {
                    "account_number": 932,
                    "balance": 3111,
                    "firstname": "Summer",
                    "lastname": "Porter",
                    "age": 33,
                    "gender": "F",
                    "address": "949 Grand Avenue",
                    "employer": "Multiflex",
                    "email": "summerporter@multiflex.com",
                    "city": "Spokane",
                    "state": "OK"
                  },
                  "sort": [
                    3111
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "587",
                  "_score": null,
                  "_source": {
                    "account_number": 587,
                    "balance": 3468,
                    "firstname": "Carly",
                    "lastname": "Johns",
                    "age": 33,
                    "gender": "M",
                    "address": "390 Noll Street",
                    "employer": "Gallaxia",
                    "email": "carlyjohns@gallaxia.com",
                    "city": "Emison",
                    "state": "DC"
                  },
                  "sort": [
                    3468
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "348",
        "_score": null,
        "_source": {
          "account_number": 348,
          "balance": 1360,
          "firstname": "Karina",
          "lastname": "Russell",
          "age": 37,
          "gender": "M",
          "address": "797 Moffat Street",
          "employer": "Limozen",
          "email": "karinarussell@limozen.com",
          "city": "Riegelwood",
          "state": "RI"
        },
        "fields": {
          "age": [
            37
          ]
        },
        "sort": [
          1360
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 42,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "348",
                  "_score": null,
                  "_source": {
                    "account_number": 348,
                    "balance": 1360,
                    "firstname": "Karina",
                    "lastname": "Russell",
                    "age": 37,
                    "gender": "M",
                    "address": "797 Moffat Street",
                    "employer": "Limozen",
                    "email": "karinarussell@limozen.com",
                    "city": "Riegelwood",
                    "state": "RI"
                  },
                  "sort": [
                    1360
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "663",
                  "_score": null,
                  "_source": {
                    "account_number": 663,
                    "balance": 2456,
                    "firstname": "Rollins",
                    "lastname": "Richards",
                    "age": 37,
                    "gender": "M",
                    "address": "129 Sullivan Place",
                    "employer": "Geostele",
                    "email": "rollinsrichards@geostele.com",
                    "city": "Morgandale",
                    "state": "FL"
                  },
                  "sort": [
                    2456
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "699",
                  "_score": null,
                  "_source": {
                    "account_number": 699,
                    "balance": 4156,
                    "firstname": "Gallagher",
                    "lastname": "Marshall",
                    "age": 37,
                    "gender": "F",
                    "address": "648 Clifford Place",
                    "employer": "Exiand",
                    "email": "gallaghermarshall@exiand.com",
                    "city": "Belfair",
                    "state": "KY"
                  },
                  "sort": [
                    4156
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "161",
                  "_score": null,
                  "_source": {
                    "account_number": 161,
                    "balance": 4659,
                    "firstname": "Doreen",
                    "lastname": "Randall",
                    "age": 37,
                    "gender": "F",
                    "address": "178 Court Street",
                    "employer": "Calcula",
                    "email": "doreenrandall@calcula.com",
                    "city": "Belmont",
                    "state": "TX"
                  },
                  "sort": [
                    4659
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "258",
                  "_score": null,
                  "_source": {
                    "account_number": 258,
                    "balance": 5712,
                    "firstname": "Lindsey",
                    "lastname": "Hawkins",
                    "age": 37,
                    "gender": "M",
                    "address": "706 Frost Street",
                    "employer": "Enormo",
                    "email": "lindseyhawkins@enormo.com",
                    "city": "Gardners",
                    "state": "AK"
                  },
                  "sort": [
                    5712
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "490",
        "_score": null,
        "_source": {
          "account_number": 490,
          "balance": 1447,
          "firstname": "Strong",
          "lastname": "Hendrix",
          "age": 26,
          "gender": "F",
          "address": "134 Beach Place",
          "employer": "Duoflex",
          "email": "stronghendrix@duoflex.com",
          "city": "Allentown",
          "state": "ND"
        },
        "fields": {
          "age": [
            26
          ]
        },
        "sort": [
          1447
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 59,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "490",
                  "_score": null,
                  "_source": {
                    "account_number": 490,
                    "balance": 1447,
                    "firstname": "Strong",
                    "lastname": "Hendrix",
                    "age": 26,
                    "gender": "F",
                    "address": "134 Beach Place",
                    "employer": "Duoflex",
                    "email": "stronghendrix@duoflex.com",
                    "city": "Allentown",
                    "state": "ND"
                  },
                  "sort": [
                    1447
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "280",
                  "_score": null,
                  "_source": {
                    "account_number": 280,
                    "balance": 3380,
                    "firstname": "Vilma",
                    "lastname": "Shields",
                    "age": 26,
                    "gender": "F",
                    "address": "133 Berriman Street",
                    "employer": "Applidec",
                    "email": "vilmashields@applidec.com",
                    "city": "Adamstown",
                    "state": "ME"
                  },
                  "sort": [
                    3380
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "596",
                  "_score": null,
                  "_source": {
                    "account_number": 596,
                    "balance": 4063,
                    "firstname": "Letitia",
                    "lastname": "Walker",
                    "age": 26,
                    "gender": "F",
                    "address": "963 Vanderveer Place",
                    "employer": "Zizzle",
                    "email": "letitiawalker@zizzle.com",
                    "city": "Rossmore",
                    "state": "ID"
                  },
                  "sort": [
                    4063
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "780",
                  "_score": null,
                  "_source": {
                    "account_number": 780,
                    "balance": 4682,
                    "firstname": "Maryanne",
                    "lastname": "Hendricks",
                    "age": 26,
                    "gender": "F",
                    "address": "709 Wolcott Street",
                    "employer": "Sarasonic",
                    "email": "maryannehendricks@sarasonic.com",
                    "city": "Santel",
                    "state": "NH"
                  },
                  "sort": [
                    4682
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "405",
                  "_score": null,
                  "_source": {
                    "account_number": 405,
                    "balance": 5679,
                    "firstname": "Strickland",
                    "lastname": "Fuller",
                    "age": 26,
                    "gender": "M",
                    "address": "990 Concord Street",
                    "employer": "Digique",
                    "email": "stricklandfuller@digique.com",
                    "city": "Southmont",
                    "state": "NV"
                  },
                  "sort": [
                    5679
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "174",
        "_score": null,
        "_source": {
          "account_number": 174,
          "balance": 1464,
          "firstname": "Gamble",
          "lastname": "Pierce",
          "age": 23,
          "gender": "F",
          "address": "650 Eagle Street",
          "employer": "Matrixity",
          "email": "gamblepierce@matrixity.com",
          "city": "Abiquiu",
          "state": "OR"
        },
        "fields": {
          "age": [
            23
          ]
        },
        "sort": [
          1464
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 42,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "174",
                  "_score": null,
                  "_source": {
                    "account_number": 174,
                    "balance": 1464,
                    "firstname": "Gamble",
                    "lastname": "Pierce",
                    "age": 23,
                    "gender": "F",
                    "address": "650 Eagle Street",
                    "employer": "Matrixity",
                    "email": "gamblepierce@matrixity.com",
                    "city": "Abiquiu",
                    "state": "OR"
                  },
                  "sort": [
                    1464
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "110",
                  "_score": null,
                  "_source": {
                    "account_number": 110,
                    "balance": 4850,
                    "firstname": "Daphne",
                    "lastname": "Byrd",
                    "age": 23,
                    "gender": "F",
                    "address": "239 Conover Street",
                    "employer": "Freakin",
                    "email": "daphnebyrd@freakin.com",
                    "city": "Taft",
                    "state": "MN"
                  },
                  "sort": [
                    4850
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "900",
                  "_score": null,
                  "_source": {
                    "account_number": 900,
                    "balance": 6124,
                    "firstname": "Gonzalez",
                    "lastname": "Watson",
                    "age": 23,
                    "gender": "M",
                    "address": "624 Sullivan Street",
                    "employer": "Marvane",
                    "email": "gonzalezwatson@marvane.com",
                    "city": "Wikieup",
                    "state": "IL"
                  },
                  "sort": [
                    6124
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "443",
                  "_score": null,
                  "_source": {
                    "account_number": 443,
                    "balance": 7588,
                    "firstname": "Huff",
                    "lastname": "Thomas",
                    "age": 23,
                    "gender": "M",
                    "address": "538 Erskine Loop",
                    "employer": "Accufarm",
                    "email": "huffthomas@accufarm.com",
                    "city": "Corinne",
                    "state": "AL"
                  },
                  "sort": [
                    7588
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "643",
                  "_score": null,
                  "_source": {
                    "account_number": 643,
                    "balance": 8057,
                    "firstname": "Hendricks",
                    "lastname": "Stokes",
                    "age": 23,
                    "gender": "F",
                    "address": "142 Barbey Street",
                    "employer": "Remotion",
                    "email": "hendricksstokes@remotion.com",
                    "city": "Lewis",
                    "state": "MA"
                  },
                  "sort": [
                    8057
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "111",
        "_score": null,
        "_source": {
          "account_number": 111,
          "balance": 1481,
          "firstname": "Traci",
          "lastname": "Allison",
          "age": 35,
          "gender": "M",
          "address": "922 Bryant Street",
          "employer": "Enjola",
          "email": "traciallison@enjola.com",
          "city": "Robinette",
          "state": "OR"
        },
        "fields": {
          "age": [
            35
          ]
        },
        "sort": [
          1481
        ],
        "inner_hits": {
          "details": {
            "hits": {
              "total": 52,
              "max_score": null,
              "hits": [
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "111",
                  "_score": null,
                  "_source": {
                    "account_number": 111,
                    "balance": 1481,
                    "firstname": "Traci",
                    "lastname": "Allison",
                    "age": 35,
                    "gender": "M",
                    "address": "922 Bryant Street",
                    "employer": "Enjola",
                    "email": "traciallison@enjola.com",
                    "city": "Robinette",
                    "state": "OR"
                  },
                  "sort": [
                    1481
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "417",
                  "_score": null,
                  "_source": {
                    "account_number": 417,
                    "balance": 1788,
                    "firstname": "Wheeler",
                    "lastname": "Ayers",
                    "age": 35,
                    "gender": "F",
                    "address": "677 Hope Street",
                    "employer": "Fortean",
                    "email": "wheelerayers@fortean.com",
                    "city": "Ironton",
                    "state": "PA"
                  },
                  "sort": [
                    1788
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "984",
                  "_score": null,
                  "_source": {
                    "account_number": 984,
                    "balance": 1904,
                    "firstname": "Viola",
                    "lastname": "Crawford",
                    "age": 35,
                    "gender": "F",
                    "address": "354 Linwood Street",
                    "employer": "Ginkle",
                    "email": "violacrawford@ginkle.com",
                    "city": "Witmer",
                    "state": "AR"
                  },
                  "sort": [
                    1904
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "527",
                  "_score": null,
                  "_source": {
                    "account_number": 527,
                    "balance": 2028,
                    "firstname": "Carver",
                    "lastname": "Peters",
                    "age": 35,
                    "gender": "M",
                    "address": "816 Victor Road",
                    "employer": "Housedown",
                    "email": "carverpeters@housedown.com",
                    "city": "Nadine",
                    "state": "MD"
                  },
                  "sort": [
                    2028
                  ]
                },
                {
                  "_index": "bank",
                  "_type": "_doc",
                  "_id": "266",
                  "_score": null,
                  "_source": {
                    "account_number": 266,
                    "balance": 2777,
                    "firstname": "Monique",
                    "lastname": "Conner",
                    "age": 35,
                    "gender": "F",
                    "address": "489 Metrotech Courtr",
                    "employer": "Flotonic",
                    "email": "moniqueconner@flotonic.com",
                    "city": "Retsof",
                    "state": "MD"
                  },
                  "sort": [
                    2777
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}
View Code

在inner_hits 中返回多個角度的組內topN

GET /twitter/_search
{
    "query": {
        "match": {
            "message": "elasticsearch"
        }
    },
    "collapse" : {
        "field" : "user", 
        "inner_hits": [ { "name": "most_liked", "size": 3, "sort": ["likes"] }, { "name": "most_recent", "size": 3, "sort": [{ "date": "asc" }] } ]
    },
    "sort": ["likes"]
}

 說明:

most_liked:最像

most_recent:最近一段時間的

 6.3.1 分頁

 from and size

GET /_search
{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

注意:搜索請求耗用的堆內存和時間與 from + size 大小成正比。分頁越深耗用越大,爲了避免因分頁致使OOM或嚴重影響性能,ES中規定from + size 不能大於索引setting參數 index.max_result_window 的值,默認值爲 10,000。

須要深度分頁, 不受index.max_result_window 限制,怎麼辦? 

Search after  在指定文檔後取文檔, 可用於深度分頁

 首次查詢第一頁

GET twitter/_search
{
    "size": 10,
    "query": {
        "match" : {
            "title" : "elasticsearch"
        }
    },
    "sort": [ {"date": "asc"}, {"_id": "desc"} ]
}

後續頁的查詢

GET twitter/_search
{
    "size": 10,
    "query": {
        "match" : {
            "title" : "elasticsearch"
        }
    },
    "search_after": [1463538857, "654323"],
    "sort": [
        {"date": "asc"},
        {"_id": "desc"}
    ]
}

注意:使用search_after,要求查詢必須指定排序,而且這個排序組合值每一個文檔惟一(最好排序中包含_id字段)。 search_after的值用的就是這個排序值。 用search_after時 from 只能爲0、-1。

6.3.2 高亮

準備數據:

PUT /hl_test/_doc/1
{
  "title": "lucene solr and elasticsearch",
  "content": "lucene solr and elasticsearch for search"
}

查詢高亮數據

GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {}
    }
  }
}

查詢結果:

{
  "took": 113,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "hl_test",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        },
        "highlight": {
          "title": [
            "<em>lucene</em> solr and elasticsearch"
          ]
        }
      }
    ]
  }
}

多字段高亮

GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "require_field_match": false,     "fields": {
      "title": {},
      "content": {}
    }
  }
}

查詢結果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "hl_test",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        },
        "highlight": {
          "title": [ "<em>lucene</em> solr and elasticsearch" ], "content": [ "<em>lucene</em> solr and elasticsearch for search" ]
        }
      }
    ]
  }
}

說明:

高亮結果在返回的每一個文檔中以hightlight節點給出

指定高亮標籤

GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "title": { "pre_tags":["<strong>"], "post_tags": ["</strong>"] },
      "content": {}
    }
  }
}

查詢結果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "hl_test",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        },
        "highlight": {
          "title": [
            "<strong>lucene</strong> solr and elasticsearch"
          ],
          "content": [
            "<em>lucene</em> solr and elasticsearch for search"
          ]
        }
      }
    ]
  }
}

高亮的詳細設置請參考官網:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

6.3.3 Profile  爲了調試、優化

對於執行緩慢的查詢,咱們很想知道它爲何慢,時間都耗在哪了,能夠在查詢上加入上 profile 來得到詳細的執行步驟、耗時信息。

GET /twitter/_search
{
  "profile": true,
  "query" : {
    "match" : { "message" : "some number" }
  }
}

信息的說明請參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html

7.  count api 查詢數量

PUT /twitter/_doc/1?refresh
{
    "user": "kimchy"
}

GET /twitter/_doc/_count?q=user:kimchy

GET /twitter/_doc/_count
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

結果說明:

{
    "count" : 1,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
    }
}

8. validate api  

用來檢查咱們的查詢是否正確,以及查看底層生成查詢是怎樣的

GET twitter/_validate/query?q=user:foo

8.1 校驗查詢

GET twitter/_doc/_validate/query
{
  "query": {
    "query_string": {
      "query": "post_date:foo",
      "lenient": false
    }
  }
}

查詢結果:

{
  "valid": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  }
}

8.2 得到查詢解釋

GET twitter/_doc/_validate/query?explain=true
{
  "query": {
    "query_string": {
      "query": "post_date:foo",
      "lenient": false
    }
  }
}

查詢結果

{
  "valid": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "explanations": [
    {
      "index": "twitter",
      "valid": true,
      "explanation": """+MatchNoDocsQuery("unmapped field [post_date]") #MatchNoDocsQuery("Type list does not contain the index type")"""
    }
  ]
}

8.3 用rewrite得到比explain 更詳細的解釋

GET twitter/_doc/_validate/query?rewrite=true
{
  "query": {
    "more_like_this": {
      "like": {
        "_id": "2"
      },
      "boost_terms": 1
    }
  }
}

查詢結果:

{
  "valid": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "explanations": [
    {
      "index": "twitter",
      "valid": true,
      "explanation": """+(MatchNoDocsQuery("empty BooleanQuery") -ConstantScore(MatchNoDocsQuery("empty BooleanQuery"))) #MatchNoDocsQuery("Type list does not contain the index type")"""
    }
  ]
}

8.4 得到全部分片上的查詢解釋

GET twitter/_doc/_validate/query?rewrite=true&all_shards=true
{
  "query": {
    "match": {
      "user": {
        "query": "kimchy",
        "fuzziness": "auto"
      }
    }
  }
}

查詢結果:

{
  "valid": true,
  "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
  },
  "explanations": [
    {
      "index": "twitter",
      "shard": 0,
      "valid": true,
      "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
    },
    {
      "index": "twitter",
      "shard": 1,
      "valid": true,
      "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
    },
    {
      "index": "twitter",
      "shard": 2,
      "valid": true,
      "explanation": """MatchNoDocsQuery("unmapped field [user]")"""
    }
  ]
}

官網連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html

9. Explain api  

得到某個查詢的評分解釋,及某個文檔是否被這個查詢命中

GET /twitter/_doc/0/_explain
{
      "query" : {
        "match" : { "message" : "elasticsearch" }
      }
}

官網連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

10. Search Shards API

讓咱們能夠了解可執行查詢的索引分片節點狀況

GET /twitter/_search_shards

查詢結果:

{
  "nodes": {
    "qkmtovyLRPWjXcfDTryNwA": {
      "name": "qkmtovy",
      "ephemeral_id": "sxgsvzsORraAnN7PIlMYpg",
      "transport_address": "127.0.0.1:9300",
      "attributes": {}
    }
  },
  "indices": {
    "twitter": {}
  },
  "shards": [
    [
      {
        "state": "STARTED",
        "primary": true,
        "node": "qkmtovyLRPWjXcfDTryNwA",
        "relocating_node": null,
        "shard": 0,
        "index": "twitter",
        "allocation_id": {
          "id": "3Yf6lOjyQja_v4yP_gL8qA"
        }
      }
    ],
    [
      {
        "state": "STARTED",
        "primary": true,
        "node": "qkmtovyLRPWjXcfDTryNwA",
        "relocating_node": null,
        "shard": 1,
        "index": "twitter",
        "allocation_id": {
          "id": "8S88pnUkSSy8kiCcwBgb9Q"
        }
      }
    ],
    [
      {
        "state": "STARTED",
        "primary": true,
        "node": "qkmtovyLRPWjXcfDTryNwA",
        "relocating_node": null,
        "shard": 2,
        "index": "twitter",
        "allocation_id": {
          "id": "_uIup55LQZKaltUfuh5aFA"
        }
      }
    ]
  ]
}
View Code

想知道指定routing值的查詢將在哪些分片節點上執行

GET /twitter/_search_shards?routing=foo,baz

查詢結果:

{
  "nodes": {
    "qkmtovyLRPWjXcfDTryNwA": {
      "name": "qkmtovy",
      "ephemeral_id": "sxgsvzsORraAnN7PIlMYpg",
      "transport_address": "127.0.0.1:9300",
      "attributes": {}
    }
  },
  "indices": {
    "twitter": {}
  },
  "shards": [
    [
      {
        "state": "STARTED",
        "primary": true,
        "node": "qkmtovyLRPWjXcfDTryNwA",
        "relocating_node": null,
        "shard": 1,
        "index": "twitter",
        "allocation_id": {
          "id": "8S88pnUkSSy8kiCcwBgb9Q"
        }
      }
    ]
  ]
}

11. Search Template 查詢模板

註冊一個模板

POST _scripts/<templatename>
{
    "script": {
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "title": "{{query_string}}"
                }
            }
        }
    }
}

使用模板進行查詢

GET _search/template
{
    "id": "<templateName>", 
    "params": {
        "query_string": "search for these words"
    }
}

查詢結果:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 38,
    "successful": 38,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

詳細瞭解請參考官網:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html

2、Query DSL

 

官網介紹連接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

 Query DSL 介紹

 1. DSL是什麼?

Domain Specific Language:領域特定語言

Elasticsearch基於JSON提供完整的查詢DSL來定義查詢。

一個查詢可由兩部分字句構成:

Leaf query clauses 葉子查詢字句
Leaf query clauses 在指定的字段上查詢指定的值, 如:match, term or range queries. 葉子字句能夠單獨使用.
Compound query clauses 複合查詢字句
以邏輯方式組合多個葉子、複合查詢爲一個查詢

 2. Query and filter context

 一個查詢字句的行爲取決於它是用在query context  仍是 filter context 中 。

Query context 查詢上下文
用在查詢上下文中的字句回答「這個文檔有多匹配這個查詢?」。除了決定文檔是否匹配,字句匹配的文檔還會計算一個字句評分,來評定文檔有多匹配。查詢上下文由 query 元素表示。
Filter context 過濾上下文
過濾上下文由 filter 元素或 bool 中的 must not 表示。用在過濾上下文中的字句回答「這個文檔是否匹配這個查詢?」,不參與相關性評分
被頻繁使用的過濾器將被ES自動緩存,來提升查詢性能。

 示例:

GET /_search
{
  <!--查詢 -->
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }}, 
        { "match": { "content": "Elasticsearch" }}  
      ],
      <!--過濾 -->
      "filter": [ 
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}

 說明:查詢和過濾都是對全部文檔進行查詢,最後兩個結果取交集

 提示:在查詢上下文中使用查詢子句來表示影響匹配文檔得分的條件,並在過濾上下文中使用全部其餘查詢子句。

 查詢分類介紹

 

1. Match all query 查詢全部

GET /_search
{
    "query": {
        "match_all": {}
    }
}

 相反,什麼都不查

GET /_search
{
    "query": {
        "match_none": {}
    }
}

 2. Full text querys

全文查詢,用於對分詞的字段進行搜索。會用查詢字段的分詞器對查詢的文本進行分詞生成查詢。可用於短語查詢、模糊查詢、前綴查詢、臨近查詢等查詢場景

 官網連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

 3. match query

全文查詢的標準查詢,它能夠對一個字段進行模糊、短語查詢。 match queries 接收 text/numerics/dates, 對它們進行分詞分析, 再組織成一個boolean查詢。可經過operator 指定bool組合操做(or、and 默認是 or ), 以及minimum_should_match 指定至少需多少個should(or)字句需知足。還可用ananlyzer指定查詢用的特殊分析器。

GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}

 說明:message是字段名

 官網連接:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

 示例:

構造索引和數據:

PUT /ftq/_doc/1
{
  "title": "lucene solr and elasticsearch",
  "content": "lucene solr and elasticsearch for search"
}

PUT /ftq/_doc/2
{
  "title": "java spring boot",
  "content": "lucene is writerd by java"
}

 執行查詢1

GET ftq/_doc/_validate/query?rewrite=true
{
  "query": {
    "match": {
      "title": "lucene java"
    }
  }
}

 查詢結果1:

{
  "valid": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "explanations": [
    {
      "index": "ftq",
      "valid": true,
      "explanation": "title:lucene title:java"
    }
  ]
}

 執行查詢2:

GET ftq/_search
{
  "query": {
    "match": {
      "title": "lucene java"
    }
  }
}

 查詢結果2:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "title": "java spring boot",
          "content": "lucene is writerd by java"
        }
      },
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

 執行查詢3:指定操做符

GET ftq/_search
{
  "query": {
    "match": {
      "title": { "query": "lucene java", "operator": "and" }
    }
  }
}

 查詢結果3:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

模糊查詢,最大編輯數爲2

GET ftq/_search
{
  "query": {
    "match": {
      "title": {
        "query": "ucen elatic",
        "fuzziness": 2
      }
    }
  }
}

模糊查詢結果:

{
  "took": 280,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.14384104,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.14384104,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

指定最少需知足兩個詞匹配

GET ftq/_search
{
  "query": {
    "match": {
      "content": {
        "query": "ucen elatic java",
        "fuzziness": 2,
        "minimum_should_match": 2
      }
    }
  }
}

 查詢結果:

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.43152314,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.43152314,
        "_source": {
          "title": "java spring boot",
          "content": "lucene is writerd by java"
        }
      }
    ]
  }
}

 可用max_expansions 指定模糊匹配的最大詞項數,默認是50。好比:反向索引中有 100 個詞項與 ucen 模糊匹配,只選用前50 個。

 4. match  phrase  query

match_phrase 查詢用來對一個字段進行短語查詢,能夠指定 analyzer、slop移動因子。

 對字段進行短語查詢1:

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": "lucene solr"
    }
  }
}

 結果1:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

 對字段進行短語查詢2:

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": "lucene elasticsearch"
    }
  }
}

結果2:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

對查詢指定移動因子:

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "lucene elasticsearch",
        "slop": 2
      }
    }
  }
}

 查詢結果:

{
  "took": 2174,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.27517417,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.27517417,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

 5. match  phrase  prefix query

match_phrase_prefix 在 match_phrase 的基礎上支持對短語的最後一個詞進行前綴匹配

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
        }
    }
}

 指定前綴匹配選用的最大詞項數量

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : {
                "query" : "quick brown f",
                "max_expansions" : 10
            }
        }
    }
}

 6. Multi match query

若是你須要在多個字段上進行文本搜索,可用multi_match 。 multi_match在 match的基礎上支持對多個字段進行文本查詢。

查詢1:

GET ftq/_search
{
  "query": {
    "multi_match" : {
      "query":    "lucene java", 
      "fields": [ "title", "content" ] 
    }
  }
}

結果1:

{
  "took": 1973,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "title": "java spring boot",
          "content": "lucene is writerd by java"
        }
      },
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

查詢2:字段通配符查詢

GET ftq/_search
{
  "query": {
    "multi_match" : {
      "query":    "lucene java", 
      "fields": [ "title", "cont*" ] 
    }
  }
}

結果2:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "title": "java spring boot",
          "content": "lucene is writerd by java"
        }
      },
      {
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        }
      }
    ]
  }
}

查詢3:給字段的相關性評分加權重

GET ftq/_search?explain=true
{
  "query": {
    "multi_match" : {
      "query":    "lucene elastic", 
      "fields": [ "title^5", "content" ] 
    }
  }
}

結果3:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.4384104,
    "hits": [
      {
        "_shard": "[ftq][3]",
        "_node": "qkmtovyLRPWjXcfDTryNwA",
        "_index": "ftq",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4384104,
        "_source": {
          "title": "lucene solr and elasticsearch",
          "content": "lucene solr and elasticsearch for search"
        },
        "_explanation": {
          "value": 1.4384104,
          "description": "max of:",
          "details": [
            {
              "value": 1.4384104,
              "description": "sum of:",
              "details": [
                {
                  "value": 1.4384104,
                  "description": "weight(title:lucene in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 1.4384104,
                      "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
                      "details": [
                        {
                          "value": 5,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.2876821,
                          "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "docFreq",
                              "details": []
                            },
                            {
                              "value": 1,
                              "description": "docCount",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 1,
                          "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "termFreq=1.0",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "parameter k1",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "parameter b",
                              "details": []
                            },
                            {
                              "value": 4,
                              "description": "avgFieldLength",
                              "details": []
                            },
                            {
                              "value": 4,
                              "description": "fieldLength",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 0.2876821,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.2876821,
                  "description": "weight(content:lucene in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.2876821,
                      "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
                      "details": [
                        {
                          "value": 0.2876821,
                          "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "docFreq",
                              "details": []
                            },
                            {
                              "value": 1,
                              "description": "docCount",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 1,
                          "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "termFreq=1.0",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "parameter k1",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "parameter b",
                              "details": []
                            },
                            {
                              "value": 6,
                              "description": "avgFieldLength",
                              "details": []
                            },
                            {
                              "value": 6,
                              "description": "fieldLength",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[ftq][2]",
        "_node": "qkmtovyLRPWjXcfDTryNwA",
        "_index": "ftq",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "title": "java spring boot",
          "content": "lucene is writerd by java"
        },
        "_explanation": {
          "value": 0.2876821,
          "description": "max of:",
          "details": [
            {
              "value": 0.2876821,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.2876821,
                  "description": "weight(content:lucene in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.2876821,
                      "description": "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
                      "details": [
                        {
                          "value": 0.2876821,
                          "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "docFreq",
                              "details": []
                            },
                            {
                              "value": 1,
                              "description": "docCount",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 1,
                          "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:",
                          "details": [
                            {
                              "value": 1,
                              "description": "termFreq=1.0",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "parameter k1",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "parameter b",
                              "details": []
                            },
                            {
                              "value": 5,
                              "description": "avgFieldLength",
                              "details": []
                            },
                            {
                              "value": 5,
                              "description": "fieldLength",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
View Code

7. Common terms query

common 經常使用詞查詢

問一、什麼是停用詞?索引時作停用詞處理的目的是什麼?

    再也不使用的詞,作停用詞處理的目的是提升索引的效率,去掉不須要的索引操做,即停用詞不須要索引
問二、若是在索引時應用停用詞處理,下面的兩個查詢會查詢什麼詞項?
the brown fox—— brown fox
not happy——happy

問三、索引時應用停用詞處理對搜索精度是否有影響?若是不作停用詞處理又會有什麼影響?如何協調這兩個問題?如何保證搜索的精確度又兼顧搜索性能?

索引時應用停用詞處理對搜索精度有影響,不作停用詞處理又會影響索引的效率,要協調這兩個問題就必需要使用tf-idf 相關性計算模型

7.1 tf-idf 相關性計算模型簡介

tf:term frequency   詞頻 :指一個詞在一篇文檔中出現的頻率。

如「世界盃」在文檔A中出現3次,那麼能夠定義「世界盃」在文檔A中的詞頻爲3。請問在一篇3000字的文章中出現「世界盃」3次和一篇150字的文章中出現3詞,哪篇文章更是與「世界盃」有關的。也就是說,簡單用出現次數做爲頻率不夠準確。那就用佔比來表示:

問:tf值越大是否就必定說明這個詞更相關?

 不是,出現太多了說明不重要

 說明:tf的計算不必定非是這樣的,能夠定義不一樣的計算方式。

df:document frequency 詞的文檔頻率 :指包含某個詞的文檔數(有多少文檔中包含這個詞)。 df越大的詞越常見,哪些詞會是高頻詞?

問1:詞的df值越大說明這個詞在這個文檔集中是越重要仍是越不重要?

 越不重要

問2:詞t的tf高,在文檔集中的重要性也高,是否說明文檔與該詞越相關?舉例:整個文檔集中只有3篇文檔中有「世界盃」,文檔A中就出現了「世界盃」好幾回。 

 不能說明文檔與該詞越相關

問3:如何用數值體現詞t在文檔集中的重要性?df能夠嗎?

 不能夠

 idf:inverse document frequency   詞的逆文檔頻率 :用來表示詞在文檔集中的重要性。文檔總數/ df ,df越小,詞越重要,這個值會很大,那就對它取個天然對數,將值映射到一個較小的取值範圍。

 

說明: +1 是爲了不除0(即詞t在文檔集中未出現的狀況)

tf-idf 相關性性計算模型:tf-idf t = tf t,d * idf t

 說明: tf-idf 相關性性計算模型的值爲詞頻( tf t,d)乘以詞的逆文檔頻率(idf t

7.2 Common terms query

common 區分經常使用(高頻)詞查詢讓咱們能夠經過cutoff_frequency來指定一個分界文檔頻率值,將搜索文本中的詞分爲高頻詞和低頻詞,低頻詞的重要性高於高頻詞,先對低頻詞進行搜索並計算全部匹配文檔相關性得分;而後再搜索和高頻詞匹配的文檔,這會搜到不少文檔,但只對和低頻詞重疊的文檔進行相關性得分計算(這可保證搜索精確度,同時大大提升搜索性能),和低頻詞累加做爲文檔得分。實際執行的搜索是 必須包含低頻詞 + 或包含高頻詞。

思考:這樣處理下,若是用戶輸入的都是高頻詞如 「to be or not to be」結果會是怎樣的?你但願是怎樣的?

優化:若是都是高頻詞,那就對這些詞進行and 查詢。
進一步優化:讓用戶能夠本身定對高頻詞作and/or 操做,本身定對低頻詞進行and/or 操做;或指定最少得多少個同時匹配

示例1:

GET /_search
{
    "query": {
        "common": {
            "message": {
                "query": "this is bonsai cool",
                "cutoff_frequency": 0.001
            }
        }
    }
}

說明:

cutoff_frequency : 值大於1表示文檔數,0-1.0表示佔比。 此處界定 文檔頻率大於 0.1%的詞爲高頻詞。

示例2:

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "low_freq_operator": "and"
            }
        }
    }
}
說明:low_freq_operator指定對低頻詞作與操做

可用參數:minimum_should_match (high_freq, low_freq), low_freq_operator (default 「or」) and high_freq_operator (default 「or」)、 boost and analyzer

示例3:

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": 2
            }
        }
    }
}

示例4:

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant not as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": { "low_freq" : 2, "high_freq" : 3 }
            }
        }
    }
}

示例5:

8. Query string query

query_string 查詢,讓咱們能夠直接用lucene查詢語法寫一個查詢串進行查詢,ES中接到請求後,經過查詢解析器解析查詢串生成對應的查詢。使用它要求掌握lucene的查詢語法。

 示例1:指定單個字段查詢

GET /_search
{
    "query": {
        "query_string" : {
            "default_field" : "content",
            "query" : "this AND that OR thus"
        }
    }
}

 示例2:指定多字段通配符查詢

GET /_search
{
    "query": {
        "query_string" : {
            "fields" : ["content", "name.*^5"],
            "query" : "this AND that OR thus"
        }
    }
}

 可與query同用的參數,如 default_field、fields,及query 串的語法請參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

 9. 查詢描述規則語法(查詢解析語法)

Term 詞項:

單個詞項的表示: 電腦
短語的表示: "聯想筆記本電腦"

Field 字段:

字段名:
示例: name:「聯想筆記本電腦」 AND type:電腦
若是name是默認字段,則可寫成: 「聯想筆記本電腦」 AND type:電腦
若是查詢串是:type:電腦 計算機 手機
注意:只有第一個是type的值,後兩個則是使用默認字段。

 Term Modifiers 詞項修飾符:

 

10. Simple Query string query

simple_query_string 查同 query_string 查詢同樣用lucene查詢語法寫查詢串,較query_string不一樣的地方:更小的語法集;查詢串有錯誤,它會忽略錯誤的部分,不拋出錯誤。更適合給用戶使用。

 示例:

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "fields": ["title^5", "body"],
        "default_operator": "and"
    }
  }
}

 語法請參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

 11. Term level querys

 

官網連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html

 11.1 Term query

term 查詢用於查詢指定字段包含某個詞項的文檔。

 示例1:

POST _search
{
  "query": {
    "term" : { "user" : "Kimchy" } 
  }
}

 示例2:加權重

GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": {
              "value": "urgent",
              "boost": 2
            }
          }
        },
        {
          "term": {
            "status": "normal"
          }
        }
      ]
    }
  }
}

 11.2 Terms query

 terms 查詢用於查詢指定字段包含某些詞項的文檔

GET /_search
{
    "query": {
        "terms" : { "user" : ["kimchy", "elasticsearch"]}
    }
}

Terms 查詢支持嵌套查詢的方式來得到查詢詞項,至關於 in (select term from other)

示例1:Terms query 嵌套查詢示例

PUT /users/_doc/2
{
    "followers" : ["1", "3"]
}

PUT /tweets/_doc/1
{
    "user" : "1"
}

GET /tweets/_search
{
  "query": {
    "terms": { "user": { "index": "users", "type": "_doc", "id": "2", "path": "followers" } }
  }
}

查詢結果:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "tweets",
        "_type": "_doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "1"
        }
      }
    ]
  }
}

嵌套查詢可用參數說明:

11.3 range query

 範圍查詢示例1:

GET _search
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}

  範圍查詢示例2:

GET _search
{
    "query": {
        "range" : {
            "date" : {
               "gte" : "now-1d/d", "lt" : "now/d"
            }
        }
    }
}

  範圍查詢示例3:

GET _search
{
    "query": {
        "range" : {
            "born" : {
                "gte": "01/01/2012",
                "lte": "2013",
                "format": "dd/MM/yyyy||yyyy"
            }
        }
    }
}

 範圍查詢參數說明:

範圍查詢時間舍入 ||說明:

時間數學計算規則請參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

11.4 exists  query

查詢指定字段值不爲空的文檔。至關 SQL 中的 column is not null

GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}

查詢指定字段值爲空的文檔

GET /_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "user"
        }
      }
    }
  }
}

 11.5 prefix query 詞項前綴查詢

 示例1:

GET /_search
{ "query": {
    "prefix" : { "user" : "ki" }
  }
}

 示例2:加權

GET /_search
{ "query": {
    "prefix" : { "user" :  { "value" : "ki", "boost" : 2.0 } }
  }
}

 11.6 wildcard query 通配符查詢: ? *

 示例1:

GET /_search
{
    "query": {
        "wildcard" : { "user" : "ki*y" }
    }
}

 示例2:加權

GET /_search
{
  "query": {
    "wildcard": {
      "user": {
        "value": "ki*y",
        "boost": 2
      }
    }
  }}

11.7  regexp query   正則查詢

示例1:

GET /_search
{
    "query": {
        "regexp":{
            "name.first": "s.*y"
        }
    }
}

示例2:加權

GET /_search
{
    "query": {
        "regexp":{
            "name.first":{
                "value":"s.*y",
                "boost":1.2
            }
        }
    }
}

正則語法請參考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax

11.8 fuzzy query 模糊查詢

示例1:

GET /_search
{
    "query": {
       "fuzzy" : { "user" : "ki" }
    }
}

示例2:

GET /_search
{
    "query": {
        "fuzzy" : {
            "user" : {
                "value": "ki", "boost": 1.0, "fuzziness": 2, "prefix_length": 0, "max_expansions": 100
            }
        }
    }
}

11.9 type query   mapping type 查詢

GET /_search
{
    "query": {
        "type" : {
            "value" : "_doc"
        }
    }
}

11.10 ids query   根據文檔id查詢

GET /_search
{
    "query": {
        "ids" : {
            "type" : "_doc",
            "values" : ["1", "4", "100"]
        }
    }
}

12. Compound querys 複合查詢

 官網連接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html

 12.1 Constant Score query

 用來包裝另外一個查詢,將查詢匹配的文檔的評分設爲一個常值。

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2         }
    }
}

 12.2 Bool query

 Bool 查詢用bool操做來組合多個查詢字句爲一個查詢。 可用的關鍵字:

 

示例:

POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}

 說明:should知足一個或者兩個或者都不知足

相關文章
相關標籤/搜索