Elasticsearch内部命中孙子

时间:2015-03-31 05:45:48

标签: elasticsearch

我对新的inner_hits功能有些麻烦。 当它在父母/孩子身上使用时,它可以工作,但是如果我试图在孙子身上使用它,它似乎无法工作。

这是我的映射

{
    "test": {
        "template": "test",
        "settings": {
            "index": {
                "number_of_replicas": 0
            }
        },
        "mappings": {
            "parents": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "properties": {
                    "parent_value": {
                        "type": "string"
                    }
                }
            },
            "children": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "_parent": {
                    "type": "parents"
                },
                "properties": {
                    "parent_id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "child_value": {
                        "type": "string"
                    }
                }
            },
            "grandchildren": {
                "dynamic": "strict",
                "_routing": {
                    "required": true
                },
                "_parent": {
                    "type": "children"
                },
                "properties": {
                    "children_id": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

我通过Sense

插入数据
PUT test/parents/parent_id?routing=1
{
    "parent_value": "PARENT VALUE"
}

PUT test/children/child_id?routing=1&parent=parent_id
{
    "parent_id": "parent_id",
    "child_value": "CHILD VALUE"
}

PUT test/grandchildren/grandchild_id?routing=1&parent=child_id
{
    "children_id": "child_id"
}

这很完美

GET test/children/_search?routing=1
{
   "post_filter": {
      "bool": {
         "must": [
            {
               "has_parent": {
                  "parent_type": "parents",
                  "filter": {
                     "bool": {
                        "must": [
                           {
                              "ids": {
                                  "values": ["parent_id"]
                              }
                           }
                        ]
                     }
                  },
                  "inner_hits": {
                  }
               }
            }
         ]
      }
   }
}

耶!

但是,如果我尝试这个,它会找到一个文档,但inner_hits是空的。

GET test/grandchildren/_search?routing=1
{
   "post_filter": {
      "bool": {
         "must": [
            {
               "has_parent": {
                  "parent_type": "children",
                  "filter": {
                     "bool": {
                        "must": [
                           {
                              "ids": {
                                  "values": ["child_id"]
                              }
                           }
                        ]
                     }
                  },
                  "inner_hits": {
                  }
               }
            }
         ]
      }
   }
}

我做错了什么..?

1 个答案:

答案 0 :(得分:0)

已知issue. workaround将复制inner hits分支的所有级别的查询:

curl -XGET "http://localhost:9200/_search" -d'
{
  "query": {
    "nested": {
      "path": "cars",
      "query": {
        "nested": {
          "path": "cars.manufacturers",
          "query": {
            "match": {
              "cars.manufacturers.country": "Japan"
            }
          }
        }
      }
    }
  },
  "inner_hits": {
    "cars": {
      "path": {
        "cars": {
          "query": {
            "nested": {
              "path": "cars.manufacturers",
              "query": {
                "match": {
                  "cars.manufacturers.country": "Japan"
                }
              }
            }
          },
          "inner_hits": {
            "manufacturers": {
              "path": {
                "cars.manufacturers": {
                  "query": {
                    "match": {
                      "cars.manufacturers.country": "Japan"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'