2017-01-13 92 views
0

我有以下文件類型:ElasticSearch _search查詢和嵌套文件

"_source": { 
    "name": "abc", 
    "email": "[email protected]", 
    "surname": "abcdn", 
    "custom_attributes": { 
     "custom1": "2bdwfwefgwef", 
     "custom2": "2015-08-03 00:43:00", 
     "custom3": "United States (English)" 
    }, 
    "language": "US", 
    "gender": "m" 
    } 

我想過濾通過,也把它的嵌套元素沒有濾波(在這種情況下,它是「custom_attributes」)

下面是我正在使用的查詢;

{ 
    "from" : 0, "size" : 10, 
    "query": { 
     "filtered": { 
      "filter": { 
      "and" : [ 
       {"missing" : { "field" : "name" } }, 
       {"missing" : { "field" : "surname" } } 
      ] 
     } 
     } 
     } 
} 

但問題是,它不帶孩子元素(它作爲空的地圖()和我不能弄清楚如何使用「嵌套」符號在這裏帶來的custom_attributes

+1

你能提供一個結果的例子嗎?即使沒有將它映射爲嵌套,您也應該獲得子文檔。 – mayid

+0

你能提供你使用的地圖嗎? – paweloque

回答

0

你應該使用bool查詢我試着用下面簡單的例子(動態映射):

POST my_index/my_type/ 
{ 
    "name": "a", 
    "surname": "a", 
    "email": "[email protected]", 
    "custom_attributes": { 
    "custom1": "2bdwfwefgwef", 
    "custom2": "2015-08-03 00:43:00", 
    "custom3": "United States (English)" 
    }, 
    "language": "US", 
    "gender": "m" 
} 
POST my_index/my_type/ 
{ 
    "name": "b", 
    "email": "[email protected]", 
    "custom_attributes": { 
    "custom1": "2bdwfwefgwef", 
    "custom2": "2015-08-03 00:43:00", 
    "custom3": "United States (English)" 
    }, 
    "language": "US", 
    "gender": "m" 
} 
POST my_index/my_type/ 
{ 
    "surname": "c", 
    "email": "[email protected]", 
    "custom_attributes": { 
    "custom1": "2bdwfwefgwef", 
    "custom2": "2015-08-03 00:43:00", 
    "custom3": "United States (English)" 
    }, 
    "language": "US", 
    "gender": "m" 
} 
POST my_index/my_type/ 
{ 
    "email": "[email protected]", 
    "custom_attributes": { 
    "custom1": "2bdwfwefgwef", 
    "custom2": "2015-08-03 00:43:00", 
    "custom3": "United States (English)" 
    }, 
    "language": "US", 
    "gender": "m" 
} 
GET my_index/_search 
{ 
    "from": 0, 
    "size": 10, 
    "query": { 
    "bool": { 
     "must_not": [ 
     { 
      "exists": { 
      "field": "name" 
      } 
     }, 
     { 
      "exists": { 
      "field": "surname" 
      } 
     } 
     ] 
    } 
    } 
} 

導致:

{ 
    "took": 51, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "my_index", 
     "_type": "my_type", 
     "_id": "AVmfDTiBdjT9iO0jiYdN", 
     "_score": 1, 
     "_source": { 
      "email": "[email protected]", 
      "custom_attributes": { 
      "custom1": "2bdwfwefgwef", 
      "custom2": "2015-08-03 00:43:00", 
      "custom3": "United States (English)" 
      }, 
      "language": "US", 
      "gender": "m" 
     } 
     } 
    ] 
    } 
} 
+0

它應該是「過濾器」:{ 「和」:[ {「missing」:{「field」:「name」}}, {「missing」:{「field」:「surname」}} ] } –

+0

好的,我已經解決了我的答案。你要找的是'must_not'中的'exists'。 – xeraa