【问题标题】:searching child-parent in elasticsearch在elasticsearch中搜索子父级
【发布时间】:2017-02-22 17:44:32
【问题描述】:

如果我在弹性搜索中有子父映射。 我将如何根据孩子的统计数据搜索父母。

例子:

{
  'parent':{
    '_id':{
      'path':'parent_id'
    },
    'properties':{
      'parent_id':{
        'type':'string'
      },
      'name':{
        'type':'string'
      },
      'job':{
        'type':'string'
      },
      'age':{
        'type':'integer'
      },

    }
  }
}{
  'child':{
    '_parent':{
      'type':'parent'
    },
    '_id':{
      'path':'child_id'
    },
    'properties':{
      'child_id':{
        'type':'string'
      },
      'name':{
        'type':'string'
      },
      'favorite_toy':{
        'type':'string'
      },
      'age':{
        'type':'integer'
      },

    }
  }
}

我会怎么做:

  1. 获取所有有一个名为“bob”的孩子的父母?
  2. 计算所有有一个名为“bob”的孩子的父母?
  3. 获取所有有两个以上孩子的父母?
  4. 获取所有有 2 个 5 岁以上孩子的父母?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    1) 和 2) - 您可以简单地将查询包装到 has_child 查询中:

    {
      "has_child":{
        "type":"child",
        "query":{
          "match":{
            "name":"bob"
          }
        }
      }
    }
    

    3) 和 4) - 我认为目前不可能。

    【讨论】:

    • 非常感谢,我想当我索引孩子时,我只需要用特定字段更新父母,比如“孩子的数量”。
    • @imotov 嗨,伊戈尔,我对原始问题的第 3 个问题很感兴趣,我想知道这是否是在您发帖时实施的。请告诉我
    • @user1631616 它没有实现,但如果你不关心相关性,你可以使用constant_score 对孩子的查询来伪造它(所以所有孩子都返回分数,使用sum score_type 和将min_score 设置为 2
    • 请 vm 为您回复。不,根本不关心相关性。今晚晚些时候会尝试玩它,如果你有这样的例子,那将非常有用:)
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2015-02-15
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多