Elasticsearch order by a certain field value first
<p>I'd like to apply a certain sort to a query, it should sort my documents by a single value first then all the others. I need to achieve something like the ORDER BY CASE WHEN in MySQL, but I couldn't find how to do it.</p>

<p>Each element in the index in Elastic has the following structure:</p>

<pre><code>{
    "id": 123,
    "name": "Title",
    "categories": ["A", "B", "C"],
    "price": 100,
    "city": "London",
    "country": "United Kingdom",
    "status": 1
}
</code></pre>

<p>I do the following query:</p>

<pre><code>{
    "fields": [],
    "sort": [{"price": {"order": "asc"}}],
    "size": 0,
    "query": {
        "query_string": {
            "query": "status:1 AND country:'United Kingdom'"
        }
    },
    "aggs": {
        "id": {
            "terms": {
                "field": "id",
                "size": 10
            }
        }
    }
}
</code></pre>

<p>So by sorting the column <code>city</code> with value "Liverpool" first and considering the following example:</p>

<pre><code>{"id": 1, "name": "Test", "categories": ["A", "B", "C"], "price": 100, "city": "London", "country": "United Kingdom", "status": 1 }
{"id": 2, "name": "Sample", "categories": ["A", "D", "F"], "price": 200, "city": "Manchester", "country": "United Kingdom", "status": 1 }
{"id": 3, "name": "Title", "categories": ["X", "Y", "Z"], "price": 1000, "city": "Liverpool", "country": "United Kingdom", "status": 1 }
</code></pre>

<p>I expect to have as output the following id: 3, 1, 2.</p>

<p>How can I change my query to obtain this behaviour?</p>

<p><strong>UPDATE</strong>: The version is 1.7.2</p>