<p dir="auto">Sorting on documents that are missing a key would be very helpful, partially matching the api for sorting numerical values. For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
&quot;sort&quot; : [
    { &quot;user&quot; : {&quot;missing&quot; : &quot;_last&quot;} },
],
&quot;query&quot; : {
    &quot;term&quot; : { &quot;user&quot; : &quot;kimchy&quot; }
}

{
&quot;sort&quot; : [
    { &quot;user&quot; : {&quot;missing&quot; : &quot;_last&quot;} },
],
&quot;query&quot; : {
    &quot;term&quot; : { &quot;user&quot; : &quot;kimchy&quot; }
}"><pre class="notranslate"><code class="notranslate">{
"sort" : [
    { "user" : {"missing" : "_last"} },
],
"query" : {
    "term" : { "user" : "kimchy" }
}

{
"sort" : [
    { "user" : {"missing" : "_last"} },
],
"query" : {
    "term" : { "user" : "kimchy" }
}
</code></pre></div>
<p dir="auto">Unfortunately we can't use this pattern for custom values. To support that, we'll need a sub-object:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
&quot;sort&quot; : [
    { &quot;user&quot; : {&quot;missing&quot; : { &quot;order&quot;: &quot;_first&quot;} } },
],
&quot;query&quot; : {
    &quot;term&quot; : { &quot;user&quot; : &quot;kimchy&quot; }
}

{
&quot;sort&quot; : [
    { &quot;user&quot; : {&quot;missing&quot; : { &quot;order&quot;: &quot;_last&quot;} } },
],
&quot;query&quot; : {
    &quot;term&quot; : { &quot;user&quot; : &quot;kimchy&quot; }
}

{
&quot;sort&quot; : [
    { &quot;user&quot; : {&quot;missing&quot; : { &quot;value&quot;: &quot;nobody&quot;} } },
],
&quot;query&quot; : {
    &quot;term&quot; : { &quot;user&quot; : &quot;kimchy&quot; }
}"><pre class="notranslate"><code class="notranslate">{
"sort" : [
    { "user" : {"missing" : { "order": "_first"} } },
],
"query" : {
    "term" : { "user" : "kimchy" }
}

{
"sort" : [
    { "user" : {"missing" : { "order": "_last"} } },
],
"query" : {
    "term" : { "user" : "kimchy" }
}

{
"sort" : [
    { "user" : {"missing" : { "value": "nobody"} } },
],
"query" : {
    "term" : { "user" : "kimchy" }
}
</code></pre></div>
<p dir="auto">This is a follow up to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="667519" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/772" data-hovercard-type="issue" data-hovercard-url="/elastic/elasticsearch/issues/772/hovercard" href="https://github.com/elastic/elasticsearch/issues/772">#772</a> that supports special sorting for 'null' values for numeric fields. The same would be very useful for (not analyzed) string fields as well. If feasible, special handling for empty and blank strings would be useful. The latter is however optional as it is possible to achieve uniform handling by filtering out blank values at indexing time.</p>