2

I need to sort documents in my elastic index using a field inside an array in that document. Document structure looks like this,

{
  'name':'xxxxx',
  'comments':[
     {comment: 'xxxxx', 'commentDate':'yyyy-MM-dd HH:mm:ss', 'id':1},
     {comment: 'xxxxx', 'commentDate':'yyyy-MM-dd HH:mm:ss', 'id':2}
   ]
}

What I want to do is sort the documents based on the commentDate field to get the documents with latest comments first. So far I've think of keeping separate field inside the document to track the last comment date. So the document becomes,

{
  'name':'xxxxx',
  'comments':[
     {comment: 'xxxxx', 'commentDate':'yyyy-MM-dd HH:mm:ss', 'id':1},
     {comment: 'xxxxx', 'commentDate':'yyyy-MM-dd HH:mm:ss', 'id':2}
   ],
  'lastCommentDate': 'yyyy-MM-dd HH:mm:ss'
}

I'd like to know whether is this the best way of doing this or is it possible to do this using script sort without changing the index?

I'm using Elastic API for Java and it would be very much appreciated if you can show me how to do this without changing the index. Also, I want to get the sorted data set directly from ES, so sorting after fetching data is not what I'm looking for

Elastic version : 5.1.1

9
  • What version of ES are you using? Commented Jan 25, 2018 at 6:35
  • @Amriteya I'm using version 5.1.1 Commented Jan 25, 2018 at 6:36
  • What is the query that you're running? Commented Jan 25, 2018 at 7:10
  • Also, are you setting the mapping of the index before ingesting the data? Commented Jan 25, 2018 at 7:11
  • 1
    That could be the problem. When you index automatically without setting the mapping, the nested documents don't get mapped properly. Commented Jan 25, 2018 at 20:35

1 Answer 1

1

If you're ingesting data without creating a mapping on the index, Elasticsearch will flatten the nested documents.

Elasticsearch does this to speed up the query although it is not suitable in cases like these.

Make sure you create a custom mapping before ingesting the data to be indexed.

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.