2012-12-07 184 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