ElasticSearch 这样的分布式系统中执行全 SQL 风格的连接查询代价昂贵,是不可行的。相应地,为了实现水平规模地扩展,ElasticSearch 提供了两种形式的 join。
1、nested query (嵌套查询)
文档中可能包含嵌套类型的字段,这些字段用来索引一些数组对象,每个对象都可以作为一条独立的文档被查询出来(用嵌套查询)
2、has_child (有子查询) and has_parent (有父查询) queries
类父子关系可以存在单个的索引的两个类型的文档之间。has_child 查询将返回其子文档能满足特定的查询的父文档,而 has_parent 则返回其父文档能满足特定查询的子文档
下面详细的谈谈parent_child的应用:
实用场景:一个父存在多个子。例如一个班级有很多的学生。不建议是一个子存在多个父的这种情况,例如一个学生多个班级。
理由是:父和子是绑定到同一个分片,如果一个子存在多个父的情况,刚好这几个parent_id在同一个分片时。只能写进一条记录。
具体场景:
现有商店(作为子)、活动(作为父),商店与门店的关系
为了达到通过商店的过滤条件查询活动的具体信息、通过活动的过滤条件查询到商店的具体信息。
①:建立index_mapping
PUT _template/act_shop
{
"order": 0,
"template": "act_shop-*",
"settings": {
"index": {
"mapping": {
"total_fields": {
"limit": "50000"
}
},
"number_of_shards": "150",
"number_of_replicas": "1",
"refresh_interval": "1s"
}
},
"mappings": {
"shop": {
"_parent": {
"type": "act"
},
"properties": {
"end_date": {
"format": "yyyy-MM-dd",
"type": "date"
},
"update_time": {
"format": "yyyy-MM-dd HH:mm:ss.SSSSSS",
"type": "date"
},
"coordinate": {
"type": "geo_point"
},
"create_time": {
"format": "yyyy-MM-dd HH:mm:ss.SSSSSS",
"type": "date"
},
"start_date": {
"format": "yyyy-MM-dd",
"type": "date"
}
}
},
"act": {
"properties": {
"end_date": {
"format": "yyyy-MM-dd",
"type": "date"
},
"update_time": {
"format": "yyyy-MM-dd HH:mm:ss.SSSSSS",
"type": "date"
},
"create_time": {
"format": "yyyy-MM-dd HH:mm:ss.SSSSSS",
"type": "date"
},
"start_date": {
"format": "yyyy-MM-dd",
"type": "date"
}
}
}
},
"aliases": {}
}
②商店的数据(shop)
PUT act_shop-2018.01.08/shop/181023?parent=266773
{
"status": 0,
"city": {
"id": 3619,
"name": "南山区"
},
"update_time": "2017-12-20 16:03:19.650000",
"tel": [],
"name": "百果园(白石中路1店)",
"tags": [
"购物服务",
"综合市场",
"果品市场"
],
"tags_enrich": {
"name": "超市网购",
"id": 20
},
"id": 181023,
"label": "have_act",
"create_time": "2016-12-20 14:25:24.402000",
"city_enrich": {
"region": "华南地区",
"name": "深圳",
"level": 1
},
"address": "红树街130号",
"coordinate": {
"lat": 22.531311,
"lon": 113.967413
},
"brand": {
"id": 22320,
"name": "百果园"
}
}
PUT act_shop-2018.01.08/shop/181023?parent=36525
{
"status": 0,
"city": {
"id": 3619,
"name": "南山区"
},
"update_time": "2017-12-20 16:03:19.650000",
"tel": [],
"name": "百果园(白石中路1店)",
"tags": [
"购物服务",
"综合市场",
"果品市场"
],
"tags_enrich": {
"name": "超市网购",
"id": 20
},
"id": 181023,
"label": "have_act",
"create_time": "2016-12-20 14:25:24.402000",
"city_enrich": {
"region": "华南地区",
"name": "深圳",
"level": 1
},
"address": "红树街130号",
"coordinate": {
"lat": 22.531311,
"lon": 113.967413
},
"brand": {
"id": 22320,
"name": "百果园"
}
}
③活动的信息(act)
PUT act_shop-2018.01.08/act/266773
{
"zone_exclude_id": [],
"create_time": "2017-12-15 10:16:28.937000",
"id": 266773,
"subject": {
"type": 2,
"name": "card_org"
},
"category": [
{
"name_en": "",
"id": 286,
"name": "其他购物"
}
],
"special_dates": [],
"original_url": "http://static.95516.com/static/page/yhfwb/zhenghe/activity-page/bgy.html",
"special_weekdays": [],
"content": """
##### 参与方式
- 至「百果园」指定门店银联手机闪付享优惠
##### 活动内容
- 活动期间,**每周六 7:00-23:00 **银联(62 开头)信用卡持卡人至「百果园」指定门店银联手机闪付享满 50 立减 10 元优惠
##### 注意事项
- 每人每活动日限享 1 次优惠
- 消费券、提货券、储值卡等储值类商品除外
""",
"usage_scene": [],
"start_date": "2017-12-15",
"status": 1,
"subject_description": "银联(62 开头)信用卡持卡人",
"update_time": "2017-12-15 10:21:45.321000",
"card_orgs": [
{
"id": 6,
"name": "银联"
}
],
"end_date": "2018-03-31",
"receive_description": "",
"unavailable_time_ranges": [],
"quota": "每活动日共计 1.5 万名",
"zone_include": [
"中国"
],
"discount": [
{
"name_en": "decrease_price",
"id": 19,
"name": "立减"
}
],
"category_enrich": {
"name": "超市网购",
"id": 20
},
"act_area": null,
"content_enrich": {
"参与方式": [
" 至「百果园」指定门店银联手机闪付享优惠"
],
"活动内容": [
" 活动期间,每周六 7:00-23:00 银联(62 开头)信用卡持卡人至「百果园」指定门店银联手机闪付享满 50 立减 10 元优惠"
],
"注意事项": [
" 每人每活动日限享 1 次优惠",
" 消费券、提货券、储值卡等储值类商品除外"
]
},
"payment": [
{
"name_en": "",
"id": 168,
"name": "Mipay"
},
{
"name_en": "",
"id": 164,
"name": "ApplePay"
},
{
"name_en": "",
"id": 166,
"name": "SamsungPay"
},
{
"name_en": "",
"id": 167,
"name": "HuaweiPay"
}
],
"small_img_url": null,
"act_type": 1,
"zone_include_id": [
24
],
"title": "百果园满 50 减 10",
"original_title": "银联百果惠",
"display_tags": [],
"big_img_url": "https://qnpic.billbear.cn/FsIX719_Qt-E-6BRXiHxDc0FKIUU",
"available_time_ranges": [
{
"range_type": 2,
"start_time": 25200,
"end_time": 82800,
"weekday": 5,
"day": null
}
],
"zone_exclude": [],
"banks": [],
"is_hot": 0,
"money_limit": null
}
PUT act_shop-2018.01.08/act/266773
{
"zone_exclude_id": [],
"create_time": "2017-02-10 13:38:36.525000",
"id": 36525,
"subject": {
"type": 2,
"name": "card_org"
},
"category": [
{
"name_en": "other_food",
"id": 55,
"name": "其他美食"
},
{
"name_en": "takeaway",
"id": 54,
"name": "外卖"
}
],
"special_dates": [],
"original_url": "https://mp.weixin.qq.com/s?__biz=MjM5NDg0Mzc5NA==&mid=2659803252&idx=4&sn=57a5e54a276f96894d73c1be59219285&chksm=bdfc5d898a8bd49f7120eaf51c189205f1a340297ab7b273bf939243447a92348e0ad58543d9&scene=0&key=acee276fe32414514538a1541ce00d3087fa9d92fb1e09e7486d463a96b50e8f35d2ea7707f24e1d148c33859f682c5a5cc1016605a8fd0386068a4f86b4968718e6713fe15329127fdbec5f6931c804&ascene=0&uin=NzQxMjgxMDAw&devicetype=iMac+MacBook9%2C1+OSX+OSX+10.12.3+build(16D32)&version=12010310&nettype=WIFI&fontScale=100&pass_ticket=pAmyZjgFDOjHEWa2GXWD4fLvbHJwLBH6wg3KUTG9su5Cyr%2FI9lATheHMruFwTaim",
"special_weekdays": [],
"content": """
##### 活动内容
1. 银联持卡人(卡号以 62 开头)在**深圳地区**的「百果园」消费,享立减优惠
2. **手机云闪付**支付消费,单笔满 50 元减 20 元
3. **IC 信用卡闪付**支付消费,单笔满 50 元减 10 元
""",
"usage_scene": [
{
"name_en": "food",
"id": 2,
"name": "餐饮"
}
],
"start_date": "2017-02-14",
"status": 0,
"subject_description": null,
"update_time": "2017-06-07 14:48:18.952000",
"card_orgs": [
{
"id": 6,
"name": "银联"
}
],
"end_date": "2017-05-31",
"receive_description": "",
"unavailable_time_ranges": [],
"quota": null,
"zone_include": [
"中国"
],
"discount": [
{
"name_en": "decrease_price",
"id": 19,
"name": "立减"
}
],
"category_enrich": {
"name": "美食",
"id": 10
},
"act_area": {
"name_en": "mainland",
"id": 34,
"name": "大陆"
},
"content_enrich": {
"活动内容": [
" 银联持卡人(卡号以 62 开头)在深圳地区的「百果园」消费,享立减优惠",
" 手机云闪付支付消费,单笔满 50 元减 20 元",
" IC 信用卡闪付支付消费,单笔满 50 元减 10 元"
]
},
"payment": [],
"small_img_url": null,
"act_type": 3,
"zone_include_id": [
24
],
"title": "百果园满 50 减 20",
"original_title": "过来,教你如何在广深每周最高劲省140元! ",
"display_tags": [],
"big_img_url": "https://qnpic.billbear.cn/Fj-nrXp6IYFMbJhGDzx8n3Jy9rxr",
"available_time_ranges": [
{
"range_type": 2,
"start_time": 0,
"weekday": 5,
"day": null,
"end_time": 86400
},
{
"range_type": 2,
"start_time": 0,
"weekday": 1,
"day": null,
"end_time": 86400
}
],
"zone_exclude": [],
"banks": [],
"is_hot": 1,
"money_limit": null
}
③双向查询
1、通过商店查询到活动
GET act_shop-2018.01.08/_search
{
"query": {
"has_child": {
"type": "shop",
"query": {
"bool": {
"must": [
{
"term": {
"name.keyword": {
"value": "百果园(白石中路1店)"
}
}
}
]
}
}
}
}
}
2、通过活动可以查询到相应的商户
GET act_shop-2018.01.08/_search
{
"size": 400,
"query": {
"has_parent": {
"parent_type": "act",
"query": {
"bool": {
"must": [
{
"term": {
"title.keyword": {
"value": "百果园满 50 减 10"
}
}
}
]
}
}
}
}
}
总结:ElastricSearch是一个非常强大的搜索引擎。本版本是5.5.2.其中新出6.0.0版本开始废弃type。有兴趣的话可以看一下官网的文档。