简体   繁体   中英

Elasticsearch: “must” query on nested fields

How to do a "must" "match" query on multiple fields under the same nesting? Here's a reproducible ES index where the "user" field is defined as "nested" type.

PUT my_index
{
    "mappings": {
        "properties": {
            "user": {
                "type": "nested",
                "properties": {
                    "firstname": {"type": "text"}
                }
            }
        }
    }
}

And here are 2 documents:

PUT my_index/_doc/1
{
  "user" : [ 
    {
      "firstname" : "John"
    },
    {
      "firstname" : "Alice"
    }
  ]
}

PUT my_index/_doc/2
{
  "user" : [ 
    {
      "firstname" : "Alice"
    }
  ]
}

For this index, how can I query for documents where "John" AND "Alice" both exist ? With the index defined above, I expect to get Document 1 but not Document 2. So far, I've tried the following code, but it's returning no hits:

GET my_index/_search 
{
    "query": {
        "nested": {
            "path": "user",
            "query": {
                "bool": {
                    "must": [
                        {"match": {"user.firstname": "John"}},
                        {"match": {"user.firstname": "Alice"}}
                    ]
                }
            }
        }
    }
}  

Below query is what is required.

POST my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "user",
            "query": {
              "match": {
                "user.firstname": "alice"
              }
            }
          }
        },
        {
          "nested": {
            "path": "user",
            "query": {
              "match": {
                "user.firstname": "john"
              }
            }
          }
        }
      ]
    }
  }
}

Notice how I've made use of two nested queries in a single must clause. That is because if you notice the documents that you have alice and john are both considered two different documents .

The query you have would work if your document structure is something like below:

POST my_index/_doc/3
{
  "user" : [    
    {
      "firstname" : ["Alice", "John"]
    }
  ]
}

Try reading this (nested datatype) and this (nested query) link to understand more on how they work and from the second link, you can see the below info:

The nested query searches nested field objects as if they were indexed as separate documents.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM