1

I have the elastic search cluster of the form:

{
    "_id":"xxxx"
    "contents":[
        {
            "name":"abc",
            "age":"24"
        }
    ]
},
{
    "_id":"yyyy"
    "contents":[
        {
            "name":"xyz",
            "age":"25"
        },
        {
            "name":"pqr",
            "age":"29"
        },
    ]

}

I have to perform a sort based query on the size of the object array field 'contents'.

I have tried:

{
    "size": 10,
    "from": 0,
    "query": {
        "match_all": {} 
    },
    "sort" : {
        "_script" : {
            "script": "doc['contents.name'].values.size()",
            "order": "desc",
            "type" : "string",
        }
    }
}

The above query sometime gives me the correct result, but most of the times it fails to give the results in sorted order.

2
  • 1
    do yo tried using type : number ? Commented Sep 1, 2014 at 12:10
  • Yes, I tried that also Commented Sep 1, 2014 at 12:39

1 Answer 1

1

Your contents field is an array, not an object. So when you try and access contents.name in your script it's undefined. You should get the size of the contents array:

{
  "size": 10,
  "from": 0,
  "query": {
    "match_all": {} 
  },
  "sort" : {
    "_script" : {
      "script": "doc['contents'].values.size()",
      "order": "desc",
      "type" : "string",
    }
  }
}    
Sign up to request clarification or add additional context in comments.

Comments

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.