nested query (嵌套查詢)json
文檔中可能包含嵌套類型的字段,這些字段用來索引一些數組對象,每一個對象均可以做爲一條獨立的文檔被查詢出來(用嵌套查詢)數組
場景:商店(shop) 活動(act) 目標是將活動(act)嵌套(冗餘)到shop數據下面app
一、index_mappingurl
PUT _template/shop { "order": 0, "template": "shop-*", "settings": { "index": { "mapping": { "total_fields": { "limit": "50000" } }, "refresh_interval": "1s", "number_of_shards": "10", "number_of_replicas": "1" } }, "mappings": { "shop": { "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" }, "acts": { "type": "nested", "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" } } }, "start_date": { "format": "yyyy-MM-dd", "type": "date" } } } }, "aliases": {} }
二、商店與活動嵌套數據code
PUT shop-2017.12.18/shop/351971 { "status": 1, "city": { "id": 587, "name": "黃石" }, "update_time": "2018-01-09 14:41:12.892000", "tel": [ "0714-6289788" ], "name": "江小魚江魚館(黃石店)", "tags": [ "餐飲服務", "餐飲相關場所", "餐飲相關" ], "tags_enrich": { "name": "美食", "id": 10 }, "brand": { "id": 35572, "name": "江小魚江魚館" }, "acts": [ { "zone_exclude_id": [], "create_time": "2017-11-30 03:57:18.732000", "id": 269791, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800061", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人**每週三上午 10:00** 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 每單僅限使用 5 張 - 酒水飲料、主食除外 - 僅限週三使用 - 不與商戶其餘優惠同享 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:50:46.481000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人每週三上午 10:00 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "每單僅限使用 5 張", "酒水飲料、主食除外", "僅限週三使用", "不與商戶其餘優惠同享", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "(週三5折)江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FriUhDNz2c4KICWvyc-ETd-xk7Gt", "available_time_ranges": [ { "range_type": 2, "start_time": 36000, "end_time": 86100, "weekday": 2, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券 (週三 5 折)", "is_hot": 0, "money_limit": null }, { "zone_exclude_id": [], "create_time": "2017-11-29 13:16:43.594000", "id": 269793, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800078", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 不與商戶其餘優惠同享 - 酒水飲料、主食除外 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:54:00.766000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "不與商戶其餘優惠同享", "酒水飲料、主食除外", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FsuJGkUVpxOwCfkrenCZLsrFpxX_", "available_time_ranges": [ { "range_type": 1, "start_time": 0, "end_time": 86400, "weekday": null, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券", "is_hot": 0, "money_limit": null } ], "label": "have_act", "create_time": "2018-01-09 14:40:48.846000", "city_enrich": { "region": "華中地區", "name": "黃石", "level": 4 }, "address": "黃石港區天津路52號十五中對面", "coordinate": { "lat": 30.216619, "lon": 115.08882 }, "id": 351971 }
三、商店與活動的雙向查詢 查詢shoporm
GET shop-2018.01.10/shop/_search { "query": { "bool": { "must": [ { "term": { "name.keyword": { "value": "江小魚江魚館(黃石店)" } } } ] } } }
返回數據cdn
{ "took": 11, "timed_out": false, "_shards": { "total": 10, "successful": 10, "failed": 0 }, "hits": { "total": 1, "max_score": 8.536735, "hits": [ { "_index": "shop-2018.01.10", "_type": "shop", "_id": "351971", "_score": 8.536735, "_source": { "status": 1, "city": { "id": 587, "name": "黃石" }, "update_time": "2018-01-09 14:41:12.892000", "tel": [ "0714-6289788" ], "name": "江小魚江魚館(黃石店)", "tags": [ "餐飲服務", "餐飲相關場所", "餐飲相關" ], "tags_enrich": { "name": "美食", "id": 10 }, "brand": { "id": 35572, "name": "江小魚江魚館" }, "acts": [ { "zone_exclude_id": [], "create_time": "2017-11-30 03:57:18.732000", "id": 269791, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800061", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人**每週三上午 10:00** 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 每單僅限使用 5 張 - 酒水飲料、主食除外 - 僅限週三使用 - 不與商戶其餘優惠同享 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:50:46.481000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人每週三上午 10:00 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "每單僅限使用 5 張", "酒水飲料、主食除外", "僅限週三使用", "不與商戶其餘優惠同享", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "(週三5折)江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FriUhDNz2c4KICWvyc-ETd-xk7Gt", "available_time_ranges": [ { "range_type": 2, "start_time": 36000, "end_time": 86100, "weekday": 2, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券 (週三 5 折)", "is_hot": 0, "money_limit": null }, { "zone_exclude_id": [], "create_time": "2017-11-29 13:16:43.594000", "id": 269793, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800078", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 不與商戶其餘優惠同享 - 酒水飲料、主食除外 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:54:00.766000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "不與商戶其餘優惠同享", "酒水飲料、主食除外", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FsuJGkUVpxOwCfkrenCZLsrFpxX_", "available_time_ranges": [ { "range_type": 1, "start_time": 0, "end_time": 86400, "weekday": null, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券", "is_hot": 0, "money_limit": null } ], "label": "have_act", "brand_logo_enrich": { "logo_url_lx": "https://cdn.xhqb.com/brand/picture_lx/35572.png", "logo_url_xh": "https://cdn.xhqb.com/brand/picture_xh/35572.jpg" }, "create_time": "2018-01-09 14:40:48.846000", "city_enrich": { "region": "華中地區", "name": "黃石", "level": 4 }, "address": "黃石港區天津路52號十五中對面", "coordinate": { "lat": 30.216619, "lon": 115.08882 }, "id": 351971 } } ] } }
四、查詢act對象
GET shop-2018.01.10/shop/_search { "query": { "nested": { "path": "acts", "query": { "bool": { "must": [ { "term": { "acts.title.keyword": { "value": "江小魚江魚館 100 元代金券" } } } ] } } } } }
返回數據索引
{ "took": 18, "timed_out": false, "_shards": { "total": 10, "successful": 10, "failed": 0 }, "hits": { "total": 1, "max_score": 8.879055, "hits": [ { "_index": "shop-2018.01.10", "_type": "shop", "_id": "351971", "_score": 8.879055, "_source": { "status": 1, "city": { "id": 587, "name": "黃石" }, "update_time": "2018-01-09 14:41:12.892000", "tel": [ "0714-6289788" ], "name": "江小魚江魚館(黃石店)", "tags": [ "餐飲服務", "餐飲相關場所", "餐飲相關" ], "tags_enrich": { "name": "美食", "id": 10 }, "brand": { "id": 35572, "name": "江小魚江魚館" }, "acts": [ { "zone_exclude_id": [], "create_time": "2017-11-30 03:57:18.732000", "id": 269791, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800061", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人**每週三上午 10:00** 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 每單僅限使用 5 張 - 酒水飲料、主食除外 - 僅限週三使用 - 不與商戶其餘優惠同享 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:50:46.481000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人每週三上午 10:00 至招行「掌上生活 APP」 50 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "每單僅限使用 5 張", "酒水飲料、主食除外", "僅限週三使用", "不與商戶其餘優惠同享", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "(週三5折)江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FriUhDNz2c4KICWvyc-ETd-xk7Gt", "available_time_ranges": [ { "range_type": 2, "start_time": 36000, "end_time": 86100, "weekday": 2, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券 (週三 5 折)", "is_hot": 0, "money_limit": null }, { "zone_exclude_id": [], "create_time": "2017-11-29 13:16:43.594000", "id": 269793, "subject": { "type": 1, "name": "bank" }, "category": [ { "name_en": "", "id": 316, "name": "中餐" } ], "special_dates": [], "original_url": "https://piao.o2o.cmbchina.com/shopwindow/weixinProductDetail?cityNo=714&productNo=9917112800078", "special_weekdays": [], "content": """ ##### 參與方式 - 在招行「掌上生活 APP」購劵 - 至「江小魚江魚館」使用 ##### 活動內容 - 活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券 ##### 注意事項 - 不與商戶其餘優惠同享 - 酒水飲料、主食除外 - 具體使用細則請以銀行和門店公告爲準 """, "usage_scene": [ { "name_en": "food", "id": 2, "name": "餐飲" } ], "start_date": "2017-11-29", "status": 1, "subject_description": "招商銀行信用卡持卡人", "update_time": "2018-01-02 15:54:00.766000", "card_orgs": [], "end_date": "2018-03-31", "bank_logo_enrich": { "logo_url": "https://cdn.xhqb.com/bank/picture_xh/11.png" }, "receive_description": "", "unavailable_time_ranges": [], "quota": "數量有限,先到先得", "zone_include": [ "黃石" ], "discount": [ { "name_en": "free_coupon", "id": 22, "name": "享券" } ], "category_enrich": { "name": "美食", "id": 10 }, "act_area": null, "content_enrich": { "參與方式": [ "在招行「掌上生活 APP」購劵", "至「江小魚江魚館」使用" ], "活動內容": [ "活動期間,招商銀行信用卡持卡人至招行「掌上生活 APP」 80 元購「江小魚江魚館」價值 100 元代金券" ], "注意事項": [ "不與商戶其餘優惠同享", "酒水飲料、主食除外", "具體使用細則請以銀行和門店公告爲準" ] }, "payment": [ { "name_en": "", "id": 169, "name": "網銀快捷支付" } ], "small_img_url": null, "act_type": 1, "zone_include_id": [ 587 ], "banks_enrich": { "name": "招商", "id": 11 }, "original_title": "江小魚江魚館100元代金券", "display_tags": [], "big_img_url": "https://qnpic.billbear.cn/FsuJGkUVpxOwCfkrenCZLsrFpxX_", "available_time_ranges": [ { "range_type": 1, "start_time": 0, "end_time": 86400, "weekday": null, "day": null } ], "zone_exclude": [], "banks": [ { "id": 11, "name": "招商銀行" } ], "title": "江小魚江魚館 100 元代金券", "is_hot": 0, "money_limit": null } ], "label": "have_act", "brand_logo_enrich": { "logo_url_lx": "https://cdn.xhqb.com/brand/picture_lx/35572.png", "logo_url_xh": "https://cdn.xhqb.com/brand/picture_xh/35572.jpg" }, "create_time": "2018-01-09 14:40:48.846000", "city_enrich": { "region": "華中地區", "name": "黃石", "level": 4 }, "address": "黃石港區天津路52號十五中對面", "coordinate": { "lat": 30.216619, "lon": 115.08882 }, "id": 351971 } } ] } }