2017-08-16 98 views
0

的父源如果在索引中有兩個映射:ElasticSearch:檢索has_parent查詢

  • 公司
  • 員工

,其中公司員工的父母,你怎麼能執行「has_parent」查詢,它也會在響應中返回父記錄的來源(公司記錄)?

這是公司的映射:

{ 
    "company" : { 
    "dynamic" : "false", 
    "_meta" : { 
     "version" : "1.0", 
     "author" : "Gil" 
    }, 
    "_all" : { 
     "enabled" : false 
    }, 
    "properties" : { 
     "name" : { 
     "type" : "text", 
     "fields" : { 
      "keyword" : { 
      "type" : "keyword", 
      "ignore_above" : 256 
      } 
     } 
     } 
    } 
    } 
} 

這是僱員映射:

{ 
    "employee" : { 
    "dynamic" : "false", 
    "_meta" : { 
     "version" : "1.0", 
     "author" : "Gil" 
    }, 
    "_all" : { 
     "enabled" : false 
    }, 
    "_parent" : { 
     "type" : "company" 
    }, 
    "_routing" : { 
     "required" : true 
    }, 
    "properties" : { 
     "firstName" : { 
     "type" : "text", 
     "fields" : { 
      "keyword" : { 
      "type" : "keyword", 
      "ignore_above" : 256 
      } 
     } 
     }, 
     "lastName" : { 
     "type" : "text", 
     "fields" : { 
      "keyword" : { 
      "type" : "keyword", 
      "ignore_above" : 256 
      } 
     } 
     } 
    } 
    } 
} 

兩個公司和僱員都在相同的索引。

這是一個示例has_parent查詢中使用前綴的查詢返回基於公司的名稱員工:

{ 
    "query" : { 
    "has_parent" : { 
     "parent_type" : "company", 
     "query" : { 
     "prefix" : { 
      "name" : "one" 
     } 
     } 
    } 
    } 
} 

這是此查詢的典型結果:

{ 
    "took" : 3, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 3, 
    "successful" : 3, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 2, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "companies", 
     "_type" : "employee", 
     "_id" : "476d8080-2cd2-4a4f-a992-852dabb5ece5", 
     "_score" : 1.0, 
     "_routing" : "54574774-671e-4c0a-bcc0-cb25ab02b125", 
     "_parent" : "54574774-671e-4c0a-bcc0-cb25ab02b125", 
     "_source" : { 
     "firstName" : "Gil", 
     "lastName" : "Fernandes" 
     } 
    } ] 
    } 
} 

回答

0

經過一番調查中,這是一個查詢,我發現是一個可能的解決方案:

{ 
    "from" : 0, 
    "size" : 20, 
    "_source" : true, 
    "query" : { 
    "bool" : { 
     "must" : [ { 
     "has_parent" : { 
      "parent_type" : "company", 
      "score" : true, 
      "query" : { 
      "wildcard" : { 
       "name" : "t*" 
      } 
      }, 
      "inner_hits" : { 
      "_source" : true 
      } 
     } 
     }, { 
     "match" : { 
      "firstName" : "John" 
     } 
     } ] 
    } 
    } 
} 

的RESU lt此查詢將是:

{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 3, 
    "successful" : 3, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.4700036, 
    "hits" : [ { 
     "_index" : "companies", 
     "_type" : "employee", 
     "_id" : "a53ea545-4d63-44c7-8309-7155b000a18c", 
     "_score" : 1.4700036, 
     "_routing" : "40110486-430a-4509-80d7-a3105b6605aa", 
     "_parent" : "40110486-430a-4509-80d7-a3105b6605aa", 
     "_source" : { 
     "firstName" : "Gil", 
     "lastName" : "Fonseca" 
     }, 
     "inner_hits" : { 
     "company" : { 
      "hits" : { 
      "total" : 1, 
      "max_score" : 1.0, 
      "hits" : [ { 
       "_type" : "company", 
       "_id" : "40110486-430a-4509-80d7-a3105b6605aa", 
       "_score" : 1.0, 
       "_source" : { 
       "name" : "TUI" 
       } 
      } ] 
      } 
     } 
     } 
    } ] 
    } 
} 

正如您可以看到母公司的數據顯示在響應中。

基本上這個表達式使得在查詢中的差:

"inner_hits" : { 
    "_source" : true 
} 

這將要求在所述內命中的來源。