elasticsearch:返回匹配的父级和匹配/不匹配的子级

8wtpewkr  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(0)|浏览(330)

我正在使用ElasticSearch7.8.1,并使用父子方法对文档进行索引。我的要求是搜索父文档和子文档,但返回的响应格式为父文档是主文档,子文档是父文档中的字段。即

1) If the child matches, I wish to return parent & child in a document. I am able to achieve this using has_child and inner_hits.
2) If the parent matches the query, I wish to return parent and child in a document even if the child does not matches. (Not sure how to achieve this)

# This is the parent child relationship mapping in index

* curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d'

{
  "mappings": {
    "properties": {
      "my_id": {
        "type": "keyword"
      },
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}
'*

Below is the query I am trying to use, but it does not return the child when the parent matches:

* curl -X POST "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'

{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "answer",
            "query": {
              "match": {
                "my_id": "4"
              }
            },
            "inner_hits": {
              "size": 1
            }
          }
        },
        {
          "match": {
            "my_id": "1"
          }
        }
      ]
    }
  }
}'*

父文档curl-x put“localhost:9200/my-index-000001/_doc/1?refresh&pretty“-h”content type:application/json'-d'{“my\u id”:“1”,“text”:“这是一个问题”,“my\u join\u field”:“问题”}“curl-x put”localhost:9200/my-index-000001/_doc/2?refresh&pretty“-h”内容类型:application/json'-d'{“my\u id”:“2”,“text”:“这是另一个问题”,“我的加入域”:“问题”}'

“子文档 curl -x放置”localhost:9200/my-index-000001/_doc/3?routing=1&refresh&pretty“-h”内容类型:application/json'-d'{“my\u id”:“3”,“text”:“this is an answer”,“my\u join\u field”:{“name”:“answer”,“parent”:“1”}}“curl-x put”localhost:9200/my-index-000001/_doc/4?routing=1&refresh&pretty“-h”内容类型:application/json'-d'{“my\u id”:“4”,“text”:“这是另一个答案”,“my\u join\u field”:{“name”:“answer”,“parent”:“1”}”

如何搜索父级和子级,但将子级作为父级文档中的字段返回。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题