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