Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

elasticsearch - Multi-Nested Elastic Query

I am trying to return all documents that match a specific keyword property in a collection.

The image below represents the document hierarchy.

The AggregateRoot has a collection of GUID Id's in the property DomainElements.

I am trying write a query that returns all documents where the Id matches a specific Guid value.

enter image description here

the following image shows the nested path in the index schema;

enter image description here

When I attempt this query I do not return any results...

{
"query": {
    "nested": {
        "path": "boundedContexts",
        "query": {
            "nested": {
                "path": "boundedContexts.aggregateRoots",
                  "query": {
                    "nested": {
                    "path": "boundedContexts.aggregateRoots.domainElements",
                        "query": {
                            "bool": {
                                "must": [
                                    { "match": { "domainelements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36" } }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

}

Here is the index documents which shows the data I am trying to query does exist ...

enter image description here

What am I missing?

Is there a more simplified way to query these results?

question from:https://stackoverflow.com/questions/65862449/multi-nested-elastic-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You almost got it -- just make sure the complete nested path is included in the field name you're targeting:

{
  "query": {
    "nested": {
      "path": "boundedContexts",
      "query": {
        "nested": {
          "path": "boundedContexts.aggregateRoots",
          "query": {
            "nested": {
              "path": "boundedContexts.aggregateRoots.domainElements",
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "boundedContexts.aggregateRoots.domainElements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}

BTW there's no need for so many (sub)nested queries. The inner-most nested path is enough:

{
  "query": {
    "nested": {
      "path": "boundedContexts.aggregateRoots.domainElements",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "boundedContexts.aggregateRoots.domainElements.id": "48be3bb4-838d-44a5-85e3-c5ea4fa8ee36"
              }
            }
          ]
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...