"must" condition with array


(Metaphor) #1

Hi all,

I have the following data:

{
  "name": "A",
  "data": [
    {"field1": "foo", "field2": "foo"},
    {"field1": "bar", "field2": "bar"}
  ]
}
{
  "name": "B",
  "data": [
    {"field1": "foo", "field2": "bar"},
    {"field1": "bar", "field2": "foo"}
  ]
}

field1 is fixed and field2 is variable.
I want "A", so I created the following query:

{
  "bool": {
    "must": [
      "term": {
        "field1": "foo"
      },
      "query_string": {
        "query": "foo",
        "fields": ["field2"]
      }
    ]
  }
}

As a result, I got both "A" and "B" by this query.
I think it's because "field1" and "field2" are not evaluated at once.
How can I get only "A"?

Elasticsearch version 1.7

Thanks in advance.


(Joshua Rich) #2

You can't query array elements separately and likewise you can't get back just one element of an array. You may want to look into remodeling your data as the nested type which may provide better flexibility to do what you want.


(Metaphor) #3

Thanks, Joshua.
I'm looking into it.


(Ravi Shanker Reddy) #4

If it is possible try to upgrade your ES version. You are using very low versions. ES 2.4.0 is available now.


(system) #5