【问题标题】:Elastic search has_child query弹性搜索 has_child 查询
【发布时间】:2012-12-07 19:37:03
【问题描述】:

我们在弹性搜索中有一个父子(一对多)关系,我们想要检查所有父对象的子对象属性(child_attr)有任何值。

我们正在生成如下的 json 查询:

1) 对于有值条件。

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

2) For 无值条件

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

这些查询仅返回所有子对象都具有某些值或所有子对象都没有搜索属性值的父对象。

它不会返回部分满足此条件的任何内容,它涵盖了大部分数据。

我也玩弄了关键字分析器来索引这个 child_attribute 但没有乐趣。

期待您的专家建议。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你得到了意想不到的结果,因为查询

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

    匹配child_attr 中用空字符串索引的记录和缺少child_attr 的记录。

    查询

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

    与第一个查询完全相反,它匹配使用非空child_attr 字段索引的所有记录。

    查询

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

    什么都不匹配。

    【讨论】:

    • 我更新了我的同步代码以使存在和缺少的过滤器能够正常工作。谢谢:)
    • 嘿,是否可以在 has child 查询中获取子字段?
    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    相关资源
    最近更新 更多