elasticsearch-搜索

PUT http://192.168.31.46:9200/books

{
    "settings":{
        "number_of_replicas":1,
        "number_of_shards":3
    },
    "mappings":{
        "IT":{
            "properties":{
                "id":{
                    "type":"long"
                },
                "title":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "language":{
                    "type":"keyword"
                },
                "author":{
                    "type":"keyword"
                },
                "price":{
                    "type":"double"
                },
                "year":{
                    "type":"date",
                    "format":"yyyy-MM-dd"
                },
                "description":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                }
            }
        }
    }
}

curl -XPOST "http://192.168.31.46:9200/_bulk?pretty" --data-binary @book.json

{"index":{"_index":"books","_type":"IT","_id":"1"}}
{"id":1,"title":"Java編程思想","language":"java","author":"BruceEckel","price":70.20,"punlish_time":"2007-10-01","description":"Java學習必讀經典,殿堂級著作!贏得了全球程序員的廣泛讚譽"}
{"index":{"_index":"books","_type":"IT","_id":"2"}}
{"id":2,"title":"Java程序性能優化","language":"java","author":"葛一鳴","price":46.50,"punlish_time":"2012-08-01","description":"讓你的Java程序更快、更穩定。深入剖析軟件設計層面、代碼層面、JVM虛擬機層面的優化方法"}
{"index":{"_index":"books","_type":"IT","_id":"3"}}
{"id":3,"title":"Python科學計算","language":"python","author":"張若愚","price":81.40,"punlish_time":"2016-05-01","description":"零基礎學python,光盤中作者獨家整合開發winPython運行環境,涵蓋了Python各個擴展庫"}
{"index":{"_index":"books","_type":"IT","_id":"4"}}
{"id":4,"title":"Python基礎教程","language":"python","author":"Helant","price":54.50,"punlish_time":"2014-03-01","description":"經典的Python入門教程,層次鮮明,就夠嚴謹,內容詳實"}
{"index":{"_index":"books","_type":"IT","_id":"5"}}
{"id":5,"title":"Javascript高級程序設計","language":"javascript","author":"NicholasC.Zakas","price":66.40,"punlish_time":"2012-10-01","description":"JavaScript技術經典著作"}

{
  "took" : 1511,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "books",
        "_type" : "IT",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "books",
        "_type" : "IT",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "books",
        "_type" : "IT",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "books",
        "_type" : "IT",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "books",
        "_type" : "IT",
        "_id" : "5",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    }
  ]
}

 curl -XGET http://192.168.31.46:9200/books/_search -d '{"query":{"match_all":{}}}'

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "id": 2,
                    "title": "Java程序性能優化",
                    "language": "java",
                    "author": "葛一鳴",
                    "price": 46.5,
                    "punlish_time": "2012-08-01",
                    "description": "讓你的Java程序更快、更穩定。深入剖析軟件設計層面、代碼層面、JVM虛擬機層面的優化方法"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "id": 4,
                    "title": "Python基礎教程",
                    "language": "python",
                    "author": "Helant",
                    "price": 54.5,
                    "punlish_time": "2014-03-01",
                    "description": "經典的Python入門教程,層次鮮明,就夠嚴謹,內容詳實"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "id": 5,
                    "title": "Javascript高級程序設計",
                    "language": "javascript",
                    "author": "NicholasC.Zakas",
                    "price": 66.4,
                    "punlish_time": "2012-10-01",
                    "description": "JavaScript技術經典著作"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Java編程思想",
                    "language": "java",
                    "author": "BruceEckel",
                    "price": 70.2,
                    "punlish_time": "2007-10-01",
                    "description": "Java學習必讀經典,殿堂級著作!贏得了全球程序員的廣泛讚譽"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "id": 3,
                    "title": "Python科學計算",
                    "language": "python",
                    "author": "張若愚",
                    "price": 81.4,
                    "punlish_time": "2016-05-01",
                    "description": "零基礎學python,光盤中作者獨家整合開發winPython運行環境,涵蓋了Python各個擴展庫"
                }
            }
        ]
    }
}

下面以term query爲例介紹如何進行此項搜搜、分頁限制、返回指定字段、顯示版本號、控制最小評分、關鍵字的高亮

term查詢查找指定字段中包含給定單詞的文檔,不被解析,精確匹配

POST http://192.168.31.46:9200/books/_search

{
    "query":
    {
        "term":{
            "title":"思想"    
        }
    }
}

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.6099695,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "1",
                "_score": 0.6099695,
                "_source": {
                    "id": 1,
                    "title": "Java編程思想",
                    "language": "java",
                    "author": "BruceEckel",
                    "price": 70.2,
                    "punlish_time": "2007-10-01",
                    "description": "Java學習必讀經典,殿堂級著作!贏得了全球程序員的廣泛讚譽"
                }
            }
        ]
    }
}

提供了基於最小評分的過濾機制。構造查詢語句、控制查詢規模、返回查詢文檔版本號、返回文檔部分字段、設置最小評分、關鍵詞高亮

POST http://192.168.31.46:9200/books/_search

{
    "min_score":0.7,
    "version":true,
    "from":0,
    "size":2,
    "_source":["title","author"],
    "query":
    {
        "term":{
            "title":"java"    
        }
    },
    "highlight":{
        "fields":{
            "title":{}
        }
    }
}

{
    "took": 111,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.8882085,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "2",
                "_version": 1,
                "_score": 0.8882085,
                "_source": {
                    "author": "葛一鳴",
                    "title": "Java程序性能優化"
                },
                "highlight": {
                    "title": [
                        "<em>Java</em>程序性能優化"
                    ]
                }
            }
        ]
    }
}

全文查詢

match query 會對查詢語句進行分詞,分詞後查詢語句中的的任何一個詞項匹配,就會被搜到,想查匹配所有關鍵詞的文檔,可用and操作符連接。

http://192.168.31.46:9200/books/_search

{
    "query":{
        "match":{
            "title":{
                "query":"java編程思想",
                "operator":"or"
            }
        }
    }
}

{
    "took": 20,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.8299085,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "1",
                "_score": 1.8299085,
                "_source": {
                    "id": 1,
                    "title": "Java編程思想",
                    "language": "java",
                    "author": "BruceEckel",
                    "price": 70.2,
                    "punlish_time": "2007-10-01",
                    "description": "Java學習必讀經典,殿堂級著作!贏得了全球程序員的廣泛讚譽"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "2",
                "_score": 0.8882085,
                "_source": {
                    "id": 2,
                    "title": "Java程序性能優化",
                    "language": "java",
                    "author": "葛一鳴",
                    "price": 46.5,
                    "punlish_time": "2012-08-01",
                    "description": "讓你的Java程序更快、更穩定。深入剖析軟件設計層面、代碼層面、JVM虛擬機層面的優化方法"
                }
            }
        ]
    }
}

match_phrase query

把query內容分詞,分詞器可定義1  分詞後所有詞項都要出現在該字段中  2   字段中的詞項順序要一致

match_phrase_prefix query 支持最後一個term前綴匹配

multi_match query

{
    "query":{
        "multi_match":{
            "query":"java編程",
            "fields":[
                "title^3","description"]//指定搜索字段的權重
        }
    }
}

common_term query

query_string query

simple_query_string

 

詞項查詢

terms query

查詢文檔中包含多個詞的文檔,

POST http://192.168.31.46:9200/books/_search

{
    "took": 86,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1.0131714,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "4",
                "_score": 1.0131714,
                "_source": {
                    "id": 4,
                    "title": "Python基礎教程",
                    "language": "python",
                    "author": "Helant",
                    "price": 54.5,
                    "punlish_time": "2014-03-01",
                    "description": "經典的Python入門教程,層次鮮明,就夠嚴謹,內容詳實"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "2",
                "_score": 0.8882085,
                "_source": {
                    "id": 2,
                    "title": "Java程序性能優化",
                    "language": "java",
                    "author": "葛一鳴",
                    "price": 46.5,
                    "punlish_time": "2012-08-01",
                    "description": "讓你的Java程序更快、更穩定。深入剖析軟件設計層面、代碼層面、JVM虛擬機層面的優化方法"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "1",
                "_score": 0.6099695,
                "_source": {
                    "id": 1,
                    "title": "Java編程思想",
                    "language": "java",
                    "author": "BruceEckel",
                    "price": 70.2,
                    "punlish_time": "2007-10-01",
                    "description": "Java學習必讀經典,殿堂級著作!贏得了全球程序員的廣泛讚譽"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "3",
                "_score": 0.6099695,
                "_source": {
                    "id": 3,
                    "title": "Python科學計算",
                    "language": "python",
                    "author": "張若愚",
                    "price": 81.4,
                    "punlish_time": "2016-05-01",
                    "description": "零基礎學python,光盤中作者獨家整合開發winPython運行環境,涵蓋了Python各個擴展庫"
                }
            }
        ]
    }
}

range query

POST  http://192.168.31.46:9200/books/_search

{
    "query":{
        "range":{
            "price":{
                "gt":50,
                "lt":70
            }
        }
    }
}

{
    "took": 50,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "books",
                "_type": "IT",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "id": 4,
                    "title": "Python基礎教程",
                    "language": "python",
                    "author": "Helant",
                    "price": 54.5,
                    "punlish_time": "2014-03-01",
                    "description": "經典的Python入門教程,層次鮮明,就夠嚴謹,內容詳實"
                }
            },
            {
                "_index": "books",
                "_type": "IT",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "id": 5,
                    "title": "Javascript高級程序設計",
                    "language": "javascript",
                    "author": "NicholasC.Zakas",
                    "price": 66.4,
                    "punlish_time": "2012-10-01",
                    "description": "JavaScript技術經典著作"
                }
            }
        ]
    }
}

{
    "query":{
        "range":{
            "punlish_time":{
                "gt":"2016-01-01",
                "lt":"2016-12-31",
                "format":"yyyy-MM-dd"
            }
        }
    }
}

{
    "query":{
        "exists":{
            "field":"author"
        }
    }
}

prefix query

{
    "query":{
        "prefix":{
            "description":"win"
        }
    }
}

wildcard query消耗資源

{
    "query":{
        "wildcard":{
            "author":"張若*"
        }
    }
}

regexp query

{
    "query":{
        "regexp":{
            "postcode":"W0-9].+"
        }
    }
}

fuzzy query,

編輯距離又稱Levenshtein距離,兩個字串之間,由一個轉成另一個所需的最小編輯操作次數。許可的編輯操作替換,插入,刪除。fuzzy查詢就是計算詞項與文檔的編輯距離來得結果的。

{
    "query":{
        "fuzzy":{
            "title":"javascritp"
        }
    }
}

type query

{
    "query":{
        "type":{
            "value":"IT"
        }
    }
}

ids query 

{
    "query":{
        "ids":{
            "type":"IT",
            "values":["1","3","5"]
        }
    }
}

複合查詢

constant_score query  包裝一個其他的類型的查詢,並返回匹配過濾器中的查詢條件且具有相同評分的文檔

{
    "query":{
        "constant_score":{
            "filter":{
                "term":{
                    "title":"java"
                }
            }
        }
    }
}

bool query 組合 多個簡單查詢,must   should   must_not   filter(只過濾,不評分)

{
    "query":{
        "bool":{
            "minimum_should_match":1,
            "must":{
                "match":{
                    "title":"java"
                }
            },
            "should":{
                "match":{
                    "description":"虛擬機"
                }
            },
            "must_not":{
                "range":{
                    "price":{
                        "gte":70
                    }
                }
            }
        }
    }
}

dis_max query

function_score query 可以修改查詢的文檔得分,

boosting query

indices query

嵌套查詢

nested query 文檔中可能包含嵌套類型的字段,這些字段用來索引一些數組對象,每個對象可以作爲一條獨立的文檔被查詢出來

 

搜索高亮

自定義高亮片段

多字段高亮

http://192.168.31.46:9200/books/_search

{
    "query":{
        "match":{
            "title":"javascript"
        }
    },
    "highlight":{
        "require_field_match":false,
        "fields":{
            "title":{},
            "description":{}
        }
    }
}

高亮性能分析

es提供了三種高亮器,默認的highlighter高亮器,postings-highlighter,fast-vector-highlighter

highlighter高亮器實現高亮功能需要對_source中保存的原始文檔進行二次分析,速度最慢,但不需要額外的存儲空間

postings-highlighter不需要二次分析,但需要在字段的映射中設置index_options`參數的取值爲offsets,即保存關鍵詞的偏移量,速度快於默認的highlight。

fast-vector-highlighter速度最快,需要在字段的映射中設置term_vector參數的取值爲with_positions_offsets,及保存關鍵詞的位置和偏移信息,佔用的存儲空間更大,空間換時間。

 

搜索排序

{
    "query":{
        "match_all":{}
    },
    "sort":
        [{
            "_doc":{
                "order":"asc"
            }
        }]
}

多字段排序

{
    "sort":[
        {
            "price":{
                "order":"desc"
            }
            
        },
        {
            "year":{
                "order":"asc"
            }
        }
        ]
}

分片影響評分

elasticsearch5。4之後對於text類型的字段,默認BM25評分模型,而不是基於tf-idf的向量空間模型,評分模型的選擇可以通過similarity參數在映射中指定,elasticsearch是在每個分片上單獨打分的,分片的數量會影響打分的結果

聚合索引

指標聚合,桶聚合,管道聚合,矩陣聚合四大類,

指標聚合

max aggregation,

{
    "size":0,
    "aggs":{
        "max_price":{
            "max":{
                "field":"price"
            }
        }
    }
}

{
    "took": 62,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "max_price": {
            "value": 81.4
        }
    }
}

Min aggregation

{
    "size":0,
    "aggs":{
        "min_year":{
            "min":{
                "field":"punlish_time"
            }
        }
    }
}

Avg aggregation

{
    "size":0,
    "aggs":{
        "avg_price":{
            "avg":{
                "field":"price"
            }
        }
    }
}

sum aggregation

{
    "took": 69,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "sum_price": {
            "value": 319
        }
    }
}

Cardinality aggregation

基數統計,先執行類似SQL中的distinct操作,去掉集合中的重複項,然後統計排重後的集合的長度。

{
    "size":0,
    "aggs":{
        "all_lan":{
            "cardinality":{
                "field":"language"
            }
        }
    }
}

stats aggregation

{
    "size":0,
    "aggs":{
        "grade_stats":{
            "stats":{
                "field":"price"
            }
        }
    }
}

extended stats aggregation

多出平方和,方差,標準差,平均值加/減兩個標準差的區間。

{
    "size":0,
    "aggs":{
        "grade_stats":{
            "extended_stats":{
                "field":"price"
            }
        }
    }
}

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "grade_stats": {
            "count": 5,
            "min": 46.5,
            "max": 81.4,
            "avg": 63.8,
            "sum": 319,
            "sum_of_squares": 21095.46,
            "variance": 148.65199999999967,
            "std_deviation": 12.19229264740638,
            "std_deviation_bounds": {
                "upper": 88.18458529481276,
                "lower": 39.41541470518724
            }
        }
    }
}

percentile aggregation

百分數統計,某一百分位對應數據的值

{
    "size":0,
    "aggs":{
        "book_price":{
            "percentiles":{
                "field":"price"
            }
        }
    }
}

"aggregations": {
        "book_price": {
            "values": {
                "1.0": 46.82,
                "5.0": 48.1,
                "25.0": 54.5,
                "50.0": 66.4,
                "75.0": 70.2,
                "95.0": 79.16,
                "99.0": 80.95200000000001
            }
        }
    }

value count aggregation 按字段統計文檔數量

{
    "size":0,
    "aggs":{
        "doc_count":{
            "value_count":{
                "field":"author"
            }
        }
    }
}

包含author字段的文檔數量

 

桶聚合

group by

就是符合某一劃分標準的文檔集合

terms aggregation

{
    "size":0,
    "aggs":{
        "per_count":{
            "terms":{
                "field":"language"
            }
        }
    }
}

"aggregations": {
        "per_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "java",
                    "doc_count": 2
                },
                {
                    "key": "python",
                    "doc_count": 2
                },
                {
                    "key": "javascript",
                    "doc_count": 1
                }
            ]
        }
    }

{
    "size":0,
    "aggs":{
        "per_count":{
            "terms":{
                "field":"language"
            },
            "aggs":{
              "avg_price":{
                "avg":{
                  "field": "price"
                }
              }
            }
        }
    }
}

Filter aggregation

{
    "size":0,
    "aggs":{
        "java_avg_price":{
          "filter": {
            "term":{
              "title":"java"
            }
          },
          "aggs":{
              "avg_price":{
                  "avg":{
              "field":"price"
            }
              }
          }
        }
    }
}

"aggregations": {
        "java_avg_price": {
            "doc_count": 2,
            "avg_price": {
                "value": 58.35
            }
        }
    }

filters aggregation

{
    "size":0,
    "aggs":{
        "java_avg_price":{
          "filters": {
              "filters":[
                  {
                      "match":{
                          "title":"java"
                      }
                  },
                  {
                      "match":{
                          "title":"python"
                      }
                  }
                  ]
            
          },
          "aggs":{
              "avg_price":{
                  "avg":{
              "field":"price"
            }
              }
            
          }
        }
    }
}

{
    "took": 115,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "java_avg_price": {
            "buckets": [
                {
                    "doc_count": 2,
                    "avg_price": {
                        "value": 58.35
                    }
                },
                {
                    "doc_count": 2,
                    "avg_price": {
                        "value": 67.95
                    }
                }
            ]
        }
    }
}

range aggregation

{
    "size":0,
    "aggs":{
        "price_ranges":{
            "range":{
                "field":"price",
                "ranges":[
                    {
                        "to":50
                    },
                    {
                        "from":50,
                        "to":80
                    },
                    {
                        "from":80
                    }
                ]
            }
        }
    }
}

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "price_ranges": {
            "buckets": [
                {
                    "key": "*-50.0",
                    "to": 50,
                    "doc_count": 1
                },
                {
                    "key": "50.0-80.0",
                    "from": 50,
                    "to": 80,
                    "doc_count": 3
                },
                {
                    "key": "80.0-*",
                    "from": 80,
                    "doc_count": 1
                }
            ]
        }
    }
}

{
    "size":0,
    "aggs":{
        "range":{
            "date_range":{
                "field":"punlish_time",
                "format":"yyyy-MM-dd",
                "ranges":[
                    {
                        "to":"2013-09-01"
                    },
                    {
                        "from":"2013-09-01",
                        "to":"2014-09-01"
                    },
                    {
                        "from":"2014-09-01"
                    }
                ]
            }
        }
    }
}

{
    "took": 56,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "range": {
            "buckets": [
                {
                    "key": "*-2013-09-01",
                    "to": 1377993600000,
                    "to_as_string": "2013-09-01",
                    "doc_count": 3
                },
                {
                    "key": "2013-09-01-2014-09-01",
                    "from": 1377993600000,
                    "from_as_string": "2013-09-01",
                    "to": 1409529600000,
                    "to_as_string": "2014-09-01",
                    "doc_count": 1
                },
                {
                    "key": "2014-09-01-*",
                    "from": 1409529600000,
                    "from_as_string": "2014-09-01",
                    "doc_count": 1
                }
            ]
        }
    }
}

date range aggregation

{
    "size":0,
    "aggs":{
        "range":{
            "date_range":{
                "field":"punlish_time",
                "format":"yyyy-MM-dd",
                "ranges":[
                    {
                        "to":"now-12M/M",
                        "from":"now-36M/M"
                    }
                ]
            }
        }
    }
}

 "aggregations": {
        "range": {
            "buckets": [
                {
                    "key": "2015-09-01-2017-09-01",
                    "from": 1441065600000,
                    "from_as_string": "2015-09-01",
                    "to": 1504224000000,
                    "to_as_string": "2017-09-01",
                    "doc_count": 1
                }
            ]
        }
    }

date Histogram aggregation

{
    "size":0,
    "aggs":{
        "books_over_time":{
            "date_histogram":{
                "field":"punlish_time",
                "interval":"year"
            }
        }
    }
}

{
    "took": 137,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "books_over_time": {
            "buckets": [
                {
                    "key_as_string": "2007-01-01T00:00:00.000Z",
                    "key": 1167609600000,
                    "doc_count": 1
                },
                {
                    "key_as_string": "2008-01-01T00:00:00.000Z",
                    "key": 1199145600000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2009-01-01T00:00:00.000Z",
                    "key": 1230768000000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2010-01-01T00:00:00.000Z",
                    "key": 1262304000000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2011-01-01T00:00:00.000Z",
                    "key": 1293840000000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2012-01-01T00:00:00.000Z",
                    "key": 1325376000000,
                    "doc_count": 2
                },
                {
                    "key_as_string": "2013-01-01T00:00:00.000Z",
                    "key": 1356998400000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2014-01-01T00:00:00.000Z",
                    "key": 1388534400000,
                    "doc_count": 1
                },
                {
                    "key_as_string": "2015-01-01T00:00:00.000Z",
                    "key": 1420070400000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2016-01-01T00:00:00.000Z",
                    "key": 1451606400000,
                    "doc_count": 1
                }
            ]
        }
    }
}

Missing aggregation空值聚合,把文檔中所有缺失字段的文檔放到一個桶中。

{
    "size":0,
    "aggs":{
        "books_without_price":{
                "missing":{
                    "field":"price"
                }
            
        }
    }
}

{
    "took": 20,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "books_without_price": {
            "doc_count": 0
        }
    }
}

children aggregation

根據父子文檔關係進行分桶

put http://192.168.31.46:9200/company

{
    "mappings":{
        "branch":{},
        "employee":{
            "_parent":{
                "type":"branch"
            }
        }
    }
}

 curl -XPOST http://192.168.31.46:9200/companh/branch/_bulk -d '

{"index":{"_id":"london"}}

{"name":"London Westminster","city":"London","country":"UK"}

{"index":{"_id":"leverpool"}}

{"name":"Liverpool Centreal","city":"Liverpool","country":"UK"}

{"index":{"_id":"paris"}}

{"name":"Champs Elysees","city":"Paris","country":"France"}

'

{
    "took": 35,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "company",
                "_type": "branch",
                "_id": "london",
                "_version": 5,
                "result": "updated",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": false,
                "status": 200
            }
        },
        {
            "index": {
                "_index": "company",
                "_type": "branch",
                "_id": "leverpool",
                "_version": 4,
                "result": "updated",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": false,
                "status": 200
            }
        },
        {
            "index": {
                "_index": "company",
                "_type": "branch",
                "_id": "paris",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": true,
                "status": 201
            }
        }
    ]
}

{"index":{"_id":1,"parent":"london"}}
{"name":"Alice Smith","dob":"1970-10-24","hobby":"hiking"}
{"index":{"_id":2,"parent":"london"}}
{"name":"Mark Tomes","dob":"1982-05-16","hobby":"diving"}
{"index":{"_id":3,"parent":"liverpool"}}
{"name":"Barry Smith","dob":"1979-04-01","hobby":"hiking"}
{"index":{"_id":4,"parent":"paris"}}
{"name":"Adrien Grand","dob":"1987-05-11","hobby":"horses"}
 

{
    "took": 277,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "company",
                "_type": "employee",
                "_id": "1",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": true,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "company",
                "_type": "employee",
                "_id": "2",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": true,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "company",
                "_type": "employee",
                "_id": "3",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": true,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "company",
                "_type": "employee",
                "_id": "4",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 3,
                    "failed": 0
                },
                "created": true,
                "status": 201
            }
        }
    ]
}

通過子文檔查詢父文檔要使用has_child查詢。查詢1980年後出生的所在的分支機構,

GET company/branch/_search
{
  "query": {
    "has_child": {
      "type": "employee",
      "query": {
        "range": {
          "dob": {
            "gte": "1980-01-01"
          }
        }
      }
    }
  }
}

{
  "took": 1162,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "branch",
        "_id": "paris",
        "_score": 1,
        "_source": {
          "name": "Champs Elysees",
          "city": "Paris",
          "country": "France"
        }
      },
      {
        "_index": "company",
        "_type": "branch",
        "_id": "london",
        "_score": 1,
        "_source": {
          "name": "London Westminster",
          "city": "London",
          "country": "UK"
        }
      }
    ]
  }
}

GET company/branch/_search
{
  "query": {
    "has_child": {
      "type": "employee",
      "score_mode": "max",
      "query": {
        "match": {
          "name": "Alice Smith"
        }
      }
      }
    }
}

{
  "took": 82,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.2039728,
    "hits": [
      {
        "_index": "company",
        "_type": "branch",
        "_id": "london",
        "_score": 1.2039728,
        "_source": {
          "name": "London Westminster",
          "city": "London",
          "country": "UK"
        }
      }
    ]
  }
}

 

Children aggregation

是一種單桶聚合,根據父子關係進行分桶,

GET company/_search
{
  "size": 0, 
  "aggs": {
    "to_answers": {
      "children": {
        "type": "employee"
      }
    }
  }
}

"aggregations": {     "to_answers": {       "doc_count": 3     }   }