2016-08-24 29 views
0

考慮包含嵌套文檔數索引文件(員工)(技能)有一個屬性,讓我們說「標籤」這樣每個員工都有一些技能。我想從ElasticSearch索引中獲取所有員工(文檔),他們掌握一組給定的技能,例如「Python」和「Java」。ElasticSearch - 查詢文件:所有給定標籤出現至少一次的嵌套文檔中

我在努力尋找一個合適的查詢,以確保所有給出技能(「巨蟒」,「Java」的)出現至少一次在一套僱員的技能,雖然他們必須一起出現!

我的映射與此類似:

{ 
    "mappings": { 
    "employee": { 
     "_all": { "enabled": false }, 
     "properties": { 
     "id" : { "type": "integer" }, 
     "first_name" : { "type": "string" }, 
     "last_name" : { "type": "string" }, 

     "skills": { 
      "type": "nested", 
      "properties": { 
      "label": { "type": "string" }, 
      "rating": { "type": "integer" } 
      } 
     } 

     } 
    } 
    } 
} 

所以我在尋找如何檢索所需結果的任何解決方案(查詢)。

回答

1

您需要使用 nested組合濾波器在bool/filter查詢,例如:

POST /employees/employee/_search 
{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "nested": { 
      "path": "skills", 
      "query": { 
       "term": { 
       "skills.label": "python" 
       } 
      } 
      } 
     }, 
     { 
      "nested": { 
      "path": "skills", 
      "query": { 
       "term": { 
       "skills.label": "java" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
}