2012-12-07 179 views
1

我們有父子(一對多)的彈性搜索的關係,我們要檢查它的子對象的屬性(child_attr)中有任何值的所有父對象。彈性搜索has_child查詢

我們生成JSON查詢如下:

1)對於具有值條件。

{ 
       "has_child" : { 
        "query" : { 
        "filtered" : { 
         "query" : { 
         "match_all" : { } 
         }, 
         "filter" : { 
         "and" : { 
          "filters" : [ { 
          "exists" : { 
           "field" : "child_attr" 
          } 
          }, { 
          "not" : { 
           "filter" : { 
           "term" : { 
            "child_attr" : "" 
           } 
           } 
          } 
          } ] 
         } 
         } 
        } 
        }, 
        "type" : "child" 
       } 
       } 

2)對於沒有價值的條件

{ 
       "has_child" : { 
        "query" : { 
        "filtered" : { 
         "query" : { 
         "match_all" : { } 
         }, 
         "filter" : { 
         "or" : { 
          "filters" : [ { 
          "missing" : { 
           "field" : "child_attr" 
          } 
          }, { 
          "term" : { 
           "child_attr" : "" 
          } 
          } ] 
         } 
         } 
        } 
        }, 
        "type" : "child" 
       } 
       } 

這些查詢僅返回在選擇了所有子對象有一定的價值或所有子對象的父對象沒有價值的搜索屬性。

它不會返回任何部分滿足大部分數據的條件。

我也玩弄關鍵字分析器來索引這個child_attribute,但沒有喜悅。

期待您的專家建議。

回答

1

你得到意想不到的結果,因爲查詢

"missing" : { 
    "field" : "child_attr" 
} 

比賽中child_attr是缺少了在child_attr索引與空字符串兩個記錄和記錄。

查詢

"exists" : { 
    "field" : "child_attr" 
} 

是第一次查詢的確切oposite,它匹配了索引與非空child_attr場的所有記錄。

查詢

"term" : { 
    "child_attr" : "" 
} 

不匹配任何東西。

+0

我更新了我的同步代碼以使存在和缺少過濾器工作。謝謝:) – anks2089

+0

嘿是否有可能得到兒童字段中有子查詢? – Dragonborn