ElasticSearch:检索has_parent查询的父源

时间:2017-08-16 17:23:09

标签: elasticsearch

如果索引中有两个映射:

  • 公司
  • 雇员

如果公司是员工的父母,那么如何执行“has_parent”查询,该查询在响应中也返回父记录的来源(公司记录)?

这是公司的映射:

{
  "company" : {
    "dynamic" : "false",
    "_meta" : {
      "version" : "1.0",
      "author" : "Gil"
    },
    "_all" : {
      "enabled" : false
    },
    "properties" : {
      "name" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

这是员工的映射:

{
  "employee" : {
    "dynamic" : "false",
    "_meta" : {
      "version" : "1.0",
      "author" : "Gil"
    },
    "_all" : {
      "enabled" : false
    },
    "_parent" : {
      "type" : "company"
    },
    "_routing" : {
      "required" : true
    },
    "properties" : {
      "firstName" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "lastName" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

公司和员工都在同一个索引中。

这是一个示例has_parent查询,它使用前缀查询根据公司名称返回员工:

{
  "query" : {
    "has_parent" : {
      "parent_type" : "company",
      "query" : {
        "prefix" : {
          "name" : "one"
        }
      }
    }
  }
}

这是此查询的典型结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "companies",
      "_type" : "employee",
      "_id" : "476d8080-2cd2-4a4f-a992-852dabb5ece5",
      "_score" : 1.0,
      "_routing" : "54574774-671e-4c0a-bcc0-cb25ab02b125",
      "_parent" : "54574774-671e-4c0a-bcc0-cb25ab02b125",
      "_source" : {
        "firstName" : "Gil",
        "lastName" : "Fernandes"
      }
    } ]
  }
}

1 个答案:

答案 0 :(得分:0)

经过一番调查,我发现这是一个可能的解决方案:

{
  "from" : 0,
  "size" : 20,
  "_source" : true,
  "query" : {
    "bool" : {
      "must" : [ {
        "has_parent" : {
          "parent_type" : "company",
          "score" : true,
          "query" : {
            "wildcard" : {
              "name" : "t*"
            }
          },
          "inner_hits" : {
            "_source" : true
          }
        }
      }, {
        "match" : {
          "firstName" : "John"
        }
      } ]
    }
  }
}

此查询的结果将是:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.4700036,
    "hits" : [ {
      "_index" : "companies",
      "_type" : "employee",
      "_id" : "a53ea545-4d63-44c7-8309-7155b000a18c",
      "_score" : 1.4700036,
      "_routing" : "40110486-430a-4509-80d7-a3105b6605aa",
      "_parent" : "40110486-430a-4509-80d7-a3105b6605aa",
      "_source" : {
        "firstName" : "Gil",
        "lastName" : "Fonseca"
      },
      "inner_hits" : {
        "company" : {
          "hits" : {
            "total" : 1,
            "max_score" : 1.0,
            "hits" : [ {
              "_type" : "company",
              "_id" : "40110486-430a-4509-80d7-a3105b6605aa",
              "_score" : 1.0,
              "_source" : {
                "name" : "TUI"
              }
            } ]
          }
        }
      }
    } ]
  }
}

正如您所见,母公司的数据显示在回复中。

基本上这个表达式在查询中有所不同:

"inner_hits" : {
  "_source" : true
}

这将在内部命中请求源。