我正在尝试建立一个包含has_parent
连接的查询。我正在查询2种文档类型,重点是我希望始终将父级包括在内在返回的文档中。我正在使用inner_hits
进行此操作,但无法通过突出显示来实现:
DELETE /my_index
PUT my_index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},"cc": {
"type": "join","relations": {
"company": "employee"
}
}
}
}
}
PUT my_index/_doc/1?refresh
{
"name": "Walmart","cc": "company"
}
PUT my_index/_doc/2?routing=1&refresh
{
"name": "Smith","cc": {
"name": "employee","parent": "1"
}
}
POST /my_index/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"query_string": {
"default_operator": "and","fields": [
"name"
],"query": "Walmart"
}
},{
"has_parent": {
"inner_hits": {
"highlight": {
"fields": {
"name": {
"number_of_fragments": 20,"type": "plain"
}
}
}
},"parent_type": "company","query": {
"bool": {
"should": [
{
"query_string": {
"fields": [
"name"
],"query": "Walmart"
}
}
]
}
}
}
}
]
}
},{
"has_parent": {
"parent_type": "company","query": {
"match_all": {}
}
}
}
]
}
}
}
当我找到公司名称( Walmart )的员工时,inner_hits
看起来像预期的那样:
...
"inner_hits" : {
"company" : {
"hits" : {
"total" : {
"value" : 1,"relation" : "eq"
},"max_score" : 0.6931472,"hits" : [
{
"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 0.6931472,"_source" : {
"name" : "Walmart","cc" : "company"
},"highlight" : {
"name" : [
"<em>Walmart</em>"
]
}
}
]
}
}
}
...
当我按员工姓名( Smith )找到该员工时,inner_hits
未填充:
...
"inner_hits" : {
"company" : {
"hits" : {
"total" : {
"value" : 0,"max_score" : null,"hits" : [ ]
}
}
}
...
我知道我可以在外部inner_hits
连接处添加has_parent
(以将结果限制为emnployee类型),但是我失去了突出显示的结果。并将inner_hits
添加到两个has_parent
联接中均以异常结束。我该怎么做才能始终获得完整的父文档(按需突出显示)?
我正在使用ElasticSearch 7.2