elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL)

这篇具有很好参考价值的文章主要介绍了elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

一、搜索 API

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

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. 查询结果说明

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

5. 特殊的查询参数用法

如果我们只想知道有多少文档匹配某个查询,可以这样用参数:

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

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

如果我们只想知道有没有文档匹配某个查询,可以这样用参数:

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

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

比较两个查询的结果可以知道第一个查询返回所有的命中文档数,第二个查询由于只需要知道有没有文档,所以只要有文档就立即返回

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": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "fields": {
          "test1": [
          ],
          "test2": [
          ]
        }
      }
    ]
  }
}
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": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "fields": {
          "balance*2": [
          ],
          "ffx": [
          ]
        }
      }
    ]
  }
}
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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      },
      {
        "_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,
        ]
      }
    ]
  }
}

结果中每个文档会有排序字段值给出

"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 参数指定按多值的:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ]
      }
    ]
  }
}

高级折叠

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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_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": [
          ]
        },
        "sort": [
        ],
        "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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                },
                {
                  "_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": [
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

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

想知道指定 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

二、Query DSL

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

官网介绍链接: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" }}} 
      ]
    }
  }
}

说明: 查询和过滤都是对所有文档进行查询,最后两个结果取交集

提示:在查询上下文中使用查询子句来表示影响匹配文档得分的条件,并在过滤上下文中使用所有其他查询子句。

查询分类介绍

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

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": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

7. Common terms query

common 常用词查询

问 1、什么是停用词?索引时做停用词处理的目的是什么?

不再使用的词,做停用词处理的目的是提高索引的效率,去掉不需要的索引操作,即停用词不需要索引  

问 2、如果在索引时应用停用词处理,下面的两个查询会查询什么词项?
the brown fox—— brown fox
not happy——happy

问 3、索引时应用停用词处理对搜索精度是否有影响?如果不做停用词处理又会有什么影响?如何协调这两个问题?如何保证搜索的精确度又兼顾搜索性能?

索引时应用停用词处理对搜索精度有影响,不做停用词处理又会影响索引的效率,要协调这两个问题就必须要使用 tf-idf 相关性计算模型

7.1 tf-idf 相关性计算模型简介

tf:term frequency 词频 :指一个词在一篇文档中出现的频率。

如 “世界杯” 在文档 A 中出现 3 次,那么可以定义 “世界杯” 在文档 A 中的词频为 3。请问在一篇 3000 字的文章中出现 “世界杯”3 次和一篇 150 字的文章中出现 3 词,哪篇文章更是与“世界杯” 有关的。也就是说,简单用出现次数作为频率不够准确。那就用占比来表示:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

问:tf 值越大是否就一定说明这个词更相关?

不是,出现太多了说明不重要

说明:tf 的计算不一定非是这样的,可以定义不同的计算方式。

df:document frequency 词的文档频率 :指包含某个词的文档数(有多少文档中包含这个词)。 df 越大的词越常见,哪些词会是高频词?

问 1:词的 df 值越大说明这个词在这个文档集中是越重要还是越不重要?

越不重要

问 2:词 t 的 tf 高,在文档集中的重要性也高,是否说明文档与该词越相关?举例:整个文档集中只有 3 篇文档中有 “世界杯”,文档 A 中就出现了“世界杯” 好几次。

不能说明文档与该词越相关

问 3:如何用数值体现词 t 在文档集中的重要性?df 可以吗?

不可以

idf:inverse document frequency 词的逆文档频率 :用来表示词在文档集中的重要性。文档总数 / df ,df 越小,词越重要,这个值会很大,那就对它取个自然对数,将值映射到一个较小的取值范围。

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

说明: +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:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

8. Query string query

query_string 查询,让我们可以直接用 lucene 查询语法写一个查询串进行查询(and or),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 词项修饰符:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

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

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

官网链接:

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

嵌套查询可用参数说明:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

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

范围查询参数说明:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

范围查询时间舍入 || 说明:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

时间数学计算规则请参考:

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 复合查询

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

官网链接:

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 操作来组合多个查询字句为一个查询。 可用的关键字:

elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL),elasticsearch,elasticsearch,笔记,jenkins

示例:

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 满足一个或者两个或者都不满足

参考

Elasticsearch入常用RESTful API总结

Elasticsearch之Search API文章来源地址https://www.toymoban.com/news/detail-761957.html

到了这里,关于elasticsearch 笔记二:搜索DSL 语法(搜索API、Query DSL)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Elasticsearch:DSL Query

    Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括: 查询所有:查询出所有的数据,一般测试用,例如:match_all,但有分页限制,一次20条左右 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。常见的有两种

    2024年02月10日
    浏览(53)
  • Elasticsearch Query DSL

    这里使用的 Elasticsearch 的版本为 7.12.1 。 1.1 文档(Document) ElasticSearch 是面向文档的,文档是所有可搜索数据的最小单位,例如 MySQL 的一条数据记录。 文档会被序列化成为 json 格式,保存在 ElasticSearch 中。 每个文档都有一个唯一 ID,例如 MySQL 中的主键 ID。 JSON文档 一篇文档包

    2024年02月15日
    浏览(56)
  • (5)elasticsearch的Query DSL

    官网连接: https://www.elastic.co/guide/en/elasticsearch/reference/7.10/query-filter-context.html Query DSL(Domain Specific Language) query string search 全文检索-fulltext search 精准匹配-term match 过滤器-filter 组合查询-bool query ​ 使用query进行检索,倾向于相关度搜索,故需要计算评分。搜索是Elastics

    2024年02月05日
    浏览(52)
  • 一起学Elasticsearch系列-Query DSL

    本文已收录至Github,推荐阅读 👉 Java随想录 微信公众号:Java随想录 DSL是Domain Specific Language的缩写,指的是为特定问题领域设计的计算机语言。这种语言专注于某特定领域的问题解决,因而比通用编程语言更有效率。 在Elasticsearch中,DSL指的是Elasticsearch Query DSL,是一种以J

    2024年02月01日
    浏览(62)
  • ElasticSearch级查询Query DSL上

    目录 ES高级查询Query DSL match_all 返回源数据_source 返回指定条数size 分页查询fromsize 指定字段排序sort 术语级别查询 Term query术语查询 Terms Query多术语查询 exists query ids query range query范围查询 prefix query前缀查询 wildcard query通配符查询 fuzzy query模糊查询        ES中提供了一种强大

    2024年02月20日
    浏览(57)
  • ElasticSearch Index查询(Query DSL)

    先贴一个Query DSL的官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html 我平时喜欢查看官方文档,了解数据查询和存储方面的性能优化点,下面是积累的脚本分享。 查询语句格式 查询类型:match_all,match,term,range,fuzzy,bool 等等 查询条件:查询条件会根

    2024年02月07日
    浏览(47)
  • 【ElasticSearch系列-03】ElasticSearch的高级句法查询Query DSL

    ElasticSearch系列整体栏目 内容 链接地址 【一】ElasticSearch下载和安装 https://zhenghuisheng.blog.csdn.net/article/details/129260827 【二】ElasticSearch概念和基本操作 https://blog.csdn.net/zhenghuishengq/article/details/134121631 【二】ElasticSearch的高级查询Query DSL https://blog.csdn.net/zhenghuishengq/article/details/1

    2024年02月06日
    浏览(56)
  • 【ElasticSearch-基础篇】ES高级查询Query DSL全文检索

    和术语级别查询(Term-Level Queries)不同,全文检索查询(Full Text Queries)旨在 基于相关性搜索和匹配文本数据 。这些查询会对输入的文本进行分析,将其 拆分 为词项(单个单词),并执行诸如分词、词干处理和标准化等操作。 全文检索的关键特点: 对输入的文本进行分析

    2024年01月22日
    浏览(53)
  • 15.Elasticsearch 7.15 Query DSL 之 Wildcard查询、Regexp查询

    返回包含与通配符模式匹配的文档。 以下搜索返回 user.id 字段包含以 ki 开头并以 y 结尾的文档。这些匹配项可以包括 kiy、kity 或 kimchy (必填, 对象) 你想查询的字段 参数名 描述 boost (Optional, float) 用于降低或提高查询相关性得分的浮点数。默认为1.0。 rewrite (可选,字符串)

    2023年04月08日
    浏览(45)
  • 【ElasticSearch-基础篇】ES高级查询Query DSL术语级别查询并结合springboot使用

    Elasticsearch 提供了基于 JSON 的完整 Query DSL(Domain Specific Language)来定义查询。 因Query DSL是利用Rest API传递JSON格式的请求体(RequestBody)数据与ES进行交互,所以我们在使用springboot的时候也可以很方便的进行集成,本文主要讲述的就是使用springboot实现各类DSL的语法查询。 Elastics

    2024年02月01日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包