2

I have a document with a nested structure the nested object has an assignment_name and a due_date:

The mapping

{
  "goal": {
    "mappings": {
      "doc": {
        "properties": {
          "title": {
            "type": "keyword"
          },
          // lot's of other fields here ...
          "steps": {
            "type": "nested",
            "properties": {
              "assignment_name": {
                "type": "keyword"
              },
              "due_date": {
                "type": "date"
              }
              // lots of other fields here
            }
          }
        }
      }
    }
  }
}

I want to:

  1. Filter all document that have a specific assignment_name (e.g.user_a)
  2. Sort the result by the next due_date, not taking other assignements into account.

This query gives me random result (no sortings):

{  
   "query":{  
      "bool":{  
         "filter":[  
            {  
               "nested":{  
                  "path":"steps",
                  "query":{  
                     "term":{  
                        "steps.assignment_name":"user_a"
                     }
                  }
               }
            }
         ]
      }
   },
   "sort":[  
      {  
         "steps.due_date":{  
            "order":"asc",
            "nested":{  
               "path":"steps",
               "filter":{  
                  "term":{  
                     "steps.assignment_name":"user_a"
                  }
               }
            }
         }
      }
   ],
   "from":0,
   "size":25
}

1 Answer 1

2

Firstly you need to ensure that datatype for steps field is nested. Then you have to use nested sorting to sort documents based on a nested document field.

The query would be:

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "steps",
            "query": {
              "term": {
                "steps.assignment_name": "user_a"
              }
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "steps.due_date": {
        "order": "asc",
        "nested": {
          "path": "steps",
          "filter": {
            "term": {
              "steps.assignment_name": "user_a"
            }
          }
        }
      }
    }
  ]
}

The catch above is to use the same filter in sort as used in the main query to filter the documents. This ensures that the correct nested document's field value is considered to sort the documents.

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

14 Comments

Hm. That does not work. I get zero results with the .keyword postfix and random (not sorted) results without it.
@shredding Can you add mapping of index to the question?
The actual mapping is somewhat different (as the problem was easier to explain with comments, but i will update accordingly) in a sec!
Seems like you are querying before the index gets refreshed.
Look at refresh index. This should be used with care for production cluster. Though its ok to use for dev/test env and UTs.
|

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.