elasticsearch系列四:搜索详解(搜索API、Query DSL) - Go语言中文社区

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


一、搜索API

 

1. 搜索API 端点地址

从索引tweet里面搜索字段user为kimchy的记录

GET /twitter/_search?q=user:kimchy

从索引tweet,user里面搜索字段user为kimchy的记录

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

从所有索引里面搜索字段tag为wow的记录

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

说明:搜索的端点地址可以是多索引多mapping type的。搜索的参数可作为URI请求参数给出,也可用 request body 给出

2. URI Search

URI 搜索方式通过URI参数来指定查询相关参数。让我们可以快速做一个查询。

GET /twitter/_search?q=user:kimchy

可用的参数请参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html

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 。

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
                  ]
   
                        
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_33911824/article/details/86130859
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:53:06
  • 阅读 ( 1248 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢