Welcome to OStack Knowledge Sharing 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 Answer

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"
              }
            }
          ]
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...