9

I have the following mapping

{
    "cloth": {
                 "dynamic" : false,
                 "_source" : {"enabled" : false },
        "properties": {
            "name": {
                "type": "string",
                "index": "analyzed"
            },
            "variation": {
                "type": "nested",
                "properties": {
                    "size": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "color": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

I am not able to figure out a way to retrieve the nested object fields using the fields query.

{
    "fields" : ["name" , "variation.size", "variation.color"],
    "query" : {
        "nested" : {
            "path" : "variation",
            "query" : {
                "bool" : {
                    "must" : [
                        { "term" : { "variation.size" : "XXL" } },
                        { "term" : { "variation.color" : "red" } }
                        ]
                }
            }
        }
    }
}

The above query returns

"_id" : "1",
  "_score" : 1.987628,
  "fields" : {
    "variation.size" : [ "XXL", "XL" ],
    "variation.color" : [ "red", "black" ],
    "name" : [ "Test shirt" ]
  }

When I tried

"fields" : ["name" , "variation"]

I got the error

status: 400

reason: "ElasticsearchIllegalArgumentException[field [variation] isn't a leaf field]"

Which is as expected.

How can I get the variation object as it is?

Expected Result. I need to retrieve the variable object as a whole so that I can preserve the association of size and color. Like "red" with "XXL".

"variation" : { "XXL" , "red"}

Update: Source is disabled for this Index Type.

Community
  • 1
  • 1
user1760178
  • 6,277
  • 5
  • 27
  • 57

2 Answers2

4

If you use Source Filtering it will return the nested objects as a whole, your query would be:

{
  "_source": [
    "name",
    "variation"
  ],
  "query": {
    "nested": {
      "path": "variation",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "variation.size": "XXL"
              }
            },
            {
              "term": {
                "variation.color": "red"
              }
            }
          ]
        }
      }
    }
  }
}
Dan Tuffery
  • 5,874
  • 29
  • 28
  • Issue 1 is the source is disabled for the Type. Issue 2 is with the query you have provided the result is 2 variation objects. variation: [2] 0: { color: "red" size: "XXL" }- 1: { color: "black" size: "XL" } But I was looking for only one variation object that matches the XXL and red. – user1760178 Oct 06 '14 at 13:33
  • 1. You can't do what you want with `fields`. 2. If one of your nested objects match the query you can't only return the matching nested document, it returns the root document with all nested documents. – Dan Tuffery Oct 06 '14 at 19:55
  • I got it now. But my question here is the _source query is not helping much as the Source is disabled for this type. So what query should I be using to get the Nested Objects? – user1760178 Oct 07 '14 at 13:01
  • It is not the `query` it is the method you are using to retrieve the document values. My query above is using a Nested Query with Source Filtering. If you have disabled the `_source` object you can only use `fields`, but the `fields` option only allows you to return leaf fields and any attempt to return object fields will return an error, has you have seen. – Dan Tuffery Oct 07 '14 at 13:22
1

You should use this:

"script_fields": {
"variation": {
  "script": {
    "inline": "doc['variation.size'].value + ' ' + doc['variation.red'].value"
  }
 }
}

I use elasticsearch v. 5.1.1

  • Can you elaborate a bit how and why your proposal solves the given issue? Right now, your answer might be correct, but it is rarely explanatory... – jkalden Dec 22 '16 at 12:12
  • You are right, but maybe somebody will have similar problem so you can find answer here – Lukáš Sirhal Dec 22 '16 at 12:40
  • I just ask you to explain your answer, not to remove it! – jkalden Dec 22 '16 at 12:41
  • ou... sory I don't read your comment well - so **variation** is name of field in response, script is reserved word. Inline means calculation for actual response. **doc['variation.size']** is path to value is returned document. Is – Lukáš Sirhal Dec 23 '16 at 07:05