_search/查询:

  1. GET /_search/
  2. {
  3. "took" : 7, //执行整个搜索请求耗费了多少毫秒
  4. "timed_out" : false, //查询是否超时
  5. "_shards" : {
  6. "total" : 7, //查询中参与分片的总数
  7. "successful" : 7,
  8. "skipped" : 0,
  9. "failed" : 0
  10. },
  11. "hits" : { //查询结果的前十个文档
  12. "total" : {
  13. "value" : 10000, //匹配到的文档总数
  14. "relation" : "gte"
  15. },
  16. "max_score" : 1.0, //查询所匹配文档的 _score 的最大值
  17. "hits" : [
  18. {
  19. "_index" : "test",
  20. "_type" : "_doc",
  21. "_id" : "3",
  22. "_score" : 1.0, //文档与查询的匹配程度
  23. "_source" : {
  24. "name" : "wangwu",
  25. "age" : 25,
  26. "gender" : "women",
  27. "birthday" : "2000-10-10"
  28. },{...}]}}
  29. 查询last_name为Smith的文档
  30. GET /megacorp/_search?q=last_name:Smith&pretty
  31. GET /megacorp/_search
  32. {
  33. "query": {
  34. "match": {
  35. "last_name": "Smith"
  36. }
  37. }
  38. }
  39. 查询last_name为Smith,age大于等于10小于等于30的文档
  40. GET /megacorp/_search
  41. {
  42. "query": {
  43. "bool": {
  44. "must": [
  45. {
  46. "match": {
  47. "last_name": "smith"
  48. }
  49. }
  50. ],
  51. "filter": {
  52. "range": {
  53. "age": {
  54. "gte": 10,
  55. "lte": 30
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }

/_search

在所有的索引中搜索所有的类型

/gb/_search

在 gb 索引中搜索所有的类型

/gb,us/_search

在 gb 和 us 索引中搜索所有的文档

/g,u/_search

在任何以 g 或者 u 开头的索引中搜索所有的类型

timed_out设置超时时间:

GET /_search?timeout=10ms
在请求超时之前,Elasticsearch 将会返回已经成功从每个分片获取的结果。
应当注意的是 timeout 不是停止执行查询,它仅仅是告知正在协调的节点返回到目前为止收集的结果并且关闭连接。在后台,其他的分片可能仍在执行查询即使是结果已经被发送了。
使用超时是因为 SLA(服务等级协议)对你是很重要的,而不是因为想去中止长时间运行的查询。

highlight高亮搜索:

  1. GET /megacorp/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "about": "rock"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "about": {}
  11. }
  12. }
  13. }

_mapping查看类型映射:

  1. GET /.test/_mapping
  2. 设置预加载以便interests使用聚合查询不用加上.keyword:
  3. PUT /employee/_mapping
  4. {
  5. "properties":{
  6. "interests":{
  7. "type":"text",
  8. "fielddata":true
  9. }
  10. }
  11. }

aggs聚合统计:

  1. interests字段出现次数排名
  2. GET /employee/_search?pretty
  3. {
  4. "aggs": {
  5. "all_interests": {
  6. "terms": { "field": "interests" }
  7. }
  8. }
  9. }
  10. 聚合统计last_name为Smith的索引,interests字段出现次数排名
  11. GET /megacorp/_search
  12. {
  13. "query": {
  14. "match": {
  15. "last_name": "smith"
  16. }
  17. },
  18. "size": 0 ,
  19. "aggs": {
  20. "all_interests": {
  21. "terms": {
  22. "field": "interests.keyword",
  23. "size": 10
  24. }
  25. }
  26. }
  27. }

aggs多个聚合:

  1. GET /megacorp/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "all_interests": {
  6. "terms": {
  7. "field": "interests.keyword"
  8. },
  9. "aggs": {
  10. "avg_avg": {
  11. "avg": {
  12. "field": "age"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

op_type插入已存在数据返回错误信息:

  1. PUT /website/_doc/126?op_type=create
  2. PUT /website/_doc/126/_create

version外部版本号控制乐观并发:

  1. PUT /website/_doc/123?version=10&version_type=external
  2. {
  3. "title": "My first blog entry",
  4. "text": "Starting to get the hang of this..."
  5. }

_mget取多个文档

不同索引写法:

  1. GET /_mget
  2. {
  3. "docs":[
  4. {
  5. "_index":"website",
  6. "_id":124
  7. },
  8. {
  9. "_index":"website",
  10. "_id":1,
  11. "_source":"text"
  12. }
  13. ]
  14. }

相同索引写法:

  1. GET /website/_mget
  2. {
  3. "docs":[
  4. {
  5. "_id":124
  6. },
  7. {
  8. "_id":1
  9. }
  10. ]
  11. }

通过id查询

  1. GET /website/_mget
  2. {
  3. "ids":[124,1]
  4. }

_bulk批量操作

不同索引写法:

  1. POST /_bulk
  2. {"delete":{"_index":"website","_id":124}}
  3. {"create":{"_index":"website","_id":220}}
  4. { "title": "My first blog post" }
  5. { "index": { "_index": "website"}}
  6. { "title": "My second blog post" }
  7. {"update":{"_index":"website","_id":2}}
  8. {"doc":{"tittle":"sba"}}

相同索引写法:

  1. POST /website/_bulk
  2. {"index":{}}
  3. {"name":"lisi"}
  4. {"update":{"_id":2}}
  5. {"doc":{"size":1}}

multi_match多字段执行相同match 查询:

  1. GET /_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": 20,
  6. "fields": ["age","size"]
  7. }
  8. }
  9. }

range 查询指定区间:

  1. GET /_search
  2. {
  3. "query": {
  4. "range": {
  5. "age": {
  6. "gte": 20,
  7. "lte": 24
  8. }
  9. }
  10. }
  11. }

term 查询

精确匹配:

  1. GET /_search
  2. {
  3. "query": {
  4. "term": {
  5. "gender": "women"
  6. }
  7. }
  8. }

指定多值进行匹配:

  1. GET /_search
  2. {
  3. "query": {
  4. "terms": {
  5. "gender": [
  6. "men",
  7. "women"
  8. ]
  9. }
  10. }
  11. }

exists查询

字段有值:

  1. GET /test/_search
  2. {
  3. "query": {
  4. "exists": {
  5. "field":"tittle"
  6. }
  7. }
  8. }

字段缺值:

  1. GET /test/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": [
  6. {
  7. "exists": {
  8. "field": "tittle"
  9. }
  10. }
  11. ]
  12. }
  13. }
  14. }

bool 查询

must:必须 匹配

文档 必须 匹配这些条件才能被包含进来。

must_not:必须不 匹配

文档 必须不 匹配这些条件才能被包含进来。

should:满足任意语句增加 _score

如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。

filter:必须 匹配,但它以不评分、过滤模式

必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

  1. 下面的查询用于查找 field字段存在的文档。Gender字段匹配 men的文档将比其他文档拥有更高的排名
  2. GET /test/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "exists": {
  9. "field": "tittle"
  10. }
  11. }
  12. ],
  13. "should": [
  14. {
  15. "match": {
  16. "gender": "men"
  17. }
  18. }]}}}
  19. GET /test/_search
  20. {"query": {
  21. "bool": {
  22. "must": [
  23. {
  24. "exists": {
  25. "field": "tittle"
  26. }
  27. }
  28. ],
  29. "should": [
  30. {
  31. "exists": {
  32. "field": "tittle"
  33. }
  34. },
  35. {
  36. "range": {
  37. "birthday": {
  38. "gte": "1998-09-01"
  39. }
  40. }
  41. }]}}}
  42. 用 filter 语句来重写,去掉计算得分对文档查询时间的影响
  43. GET /test/_search
  44. {
  45. "query": {
  46. "bool": {
  47. "must": [
  48. {
  49. "exists": {
  50. "field": "tittle"
  51. }
  52. }
  53. ],
  54. "should": [
  55. {
  56. "exists": {
  57. "field": "tittle"
  58. }
  59. }
  60. ],
  61. "filter": {
  62. "range": {
  63. "birthday": {
  64. "gte": "1998-09-01"
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }

constant_score 查询:

  1. 可以使用它来取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
  2. GET /.test/_search
  3. {
  4. "query": {
  5. "constant_score": {
  6. "filter": {
  7. "term": {
  8. "gender": "men"
  9. }
  10. }
  11. }
  12. }
  13. }
  14. 在 constant_score 查询中,它可以包含查询或过滤,为任意一个匹配的文档指定评分 1 ,忽略 TF/IDF 信息:
  15. GET /_search
  16. {
  17. "query": {
  18. "bool": {
  19. "should": [
  20. { "constant_score": {
  21. "query": { "match": { "description": "wifi" }}
  22. }},
  23. { "constant_score": {
  24. "query": { "match": { "description": "garden" }}
  25. }},
  26. { "constant_score": {
  27. "query": { "match": { "description": "pool" }}
  28. }}
  29. ]
  30. }
  31. }
  32. }
  33. 或许不是所有的设施都同等重要——对某些用户来说有些设施更有价值。
  34. 如果最重要的设施是游泳池,那我们可以为更重要的设施增加权重:
  35. GET /_search
  36. {
  37. "query": {
  38. "bool": {
  39. "should": [
  40. { "constant_score": {
  41. "query": { "match": { "description": "wifi" }}
  42. }},
  43. { "constant_score": {
  44. "query": { "match": { "description": "garden" }}
  45. }},
  46. { "constant_score": {
  47. "boost": 2
  48. "query": { "match": { "description": "pool" }}
  49. }}
  50. ]
  51. }
  52. }
  53. }

validate-query验证查询是否合法:

  1. GET /.test/_validate/query
  2. {
  3. "query": {
  4. "match": {
  5. "gender": "men"
  6. }
  7. }
  8. }

explain查询不合法的原因:

  1. GET /.test/_validate/query?explain
  2. {
  3. "query": {
  4. "match": {
  5. "gender": "men women"
  6. }
  7. }
  8. }

sort排序:

  1. GET /_search
  2. {
  3. "query": {
  4. "match": {
  5. "gender": "men"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "birthday": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

sort多级排序:

  1. GET /_search
  2. {
  3. "query": {
  4. "match": {
  5. "gender": "men women"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "age": {
  11. "order": "desc"
  12. }
  13. },
  14. {
  15. "birthday": {
  16. "order": "asc"
  17. }
  18. }
  19. ]
  20. }

consistency一致性:

默认规定数量即:
int( (primary + number_of_replicas) / 2 ) + 1
注:number_of_replicas 指的是在索引设置中的设定副本分片数,如当前索引拥有三个副本分片,那规定数量的计算结果即:
int( (primary + 3 replicas) / 2 ) + 1 = 3
此时你只启动两个节点,那么处于活跃状态的分片副本数量就达不到规定数量,也因此您将无法索引和删除任何文档。
consistency 参数的值可以设为 one (只要主分片状态 ok 就允许执行
写操作),all(必须要主分片和所有副本分片的状态没问题才允许执行写操作), 或 quorum 。默认值为 quorum , 即大多数的分片副本状态没问题就允许执行写_操作。

size&from分页:

size 显示应该返回的结果数量,默认是 10
from 显示应该跳过的初始结果数量,默认是 0

GET /_search?size=5 查询前5条数据
GET /_search?size=5&from=5 每页显示5条数据,查询第5页

分析器

字符过滤器:

首先,字符串按顺序通过每个 字符过滤器 。他们的任务是在分词前整理字符串。一个字符过滤器可以用来去掉HTML,或者将 & 转化成 and。

分词器:

其次,字符串被 分词器 分为单个的词条。一个简单的分词器遇到空格和标点的时候,可能会将文本拆分成词条。

Token过滤器:

最后,词条按顺序通过每个 token 过滤器 。这个过程可能会改变词条(例如,小写化 Quick ),删除词条(例如, 像 a, and, the 等无用词),或者增加词条(例如,像 jump 和 leap 这种同义词)。

内置分析器

“Set the shape to semi-transparent by calling set_trans(5)”

标准分析器:

标准分析器是Elasticsearch默认使用的分析器。它是分析各种语言文本最常用的选择。它根据 Unicode联盟定义的单词边界划分文本。删除绝大部分标点。最后,将词条小写。它会产生
set, the, shape, to, semi, transparent, by, calling, set_trans, 5

简单分析器:

简单分析器在任何不是字母的地方分隔文本,将词条小写。它会产生
set, the, shape, to, semi, transparent, by, calling, set, trans

空格分析器:

空格分析器在空格的地方划分文本。它会产生
Set, the, shape, to, semi-transparent, by, calling, set_trans(5)

语言分析器:

特定语言分析器可用于 很多语言。它们可以考虑指定语言的特点。例如, 英语 分析器附带了一组英语无用词(常用单词,例如and或者the,它们对相关性没有多少影响),它们会被删除。由于理解英语语法的规则,这个分词器可以提取英语单词的词干 。
英语 分词器会产生下面的词条:
set, shape, semi, transpar, call, set_tran, 5
注意看 transparent、 calling 和 set_trans 已经变为词根格式。

type简单域类型

字符串类型:

string 5.x后不支持
text 全文检索内容,字段内容会被分析为一个个词项
keyword 结构化字段

整数类型:

byte, short, integer, long

浮点类型:

double,float ,half_float,scaled_float

date类型:

内部会将日期数据转换为UTC,并存储为milliseconds-since-the-epoch的long型整数。

boolean类型:

binary类型:

用base64来存储二进制形式的数据,例如图像。默认情况下,该类型的字段只存储不索引。二进制类型只支持index_name属性。

array类型:

object类型:

ip类型:

ip类型的字段用于存储IPv4或者IPv6的地址

-设置字段type

  1. PUT /.test1
  2. {
  3. "settings": {
  4. "number_of_shards": 3,
  5. "number_of_replicas": 2
  6. },
  7. "mappings": {
  8. "properties": {
  9. "tittle":{
  10. "type": "text"
  11. }
  12. }
  13. }
  14. }

index控制索引

analyzed:

首先分析字符串,然后索引它。换句话说,以全文索引这个域。

not_analyzed:

索引这个域,所以它能够被搜索,但索引的是精确值。不会对它进行分析。

no:

不索引这个域。这个域不会被搜索到。

analyze查看文本分析:

  1. GET /_analyze
  2. {
  3. "analyzer": "standard",
  4. "text":"Text to analyze"
  5. }
  6. 返回:
  7. {
  8. "tokens" : [
  9. {
  10. "token" : "text", //实际存储到索引中的词条
  11. "start_offset" : 0, //字符在原始字符串中的起始位置
  12. "end_offset" : 4, //字符在原始字符串中的结束位置
  13. "type" : "<ALPHANUM>",
  14. "position" : 0 //词条在原始文本中出现的位置
  15. },
  16. {
  17. "token" : "to",
  18. "start_offset" : 5,
  19. "end_offset" : 7,
  20. "type" : "<ALPHANUM>",
  21. "position" : 1
  22. },......
  23. ]
  24. }


refresh API:

  1. 刷新所有的索引:
  2. POST /_refresh
  3. 只刷新my_index索引:
  4. POST /_refreshogs/_refresh
  5. refresh_interval设置刷新频率:
  6. PUT /my_index
  7. {
  8. "settings": {
  9. "refresh_interval": "30s"
  10. }
  11. }
  12. 关闭自动刷新
  13. PUT /my_index/_settings
  14. {
  15. "refresh_interval":-1
  16. }
  17. 每秒自动刷新
  18. PUT /my_index/_settings
  19. {
  20. "refresh_interval":"1s"
  21. }
  22. refresh_interval 需要一个持续时间值,例如 1s(1 秒)或2m(2分钟)。
  23. 一个绝对值1表示的是1毫秒 --无疑会使你的集群陷入瘫痪

flush API:

  1. 这个执行一个提交并且截断 translog 的行为在 Elasticsearch 被称作一次 flush .
  2. 分片每30分钟被自动刷新(flush),或者在 translog 太大的时候也会刷新
  3. 刷新my_index索引:
  4. POST /my_index/_flush
  5. 刷新所有的索引并且等待所有刷新在返回前完成:
  6. POST /_flush?wait_if_ongoing

提高精度

operator :

  1. GET /.test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "msg": {
  6. "query": "pig egg",
  7. "operator": "and"
  8. }
  9. }
  10. }
  11. }

minimum_should_match:

  1. GET /.test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "msg": {
  6. "query": "pig egg",
  7. "minimum_should_match": "100%"
  8. }
  9. }
  10. }
  11. }
  12. GET /.test/_search
  13. {
  14. "query": {
  15. "bool": {
  16. "should": [
  17. {"match": {
  18. "msg": "pig"
  19. }},
  20. {
  21. "match": {
  22. "msg": "egg"
  23. }
  24. }
  25. ],
  26. "minimum_should_match": 2
  27. }
  28. }
  29. }

boost权重:

  1. GET /.test/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {"match": {
  7. "msg": "pig"
  8. }}
  9. ],
  10. "should": [
  11. {"match": {
  12. "msg":{
  13. "query": "and",
  14. "boost": 2
  15. }
  16. }},
  17. {
  18. "match": {
  19. "msg": {
  20. "query": "an",
  21. "boost": 3
  22. }
  23. }
  24. }
  25. ]
  26. }
  27. }
  28. }

indices_boost:

  1. GET /docs_2014_*/_search
  2. {
  3. "indices_boost": {
  4. "docs_2014_10": 3,
  5. "docs_2014_09": 2
  6. },
  7. "query": {
  8. "match": {
  9. "text": "quick brown fox"
  10. }
  11. }
  12. }
  13. 这个多索引查询涵盖了所有以字符串 docs_2014_ 开始的索引。
  14. 其中,索引 docs_2014_10 中的所有文件的权重是 3 ,索引 docs_2014_09 中是 2 ,
  15. 其他所有匹配的索引权重为默认值 1 。

boosting 查询:

  1. GET /_search
  2. {
  3. "query": {
  4. "boosting": {
  5. "positive": {
  6. "match": {
  7. "text": "apple"
  8. }
  9. },
  10. "negative": {
  11. "match": {
  12. "text": "pie tart fruit crumble tree"
  13. }
  14. },
  15. "negative_boost": 0.5
  16. }
  17. }
  18. }
  19. 它接受 positive 和 negative 查询。
  20. 只有那些匹配 positive 查询的文档罗列出来,
  21. 对于那些同时还匹配 negative 查询的文档将通过文档的原始 _score 与 negative_boost 相乘的方式降级后的结果。
  22. 为了达到效果, negative_boost 的值必须小于 1.0 。
  23. 在这个示例中,所有包含负向词的文档评分 _score 都会减半。

dis_max查询:

  1. {
  2. "query": {
  3. "dis_max": {
  4. "queries": [
  5. { "match": { "title": "Brown fox" }},
  6. { "match": { "body": "Brown fox" }}
  7. ]
  8. }
  9. }
  10. }

tie_breaker:

  1. 将其他匹配语句的评分也考虑其中:
  2. {
  3. "query": {
  4. "dis_max": {
  5. "queries": [
  6. { "match": { "title": "Quick pets" }},
  7. { "match": { "body": "Quick pets" }}
  8. ],
  9. "tie_breaker": 0.3
  10. }
  11. }
  12. }

模糊匹配:

  1. {
  2. "multi_match": {
  3. "query": "Quick brown fox",
  4. "fields": "*_title"
  5. }
  6. }

使用 ^ 字符为单个字段提升权重:

  1. {
  2. "multi_match": {
  3. "query": "Quick brown fox",
  4. "fields": [ "*_title", "chapter_title^2" ]
  5. }
  6. }

chapter_title 这个字段的 boost 值为 2 ,而其他两个字段 book_title 和 section_title 字段的默认 boost 值为 1

copy_to:

  1. PUT /my_index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "first_name":{
  6. "type": "text",
  7. "copy_to": "full_name"
  8. },
  9. "last_name":{
  10. "type": "text",
  11. "copy_to": "full_name"
  12. },
  13. "full_name":{
  14. "type": "text"
  15. }
  16. }
  17. }
  18. }

cross_fields 跨字段查询:

  1. peter 和 smith 都必须同时出现在相同字段中:
  2. GET /_validate/query?explain
  3. {
  4. "query": {
  5. "multi_match": {
  6. "query": "peter smith",
  7. "type": "most_fields",
  8. "operator": "and",
  9. "fields": [ "first_name", "last_name" ]
  10. }
  11. }
  12. }
  13. 同时在 first_name 和 last_name 两个字段中查找 smith 的 IDF ,然后用两者的最小值作为两个字段的 IDF
  14. GET /_validate/query?explain
  15. {
  16. "query": {
  17. "multi_match": {
  18. "query": "peter smith",
  19. "type": "cross_fields",
  20. "operator": "and",
  21. "fields": [ "first_name", "last_name" ]
  22. }
  23. }
  24. }

match_phrase 短语匹配:

对词语位置敏感的查询, 就可以利用位置信息去匹配包含所有查询词项,且各词项顺序也与我们搜索指定一致的文档,中间不夹杂其他词项

  1. GET /my_index/my_type/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": "quick brown fox"
  6. }
  7. }
  8. }

slop 查询词条相隔多远时仍然能将文档视为匹配:

  1. GET /my_index/_doc/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "quick fox",
  7. "slop": 1
  8. }
  9. }
  10. }
  11. }
  12. 例:为了使查询 fox quick 匹配我们的文档, 我们需要 slop 的值为 3:
  13. Pos 1 Pos 2 Pos 3
  14. -----------------------------------------------
  15. Doc: quick brown fox
  16. -----------------------------------------------
  17. Query: fox quick
  18. Slop 1: fox|quick ↵
  19. Slop 2: quick ↳ fox
  20. Slop 3: quick ↳ fox
  21. 注意:fox 和 quick 在这步中占据同样的位置。 因此将 fox quick 转换顺序成 quick fox 需要两步,
  22. 或者值为 2 的 slop 。

position_increment_gap 增加当前词条 position 的指定值:

  1. DELETE /my_index/groups/
  2. PUT /my_index/_mapping/groups
  3. {
  4. "properties": {
  5. "names": {
  6. "type": "string",
  7. "position_increment_gap": 100
  8. }
  9. }
  10. }
  11. 索引 names 数组时,会产生如下的结果:
  12. Position 1: john
  13. Position 2: abraham
  14. Position 103: lincoln
  15. Position 104: smith
  16. 现在我们的短语查询可能无法匹配该文档因为 abraham 和 lincoln 之间的距离为 100 。
  17. 为了匹配这个文档你必须添加值为 100 的 slop 。

shingles 提高相关度评分:

  1. 匹配相邻词项
  2. GET /my_index/my_type/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": {
  7. "match": {
  8. "title": "the hungry alligator ate sue"
  9. }
  10. },
  11. "should": {
  12. "match": {
  13. "title.shingles": "the hungry alligator ate sue"
  14. }
  15. }
  16. }
  17. }
  18. }

wildcard 通配符查询:

使用标准的 shell 通配符查询: ? 匹配任意字符, * 匹配 0 或多个字符。

  1. GET /my_index/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "full_name": "k?d*"
  6. }
  7. }
  8. }

regexp 正则式查询:

  1. 匹配只以 W 开始并跟随一个数字的索引:
  2. GET /my_index/_search
  3. {
  4. "query": {
  5. "regexp": {
  6. "full_name": "k[0-9].+"
  7. }
  8. }
  9. }
  10. 避免使用左通配这样的模式匹配(如: *foo 或 .*foo 这样的正则式)

match_phrase_prefix 输入即搜索:

  1. GET /my_index/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "full_name": {
  6. "query": "k",
  7. "max_expansions": 10
  8. }
  9. }
  10. }
  11. }
  12. max_expansions 控制与前缀匹配的词的数量,它会先查找第一个与前缀 bl 匹配的词,
  13. 然后依次查找搜集与之匹配的词(按字母顺序),直到没有更多可匹配的词或当数量超过 max_expansions 时结束。

index_options词频:

  1. 词频:
  2. 词在文档中出现的频度是多少?频度越高,权重 越高 。 5 次提到同一词的字段比只提到 1 次的更相关。
  3. 词频的计算方式如下:
  4. tf(t in d) = √frequency
  5. 词 t 在文档 d 的词频( tf )是该词在文档中出现次数的平方根。
  6. 如果不在意词在某个字段中出现的频次,而只在意是否出现过,则可以在字段映射中禁用词频统计:
  7. PUT /my_index
  8. {
  9. "mappings": {
  10. "doc": {
  11. "properties": {
  12. "text": {
  13. "type": "string",
  14. "index_options": "docs"
  15. }
  16. }
  17. }
  18. }
  19. }
  20. 将参数 index_options 设置为 docs 可以禁用词频统计及词频位置,这个映射的字段不会计算词的出现次数,
  21. 对于短语或近似查询也不可用。要求精确查询的 not_analyzed 字符串字段会默认使用该设置。

idf逆向文档频率:

词在集合所有文档里出现的频率是多少?频次越高,权重 越低 。
常用词如 and 或 the 对相关度贡献很少,因为它们在多数文档中都会出现,
一些不常见词如 elastic 或 hippopotamus 可以帮助我们快速缩小范围找到感兴趣的文档。
逆向文档频率的计算公式如下:
idf(t) = 1 + log ( numDocs / (docFreq + 1))
词 t 的逆向文档频率( idf )是:索引中文档数量除以所有包含该词的文档数,然后求其对数。

norm字段长度归一值:

  1. 字段的长度是多少?字段越短,字段的权重 越高 。如果词出现在类似标题 title 这样的字段,
  2. 要比它出现在内容 body 这样的字段中的相关度更高。字段长度的归一值公式如下:
  3. norm(d) = 1 / √numTerms
  4. 字段长度归一值( norm )是字段中词数平方根的倒数。
  5. PUT /my_index
  6. {
  7. "mappings": {
  8. "doc": {
  9. "properties": {
  10. "text": {
  11. "type": "string",
  12. "norms": { "enabled": false }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. 这个字段不会将字段长度归一值考虑在内,长字段和短字段会以相同长度计算评分。
  19. 对于有些应用场景如日志,归一值不是很有用,
  20. 要关心的只是字段是否包含特殊的错误码或者特定的浏览器唯一标识符。字段的长度对结果没有影响,
  21. 禁用归一值可以节省大量内存空间。

function_score、field_value_factor相关度排序依据:

  1. 设想有个网站供用户发布博客并且可以让他们为自己喜欢的博客点赞,
  2. 我们希望将更受欢迎的博客放在搜索结果列表中相对较上的位置,
  3. 同时全文搜索的评分仍然作为相关度的主要排序依据,可以简单的通过存储每个博客的点赞数来实现它:
  4. PUT /blogposts/post/1
  5. {
  6. "title": "About popularity",
  7. "content": "In this post we will talk about...",
  8. "votes": 6
  9. }
  10. 在搜索时,可以将 function_score 查询与 field_value_factor 结合使用,即将点赞数与全文相关度评分结合:
  11. GET /blogposts/post/_search
  12. {
  13. "query": {
  14. "function_score": {
  15. "query": {
  16. "multi_match": {
  17. "query": "popularity",
  18. "fields": [ "title", "content" ]
  19. }
  20. },
  21. "field_value_factor": {
  22. "field": "votes"
  23. }
  24. }
  25. }
  26. }

modifier受欢迎度:

一种融入受欢迎度更好方式是用 modifier 平滑 votes 的值。换句话说,我们希望最开始的一些赞更重要,但是其重要性会随着数字的增加而降低。 0 个赞与 1 个赞的区别应该比 10 个赞与 11 个赞的区别大很多。
对于上述情况,典型的 modifier 应用是使用 log1p 参数值,公式如下:
new_score = old_score * log(1 + number_of_votes)

  1. GET /blogposts/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match": {
  7. "query": "popularity",
  8. "fields": ["title","content"]
  9. }
  10. },
  11. "field_value_factor": {
  12. "field": "votes",
  13. "modifier": "log1p"
  14. }
  15. }
  16. }
  17. }
修饰符 含义
none 不要对字段值应用任何乘数
log 取字段值的常用对数
log1p 将1加到字段值并取对数
log2p 在字段值上加2并取公共对数
ln 取字段值的自然对数
ln1p 将1加到栏位值并取自然对数
ln2p 将2加到字段值上并取自然对数
square 对字段值求平方(乘以它本身)
sqrt 取字段值的平方根
reciprocal 报答字段值,同1/x那里x是该字段的值

factor受欢迎程度:

  1. GET /blogposts/post/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match": {
  7. "query": "popularity",
  8. "fields": [ "title", "content" ]
  9. }
  10. },
  11. "field_value_factor": {
  12. "field": "votes",
  13. "modifier": "log1p",
  14. "factor": 2
  15. }
  16. }
  17. }
  18. }
  19. 双倍效果:
  20. 添加了 factor 会使公式变成这样:
  21. new_score = old_score * log(1 + factor * number_of_votes)
  22. factor 值大于 1 会提升效果, factor 值小于 1 会降低效果。

boost_mode函数:

或许将全文评分与 field_value_factor 函数值乘积的效果仍然可能太大,我们可以通过参数 boost_mode 来控制函数与查询评分 _score 合并后的结果,参数接受的值为:
multiply
评分 _score 与函数值的积(默认)
sum
评分 _score 与函数值的和
min
评分 _score 与函数值间的较小值
max
评分 _score 与函数值间的较大值
replace
函数值替代评分 _score
与使用乘积的方式相比,使用评分 _score 与函数值求和的方式可以弱化最终效果,特别是使用一个较小 factor 因子时:

  1. GET /blogposts/post/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match": {
  7. "query": "popularity",
  8. "fields": [ "title", "content" ]
  9. }
  10. },
  11. "field_value_factor": {
  12. "field": "votes",
  13. "modifier": "log1p",
  14. "factor": 0.1
  15. },
  16. "boost_mode": "sum"
  17. }
  18. }
  19. }
  20. 将函数计算结果值累加到评分 _score 。
  21. 之前请求的公式现在变成下面这样:
  22. new_score = old_score + log(1 + 0.1 * number_of_votes)

max_boost限制函数的最大效果:

  1. GET /blogposts/post/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match": {
  7. "query": "popularity",
  8. "fields": [ "title", "content" ]
  9. }
  10. },
  11. "field_value_factor": {
  12. "field": "votes",
  13. "modifier": "log1p",
  14. "factor": 0.1
  15. },
  16. "boost_mode": "sum",
  17. "max_boost": 1.5
  18. }
  19. }
  20. }
  21. 无论 field_value_factor 函数的结果如何,最终结果都不会大于 1.5 ;
  22. max_boost 只对函数的结果进行限制,不会对最终评分 _score 产生直接影响。

weight过滤提成权重:

  1. GET /_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "filter": {
  6. "term": { "city": "Barcelona" }
  7. },
  8. "functions": [
  9. {
  10. "filter": { "term": { "features": "wifi" }},
  11. "weight": 1
  12. },
  13. {
  14. "filter": { "term": { "features": "garden" }},
  15. "weight": 1
  16. },
  17. {
  18. "filter": { "term": { "features": "pool" }},
  19. "weight": 2
  20. }
  21. ],
  22. "score_mode": "sum",
  23. }
  24. }
  25. }

过滤 vs. 查询

首先要注意的是 filter 过滤器代替了 query 查询,在本例中,我们无须使用全文搜索,只想找到 city 字段中包含 Barcelona 的所有文档,逻辑用过滤比用查询表达更清晰。过滤器返回的所有文档的评分 _score 的值为 1 。 function_score 查询接受 query 或 filter ,如果没有特别指定,则默认使用 match_all 查询。

函数 functions

functions 关键字保持着一个将要被使用的函数列表。可以为列表里的每个函数都指定一个 filter 过滤器,在这种情况下,函数只会被应用到那些与过滤器匹配的文档,例子中,我们为与过滤器匹配的文档指定权重值 weight 为 1 (为与 pool 匹配的文档指定权重值为 2 )。

评分模式 score_mode

每个函数返回一个结果,所以需要一种将多个结果缩减到单个值的方式,然后才能将其与原始评分 _score 合并。评分模式 score_mode 参数正好扮演这样的角色,它接受以下值:
multiply函数结果求积(默认)。sum函数结果求和。avg函数结果的平均值。max函数结果的最大值。min函数结果的最小值。first使用首个函数(可以有过滤器,也可能没有)的结果作为最终结果
在本例中,我们将每个过滤器匹配结果的权重 weight 求和,并将其作为最终评分结果,所以会使用 sum 评分模式。
不与任何过滤器匹配的文档会保有其原始评分, _score 值的为 1 。

配置BM25相似度算法:

  1. PUT /my_index
  2. {
  3. "settings": {
  4. "similarity": {
  5. "my_bm25": {
  6. "type": "BM25",
  7. "b": 0 //禁用字段长度规范化(field-length normalization)
  8. }
  9. }
  10. },
  11. "mappings": {
  12. "doc": {
  13. "properties": {
  14. "title": {
  15. "type": "string",
  16. "similarity": "my_bm25" //自定义相似度算法
  17. },
  18. "body": {
  19. "type": "string",
  20. "similarity": "BM25" //使用内置相似度算法 BM25
  21. }
  22. }
  23. }
  24. }
  25. }
  26. 自定义的相似度算法可以通过关闭索引,更新索引设置,开启索引这个过程进行更新。
  27. 这样可以无须重建索引又能试验不同的相似度算法配置。

terms 桶操作:

  1. 汽车经销商可能会想知道哪个颜色的汽车销量最好,用聚合可以轻易得到结果:
  2. GET /cars/_search
  3. {
  4. "size": 0, //我们并不关心搜索结果的具体内容,所以将返回记录数设置为 0 来提高查询速度。设置 size: 0 与 Elasticsearch 1.x 中使用 count 搜索类型等价。
  5. "aggs": { //聚合操作
  6. "popular_color": { //聚合指定名称
  7. "terms": { //定义单个桶的类型
  8. "field": "color.keyword"
  9. }
  10. }
  11. }
  12. }
  13. 返回:
  14. {
  15. "took" : 5,
  16. "timed_out" : false,
  17. "_shards" : {
  18. "total" : 1,
  19. "successful" : 1,
  20. "skipped" : 0,
  21. "failed" : 0
  22. },
  23. "hits" : {
  24. "total" : {
  25. "value" : 16,
  26. "relation" : "eq"
  27. },
  28. "max_score" : null,
  29. "hits" : [ ] //因为我们设置了 size 参数,所以不会有 hits 搜索结果返回。
  30. },
  31. "aggregations" : {
  32. "popular_color" : { //作为 aggregations 字段的一部分被返回的。
  33. "doc_count_error_upper_bound" : 0,
  34. "sum_other_doc_count" : 0,
  35. "buckets" : [
  36. {
  37. "key" : "red", //每个桶的 key 都与 color 字段里找到的唯一词对应。它总会包含 doc_count 字段,告诉我们包含该词项的文档数量。
  38. "doc_count" : 8 //每个桶的数量代表该颜色的文档数量。
  39. },
  40. {
  41. "key" : "blue",
  42. "doc_count" : 4
  43. },
  44. {
  45. "key" : "green",
  46. "doc_count" : 4
  47. } ] } }}

aggs聚合嵌套:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "colors":{
  6. "terms": {
  7. "field": "color.keyword"
  8. },
  9. "aggs": {
  10. "avg_price":{
  11. "avg": {
  12. "field": "price"
  13. }
  14. },
  15. "make":{
  16. "terms": {
  17. "field": "make.keyword"
  18. },
  19. "aggs": {
  20. "min_price":{
  21. "min": {
  22. "field": "price"
  23. }
  24. },
  25. "max_price":{
  26. "max": {
  27. "field": "price"
  28. } } } } } } }}
  29. 返回结果:
  30. {
  31. "took" : 36,
  32. "timed_out" : false,
  33. "_shards" : {
  34. "total" : 1,
  35. "successful" : 1,
  36. "skipped" : 0,
  37. "failed" : 0
  38. },
  39. "hits" : {
  40. "total" : {
  41. "value" : 16,
  42. "relation" : "eq"
  43. },
  44. "max_score" : null,
  45. "hits" : [ ]
  46. },
  47. "aggregations" : {
  48. "colors" : {
  49. "doc_count_error_upper_bound" : 0,
  50. "sum_other_doc_count" : 0,
  51. "buckets" : [
  52. {
  53. "key" : "red",
  54. "doc_count" : 8,
  55. "avg_price" : {
  56. "value" : 32500.0
  57. },
  58. "make" : {
  59. "doc_count_error_upper_bound" : 0,
  60. "sum_other_doc_count" : 0,
  61. "buckets" : [
  62. {
  63. "key" : "honda",
  64. "doc_count" : 6,
  65. "max_price" : {
  66. "value" : 20000.0
  67. },
  68. "min_price" : {
  69. "value" : 10000.0
  70. }
  71. },
  72. {
  73. "key" : "bmw",
  74. "doc_count" : 2,
  75. "max_price" : {
  76. "value" : 80000.0
  77. },
  78. "min_price" : {
  79. "value" : 80000.0
  80. }
  81. }
  82. ]
  83. }
  84. },
  85. {
  86. "key" : "blue",
  87. "doc_count" : 4,
  88. "avg_price" : {
  89. "value" : 20000.0
  90. },
  91. "make" : {
  92. "doc_count_error_upper_bound" : 0,
  93. "sum_other_doc_count" : 0,
  94. "buckets" : [
  95. {
  96. "key" : "ford",
  97. "doc_count" : 2,
  98. "max_price" : {
  99. "value" : 25000.0
  100. },
  101. "min_price" : {
  102. "value" : 25000.0
  103. }
  104. },
  105. {
  106. "key" : "toyota",
  107. "doc_count" : 2,
  108. "max_price" : {
  109. "value" : 15000.0
  110. },
  111. "min_price" : {
  112. "value" : 15000.0
  113. }
  114. }
  115. ]
  116. }
  117. },
  118. {
  119. "key" : "green",
  120. "doc_count" : 4,
  121. "avg_price" : {
  122. "value" : 21000.0
  123. },
  124. "make" : {
  125. "doc_count_error_upper_bound" : 0,
  126. "sum_other_doc_count" : 0,
  127. "buckets" : [
  128. {
  129. "key" : "ford",
  130. "doc_count" : 2,
  131. "max_price" : {
  132. "value" : 30000.0
  133. },
  134. "min_price" : {
  135. "value" : 30000.0
  136. }
  137. },
  138. {
  139. "key" : "toyota",
  140. "doc_count" : 2,
  141. "max_price" : {
  142. "value" : 12000.0
  143. },
  144. "min_price" : {
  145. "value" : 12000.0
  146. } } ] } } ] } }}

条形图统计:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "price":{
  6. "histogram": { //价格区间,每个区间2万
  7. "field": "price",
  8. "interval": 20000
  9. },
  10. "aggs": {
  11. "revenue":{
  12. "sum": { //区间价格合计
  13. "field": "price"
  14. } } } } }}
  15. 返回结果:
  16. {
  17. "took" : 3,
  18. "timed_out" : false,
  19. "_shards" : {
  20. "total" : 1,
  21. "successful" : 1,
  22. "skipped" : 0,
  23. "failed" : 0
  24. },
  25. "hits" : {
  26. "total" : {
  27. "value" : 16,
  28. "relation" : "eq"
  29. },
  30. "max_score" : null,
  31. "hits" : [ ]
  32. },
  33. "aggregations" : {
  34. "price" : {
  35. "buckets" : [
  36. {
  37. "key" : 0.0, //区间1
  38. "doc_count" : 6, //数据条数
  39. "revenue" : {
  40. "value" : 74000.0 //sum总和
  41. }
  42. },
  43. {
  44. "key" : 20000.0,
  45. "doc_count" : 8,
  46. "revenue" : {
  47. "value" : 190000.0
  48. }
  49. }......}

image.png

extended_stats计算:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "make":{
  6. "terms": {
  7. "field": "make.keyword",
  8. "size": 10
  9. },
  10. "aggs": {
  11. "stats":{
  12. "extended_stats": {
  13. "field": "price"
  14. } } } } }}
  15. 返回结果:
  16. {
  17. "took" : 31,
  18. "timed_out" : false,
  19. "_shards" : {
  20. "total" : 1,
  21. "successful" : 1,
  22. "skipped" : 0,
  23. "failed" : 0
  24. },
  25. "hits" : {
  26. "total" : {
  27. "value" : 16,
  28. "relation" : "eq"
  29. },
  30. "max_score" : null,
  31. "hits" : [ ]
  32. },
  33. "aggregations" : {
  34. "make" : {
  35. "doc_count_error_upper_bound" : 0,
  36. "sum_other_doc_count" : 0,
  37. "buckets" : [
  38. {
  39. "key" : "honda",
  40. "doc_count" : 6,
  41. "stats" : {
  42. "count" : 6,
  43. "min" : 10000.0,
  44. "max" : 20000.0,
  45. "avg" : 16666.666666666668, //平均值
  46. "sum" : 100000.0,
  47. "sum_of_squares" : 1.8E9, //平方和
  48. "variance" : 2.222222222222221E7, //方差
  49. "std_deviation" : 4714.045207910315, //标准偏差
  50. "std_deviation_bounds" : { //标准偏差界限
  51. "upper" : 26094.757082487296,
  52. "lower" : 7238.5762508460375
  53. }
  54. }
  55. }......}

date_histogram时间统计:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "sales":{
  6. "date_histogram": {
  7. "field": "sold", //查询日期字段
  8. "calendar_interval":"month", //按月buckets
  9. "format": "yyyy-MM-dd" //格式化
  10. }
  11. }
  12. }
  13. }
  14. 返回结果:
  15. {
  16. "took" : 3,
  17. "timed_out" : false,
  18. "_shards" : {
  19. "total" : 1,
  20. "successful" : 1,
  21. "skipped" : 0,
  22. "failed" : 0
  23. },
  24. "hits" : {
  25. "total" : {
  26. "value" : 16,
  27. "relation" : "eq"
  28. },
  29. "max_score" : null,
  30. "hits" : [ ]
  31. },
  32. "aggregations" : {
  33. "sales" : {
  34. "buckets" : [
  35. {
  36. "key_as_string" : "2014-01-01",
  37. "key" : 1388534400000,
  38. "doc_count" : 2
  39. },
  40. {
  41. "key_as_string" : "2014-02-01",
  42. "key" : 1391212800000,
  43. "doc_count" : 2
  44. }......
  45. }

返回空buckets:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "sales":{
  6. "date_histogram": {
  7. "field": "sold",
  8. "calendar_interval":"month",
  9. "format": "yyyy-MM-dd",
  10. "min_doc_count": 0, //强制返回空 buckets
  11. "extended_bounds": { //强制返回区间内日期
  12. "min": "2014-01-01",
  13. "max": "2014-12-31"
  14. }
  15. }
  16. }
  17. }
  18. }
  19. 返回结果:
  20. {
  21. "took" : 7,
  22. "timed_out" : false,
  23. "_shards" : {
  24. "total" : 1,
  25. "successful" : 1,
  26. "skipped" : 0,
  27. "failed" : 0
  28. },
  29. "hits" : {
  30. "total" : {
  31. "value" : 16,
  32. "relation" : "eq"
  33. },
  34. "max_score" : null,
  35. "hits" : [ ]
  36. },
  37. "aggregations" : {
  38. "sales" : {
  39. "buckets" : [
  40. {
  41. "key_as_string" : "2014-01-01",
  42. "key" : 1388534400000,
  43. "doc_count" : 2
  44. },
  45. {
  46. "key_as_string" : "2014-02-01",
  47. "key" : 1391212800000,
  48. "doc_count" : 2
  49. }......
  50. }

image.png

post_filter 过滤搜索结果:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "match": {
  6. "make": "ford"
  7. }
  8. },
  9. "post_filter": {
  10. "term": {
  11. "color": "green"
  12. }
  13. },
  14. "aggs": {
  15. "all_colors":{
  16. "terms": {
  17. "field": "color.keyword"
  18. }
  19. }
  20. }
  21. }

排序:

在聚合中引入order对象, 它允许我们可以根据以下几个值中的一个值进行排序:

_count

按文档数排序。对 terms 、 histogram 、 date_histogram 有效。

_term

按词项的字符串值的字母顺序排序。只在 terms 内使用。

_key

按每个桶的键值数值排序(理论上与 _term 类似)。 只在 histogram 和 date_histogram 内使用。

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "colors":{
  6. "terms": {
  7. "field": "color.keyword",
  8. "order": {
  9. "_count": "desc"
  10. }
  11. }
  12. }
  13. }
  14. }

自定义排序

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "colors":{
  6. "terms": {
  7. "field": "color.keyword",
  8. "order": {
  9. "avg_price":"desc" //自定义avg_price排序
  10. }
  11. },
  12. "aggs": {
  13. "avg_price":{ //实现avg_price排序逻辑
  14. "avg": {
  15. "field": "price"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. GET /cars/_search
  23. {
  24. "size": 0,
  25. "aggs": {
  26. "colors":{
  27. "terms": {
  28. "field": "color.keyword",
  29. "order": {
  30. "status.max":"desc" //使用 . 符号,根据感兴趣的度量进行排序
  31. }
  32. },
  33. "aggs": {
  34. "status":{
  35. "extended_stats": {
  36. "field": "price" //根据status的价格降序排序
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. 返回结果:
  44. {
  45. "took" : 7,
  46. "timed_out" : false,
  47. "_shards" : {
  48. "total" : 1,
  49. "successful" : 1,
  50. "skipped" : 0,
  51. "failed" : 0
  52. },
  53. "hits" : {
  54. "total" : {
  55. "value" : 16,
  56. "relation" : "eq"
  57. },
  58. "max_score" : null,
  59. "hits" : [ ]
  60. },
  61. "aggregations" : {
  62. "colors" : {
  63. "doc_count_error_upper_bound" : 0,
  64. "sum_other_doc_count" : 0,
  65. "buckets" : [
  66. {
  67. "key" : "red",
  68. "doc_count" : 8,
  69. "status" : {
  70. "count" : 8,
  71. "min" : 10000.0,
  72. "max" : 80000.0,
  73. "avg" : 32500.0,
  74. "sum" : 260000.0,
  75. "sum_of_squares" : 1.46E10,
  76. "variance" : 7.6875E8,
  77. "std_deviation" : 27726.341266023544,
  78. "std_deviation_bounds" : {
  79. "upper" : 87952.6825320471,
  80. "lower" : -22952.68253204709
  81. }
  82. }
  83. },
  84. {
  85. "key" : "green",
  86. "doc_count" : 4,
  87. "status" : {
  88. "count" : 4,
  89. "min" : 12000.0,
  90. "max" : 30000.0,
  91. "avg" : 21000.0,
  92. "sum" : 84000.0,
  93. "sum_of_squares" : 2.088E9,
  94. "variance" : 8.1E7,
  95. "std_deviation" : 9000.0,
  96. "std_deviation_bounds" : {
  97. "upper" : 39000.0,
  98. "lower" : 3000.0
  99. }
  100. }
  101. }......}

cardinality去重:

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "distinct_colors": {
  6. "cardinality": {
  7. "field":"color.keyword"
  8. }
  9. }
  10. }
  11. }
  12. 返回结果:
  13. {
  14. ......
  15. "aggregations" : {
  16. "distinct_colors" : {
  17. "value" : 3
  18. }
  19. }
  20. }