ElastricSearch的parent_child的應用

ElasticSearch 這樣的分佈式系統中執行全 SQL 風格的鏈接查詢代價昂貴,是不可行的。相應地,爲了實現水平規模地擴展,ElasticSearch 提供了兩種形式的 join。html

一、nested query (嵌套查詢)json

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

二、has_child (有子查詢) and has_parent (有父查詢) queriesapp

類父子關係能夠存在單個的索引的兩個類型的文檔之間。has_child 查詢將返回其子文檔能知足特定的查詢的父文檔,而 has_parent 則返回其父文檔能知足特定查詢的子文檔分佈式

下面詳細的談談parent_child的應用:ui

實用場景:一個父存在多個子。例如一個班級有不少的學生。不建議是一個子存在多個父的這種狀況,例如一個學生多個班級。搜索引擎

理由是:父和子是綁定到同一個分片,若是一個子存在多個父的狀況,恰好這幾個parent_id在同一個分片時。只能寫進一條記錄。url

具體場景:spa

現有商店(做爲子)、活動(做爲父),商店與門店的關係3d

爲了達到經過商店的過濾條件查詢活動的具體信息、經過活動的過濾條件查詢到商店的具體信息。

①:創建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
  }

③雙向查詢

一、經過商店查詢到活動

GET act_shop-2018.01.08/_search
{
  "query": {
    "has_child": {
      "type": "shop",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "name.keyword": {
                  "value": "百果園(白石中路1店)"
                }
              }
            }
          ]
        }
      }
    }
  }
}

二、經過活動能夠查詢到相應的商戶

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。有興趣的話能夠看一下官網的文檔。

相關文章
相關標籤/搜索