5

I'm curious if anyone knows how to query multiple subfields in an Elasticsearch index. For example records:

{
  "foo": [
    {
      "bar": "Good example",
      "subfield":  32
    }
  ]
},
{
  "foo": [
    {
      "bar": "Good example",
      "subfield":  50
    }
  ]
},
{
  "foo": [
    {
      "bar": "Bad example",
      "subfield":  32
    }
  ]
}

I'm hoping to build a query foo.bar = "Good example" AND foo.subfield = 32 where only the first record returns. Elasticsearch's query DSL or query string's are welcomed.

1 Answer 1

5

You want a nested query.

{
  "query": {
    "nested": {
      "path": "foo",
      "query": {
        "bool": {
          "must": [
            { "term": { "foo.bar": "Good example" } },
            { "term": { "foo.subfield": 32 } }
          ]
        }
      }
    }
  }
}

Note that you'll have to update your mapping to include a nested field mapping, per the documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

already have the nested field type in the mapping, haven't tried with 'term' yet. But using the bool and match it returns all 3 objects.
That worked! And realized my match usage was working too, I just was querying it wrong. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.