<p dir="auto">Hi,</p>
<p dir="auto">I'm working with histogram aggregation but there is something strange with keys.<br>
For instance (cf : <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html" rel="nofollow">http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html</a>):</p>
<p dir="auto">If I use this request :</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;aggs&quot; : {
        &quot;prices&quot; : {
            &quot;histogram&quot; : {
                &quot;field&quot; : &quot;price&quot;,
                &quot;interval&quot; : 50
            }
        }
    }
}"><pre class="notranslate">{
    <span class="pl-ent">"aggs"</span> : {
        <span class="pl-ent">"prices"</span> : {
            <span class="pl-ent">"histogram"</span> : {
                <span class="pl-ent">"field"</span> : <span class="pl-s"><span class="pl-pds">"</span>price<span class="pl-pds">"</span></span>,
                <span class="pl-ent">"interval"</span> : <span class="pl-c1">50</span>
            }
        }
    }
}</pre></div>
<p dir="auto">I obtain something like this :</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;aggregations&quot;: {
        &quot;prices&quot; : {
            &quot;buckets&quot;: [
                {
                    &quot;key_as_string&quot; : &quot;0&quot;,
                    &quot;key&quot;: 0,
                    &quot;doc_count&quot;: 2
                },
                {
                    &quot;key_as_string&quot; : &quot;50&quot;,
                    &quot;key&quot;: 50,
                    &quot;doc_count&quot;: 4
                },
                {
                    &quot;key_as_string&quot; : &quot;150&quot;,
                    &quot;key&quot;: 150,
                    &quot;doc_count&quot;: 3
                }
            ]
        }
    }
}"><pre class="notranslate">{
    <span class="pl-ent">"aggregations"</span>: {
        <span class="pl-ent">"prices"</span> : {
            <span class="pl-ent">"buckets"</span>: [
                {
                    <span class="pl-ent">"key_as_string"</span> : <span class="pl-s"><span class="pl-pds">"</span>0<span class="pl-pds">"</span></span>,
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">0</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">2</span>
                },
                {
                    <span class="pl-ent">"key_as_string"</span> : <span class="pl-s"><span class="pl-pds">"</span>50<span class="pl-pds">"</span></span>,
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">50</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">4</span>
                },
                {
                    <span class="pl-ent">"key_as_string"</span> : <span class="pl-s"><span class="pl-pds">"</span>150<span class="pl-pds">"</span></span>,
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">150</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">3</span>
                }
            ]
        }
    }
}</pre></div>
<p dir="auto">Instead of :</p>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;aggregations&quot;: {
        &quot;prices&quot; : {
            &quot;buckets&quot;: [
                {
                    &quot;key&quot;: 0,
                    &quot;doc_count&quot;: 2
                },
                {
                    &quot;key&quot;: 50,
                    &quot;doc_count&quot;: 4
                },
                {
                    &quot;key&quot;: 150,
                    &quot;doc_count&quot;: 3
                }
            ]
        }
    }
}"><pre class="notranslate">{
    <span class="pl-ent">"aggregations"</span>: {
        <span class="pl-ent">"prices"</span> : {
            <span class="pl-ent">"buckets"</span>: [
                {
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">0</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">2</span>
                },
                {
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">50</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">4</span>
                },
                {
                    <span class="pl-ent">"key"</span>: <span class="pl-c1">150</span>,
                    <span class="pl-ent">"doc_count"</span>: <span class="pl-c1">3</span>
                }
            ]
        }
    }
}</pre></div>
<p dir="auto">You could say, it's not important but it generates json ~1/3 bigger...<br>
Is there a mean to disable this ???</p>
<p dir="auto">Moreover, in Elasticsearch Java API, it could be fine to have a method to request the response as a hash instead keyed by the buckets keys (cf :<a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#_response_format" rel="nofollow">http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#_response_format</a>)</p>
<p dir="auto">Thanks!!!</p>
<h1 dir="auto">Completion Suggester V2</h1>
<p dir="auto">The completion suggester provides auto-complete/search-as-you-type functionality.<br>
This is a navigational feature to guide users to relevant results as they are typing, improving search precision. It is not meant for spell correction or did-you-mean functionality like the term or phrase suggesters.</p>
<p dir="auto">The completions are indexed as a weighted FST (finite state transducer) to provide fast Top N prefix-based<br>
searches suitable for serving relevant results as a user types.</p>
<p dir="auto"><strong>Notable Features</strong>:</p>
<ul dir="auto">
<li>Document oriented suggestions:
<ul dir="auto">
<li>Near-real time.</li>
<li>Deleted document filtering.</li>
<li>Multiple Context support.</li>
<li>Return document field values via <code class="notranslate">payload</code>.</li>
</ul>
</li>
<li>Query Interface:
<ul dir="auto">
<li>Regular expression support via <code class="notranslate">regex</code>.</li>
<li>Typo tolerance via <code class="notranslate">fuzzy</code>.</li>
<li>Context boosting at query time.</li>
</ul>
</li>
</ul>
<p dir="auto">Completion Suggester V2 is based on <a href="https://issues.apache.org/jira/browse/LUCENE-6339" rel="nofollow">LUCENE-6339</a> and <a href="https://issues.apache.org/jira/browse/LUCENE-6459" rel="nofollow">LUCENE-6459</a>, the first iteration of Lucene's new <em>suggest</em> API.</p>
<h2 dir="auto">Mapping</h2>
<p dir="auto">The completion fields are indexed in a special way, hence a field mapping has to be defined.<br>
Following shows a field mapping for a completion field named <em>title_suggest</em>:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="PUT {INDEX_NAME}
{
 &quot;mappings&quot;: {
  {TYPE_NAME}: {
   &quot;properties&quot;: {
    &quot;title_suggest&quot;: {
     &quot;type&quot;: &quot;completion&quot;
   }
  }
 }
}"><pre class="notranslate">PUT {INDEX_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>mappings<span class="pl-pds">"</span></span>: {
  {TYPE_NAME}: {
   <span class="pl-s"><span class="pl-pds">"</span>properties<span class="pl-pds">"</span></span>: {
    <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>: {
     <span class="pl-s"><span class="pl-pds">"</span>type<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span>
   }
  }
 }
}</pre></div>
<p dir="auto">You can choose index and search time analyzer for the completion field by adding <code class="notranslate">analyzer</code><br>
and <code class="notranslate">search_analyzer</code> options.</p>
<h3 dir="auto">Context Mappings</h3>
<p dir="auto">Adding a <code class="notranslate">contexts</code> option in the field mapping defines a context-enabled completion field. You may want<br>
a context-enabled completion field, if you require filtering or boosting suggestions by a criteria other than<br>
just its prefix. Note that adding high-cardinality context values will increase the size of the in-memory<br>
index significantly.</p>
<p dir="auto">There are two types of supported context types: <code class="notranslate">category</code> and <code class="notranslate">geo</code>.</p>
<h4 dir="auto">Category Context Mapping</h4>
<p dir="auto">Category contexts are indexed as prefixes to the completion field value.</p>
<p dir="auto">The following adds a category context named <em>genre</em>:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="...
&quot;contexts&quot;: [
 {
   &quot;name&quot;: &quot;genre&quot;,
   &quot;type&quot;: &quot;category&quot;
 }
]"><pre class="notranslate">...
<span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span>: [
 {
   <span class="pl-s"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>type<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>category<span class="pl-pds">"</span></span>
 }
]</pre></div>
<p dir="auto">You can also pull context values from another field in a document by using a <code class="notranslate">path</code> option specifying the field name.</p>
<h4 dir="auto">Geo Context Mapping</h4>
<p dir="auto">Geo points are encoded as geohash strings and prefixed to the completion field value.<br>
The following adds a geo context named <em>location</em>:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="...
&quot;contexts&quot;: [
 {
   &quot;name&quot;: &quot;location&quot;,
   &quot;type&quot;: &quot;geo&quot;
 }
]"><pre class="notranslate">...
<span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span>: [
 {
   <span class="pl-s"><span class="pl-pds">"</span>name<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>location<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>type<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>geo<span class="pl-pds">"</span></span>
 }
]</pre></div>
<p dir="auto">You can also set <code class="notranslate">precision</code> option to choose the geohash length and <code class="notranslate">path</code> to pull context values from another<br>
field in the document.</p>
<h2 dir="auto">Indexing</h2>
<p dir="auto">Just like any other field, you can add multiple completion fields to a document. You can also index multiple completions<br>
for a completion field per document. Each completion value is tied to its document and can be assigned an index-time<br>
weight, which determines its relative rank among other completion values which share a common prefix.</p>
<p dir="auto">The following indexes a completion value and its weight for the <em>title_suggest</em> completion field:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX_NAME}/{TYPE_NAME}
{
 &quot;title_suggest&quot;: {
  &quot;input&quot;: &quot;title1&quot;,
  &quot;weight&quot;: 7
 }
}"><pre class="notranslate">POST {INDEX_NAME}/{TYPE_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>: {
  <span class="pl-s"><span class="pl-pds">"</span>input<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>title1<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>weight<span class="pl-pds">"</span></span>: 7
 }
}</pre></div>
<p dir="auto">You can use the short-form, if you prefer not to add <code class="notranslate">weight</code> to the completions:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX_NAME}/{TYPE_NAME}
{
 &quot;title_suggest&quot;: &quot;title1&quot;,
}"><pre class="notranslate">POST {INDEX_NAME}/{TYPE_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>title1<span class="pl-pds">"</span></span>,
}</pre></div>
<p dir="auto">Arrays are also supported to index multiple values,</p>
<p dir="auto">The following indexes multiple completion entries (<code class="notranslate">input</code> and <code class="notranslate">weight</code>) for a single document:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX_NAME}/{TYPE_NAME}
{
 &quot;title_suggest&quot;: [
  {
   &quot;input&quot;: &quot;title1&quot;,
   &quot;weight&quot;: 14
  },
  {
   &quot;input&quot;: &quot;alternate_title&quot;,
   &quot;weight&quot;: 7
  }
 ]
}"><pre class="notranslate">POST {INDEX_NAME}/{TYPE_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>: [
  {
   <span class="pl-s"><span class="pl-pds">"</span>input<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>title1<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>weight<span class="pl-pds">"</span></span>: 14
  },
  {
   <span class="pl-s"><span class="pl-pds">"</span>input<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>alternate_title<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>weight<span class="pl-pds">"</span></span>: 7
  }
 ]
}</pre></div>
<h4 dir="auto">Indexing context-enabled fields</h4>
<p dir="auto">You can use the <code class="notranslate">path</code> option previously mentioned to pull context values from another field<br>
in the document or add <code class="notranslate">contexts</code> option to the completion entry while indexing.</p>
<p dir="auto">The following explicitly indexes context values along with completions:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX_NAME}/{TYPE_NAME}
{
 &quot;genre_title_suggest&quot;: {
  &quot;input&quot;: &quot;title1&quot;,
  &quot;contexts&quot;: {
   &quot;genre&quot;: [&quot;genre1&quot;, &quot;genre2&quot;]
  },
  &quot;weight&quot;: 7
 }
}"><pre class="notranslate">POST {INDEX_NAME}/{TYPE_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>genre_title_suggest<span class="pl-pds">"</span></span>: {
  <span class="pl-s"><span class="pl-pds">"</span>input<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>title1<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span>: {
   <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span>: [<span class="pl-s"><span class="pl-pds">"</span>genre1<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>genre2<span class="pl-pds">"</span></span>]
  },
  <span class="pl-s"><span class="pl-pds">"</span>weight<span class="pl-pds">"</span></span>: 7
 }
}</pre></div>
<p dir="auto">You can also configure the <code class="notranslate">path</code> option in the context mapping to pull values from another<br>
field as follows (assuming <code class="notranslate">path</code> for the <em>genre</em> context has been set to <em>genre</em> field):</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX_NAME}/{TYPE_NAME}
{
 &quot;genre_title_suggest&quot;: &quot;title1&quot;,
 &quot;genre&quot;: [&quot;genre1&quot;, &quot;genre2&quot;]
}"><pre class="notranslate">POST {INDEX_NAME}/{TYPE_NAME}
{
 <span class="pl-s"><span class="pl-pds">"</span>genre_title_suggest<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>title1<span class="pl-pds">"</span></span>,
 <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span>: [<span class="pl-s"><span class="pl-pds">"</span>genre1<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>genre2<span class="pl-pds">"</span></span>]
}</pre></div>
<h2 dir="auto">Query Interface</h2>
<p dir="auto">The point of indexing values as completions is to be able to run fast prefix-based searches on them.<br>
You can run <strong>Prefix</strong>, <strong>Fuzzy</strong> and <strong>Regex</strong> queries on all completion fields. In case of a context-<br>
enabled completion field, providing no context indicates  <em>all</em> contexts will be considered. But you<br>
can not run a <strong>Context</strong> query on a completion field with no contexts. When a query is run on a context-<br>
enabled field, the contexts for a completion is returned with the suggestion.</p>
<h4 dir="auto">Prefix Query</h4>
<p dir="auto">The following suggests completions from the field <em>title_suggest</em> that start with <em>titl</em>:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;prefix&quot; : &quot;titl&quot;,
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;title_suggest&quot;
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>titl<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>
  }
 }
}</pre></div>
<p dir="auto">The suggestions are sorted by their index-time <code class="notranslate">weight</code>.</p>
<h4 dir="auto">Fuzzy Prefix Query</h4>
<p dir="auto">A fuzzy prefix query can serve typo-tolerant suggestions. It scores suggestions <em>closer</em> (based on its edit distance)<br>
to the provided <em>prefix</em> higher, regardless of their <em>weight</em>.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;prefix&quot; : &quot;sug&quot;,
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;suggest&quot;,
   &quot;fuzzy&quot; : {        (1)
    &quot;fuzziness&quot; : 2
   }
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>sug<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>suggest<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>fuzzy<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {        (1)
    <span class="pl-s"><span class="pl-pds">"</span>fuzziness<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 2
   }
  }
 }
}</pre></div>
<p dir="auto">Specify <em>fuzzy</em> as shown in (1) to use typo-tolerant suggester. <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#fuzzy" rel="nofollow">Full options for fuzzy</a></p>
<h4 dir="auto">Regex Prefix Query</h4>
<p dir="auto">A regex prefix query matches all the term prefixes that match a regular expression. Regex is anchored at the begining but not at the end.<br>
The suggestions are sorted by their index-time <code class="notranslate">weight</code>.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;regex&quot; : &quot;s[u|a]g&quot;,    (1)
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;suggest&quot;
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>regex<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>s[u|a]g<span class="pl-pds">"</span></span>,    (1)
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>suggest<span class="pl-pds">"</span></span>
  }
 }
}</pre></div>
<p dir="auto">Specify <em>regex</em> as shown in (1), instead of <em>prefix</em> to use regular expressions. <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax" rel="nofollow">Supported regular expression syntax</a></p>
<h3 dir="auto">Context Query</h3>
<p dir="auto">Adding <code class="notranslate">contexts</code> (1) option to the query enables filtering and/or boosting suggestions based on their context values.<br>
This query scores suggestions by multiplying the query-time <em>boost</em> withe the suggestion <em>weight</em>.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;prefix&quot; : &quot;sug&quot;,
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;genre_title_suggest&quot;,
   &quot;contexts&quot;: {           (1)
    &quot;genre&quot;: [
     {
      &quot;value&quot; : &quot;rock&quot;, 
      &quot;boost&quot; : 3
     },
     {
      &quot;value&quot; : &quot;indie&quot;,
      &quot;boost&quot; : 2
     }
    ]
   }
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>sug<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>genre_title_suggest<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span>: {           (1)
    <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span>: [
     {
      <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>rock<span class="pl-pds">"</span></span>, 
      <span class="pl-s"><span class="pl-pds">"</span>boost<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 3
     },
     {
      <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>indie<span class="pl-pds">"</span></span>,
      <span class="pl-s"><span class="pl-pds">"</span>boost<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 2
     }
    ]
   }
  }
 }
}</pre></div>
<p dir="auto">The contexts can also be specified without any boost:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="  ...
  &quot;contexts&quot;: {
    &quot;genre&quot; : [&quot;rock&quot;, &quot;indie&quot;]
  }"><pre class="notranslate">  ...
  <span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span>: {
    <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [<span class="pl-s"><span class="pl-pds">"</span>rock<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>indie<span class="pl-pds">"</span></span>]
  }</pre></div>
<h4 dir="auto">Geo Context Query:</h4>
<p dir="auto">The result will be scored such that the suggestions are first sorted by the distance between the corresponding geo context and the provided<br>
geo location and then by the weight of the suggestions.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="  ...
  &quot;contexts&quot; : {
    &quot;location&quot; : {
      &quot;context&quot; : {
        &quot;lat&quot; : ..,
        &quot;lon&quot; : ..
      },
      &quot;precision&quot; : ..
    }
  }"><pre class="notranslate">  ...
  <span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
    <span class="pl-s"><span class="pl-pds">"</span>location<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
      <span class="pl-s"><span class="pl-pds">"</span>context<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
        <span class="pl-s"><span class="pl-pds">"</span>lat<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> ..,
        <span class="pl-s"><span class="pl-pds">"</span>lon<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> ..
      },
      <span class="pl-s"><span class="pl-pds">"</span>precision<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> ..
    }
  }</pre></div>
<h4 dir="auto">Example</h4>
<p dir="auto">The following performs a <strong>Fuzzy Prefix Query</strong> combined with a <strong>Context Query</strong> on a context-enabled completion field named <em>genre_song_suggest</em>.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;prefix&quot; : &quot;like a roling st&quot;,
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;genre_song_suggest&quot;,
   &quot;fuzzy&quot; : {
    &quot;fuzziness&quot; : 2
   },
   &quot;contexts&quot; : {
    &quot;genre&quot;: [
     {
      &quot;context&quot; : &quot;rock&quot;, 
      &quot;boost&quot; : 3
     },
     {
      &quot;context&quot; : &quot;indie&quot;,
      &quot;boost&quot; : 2
     }
    ]
   }
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>like a roling st<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>genre_song_suggest<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>fuzzy<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
    <span class="pl-s"><span class="pl-pds">"</span>fuzziness<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 2
   },
   <span class="pl-s"><span class="pl-pds">"</span>contexts<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
    <span class="pl-s"><span class="pl-pds">"</span>genre<span class="pl-pds">"</span></span>: [
     {
      <span class="pl-s"><span class="pl-pds">"</span>context<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>rock<span class="pl-pds">"</span></span>, 
      <span class="pl-s"><span class="pl-pds">"</span>boost<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 3
     },
     {
      <span class="pl-s"><span class="pl-pds">"</span>context<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>indie<span class="pl-pds">"</span></span>,
      <span class="pl-s"><span class="pl-pds">"</span>boost<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 2
     }
    ]
   }
  }
 }
}</pre></div>
<p dir="auto">This query will return <strong>all</strong> song names for the genre <em>rock</em> and <em>indie</em> that are within an edit distance of <em>2</em> from the prefix <em>like a roling st</em>.<br>
The song names with genre of <em>rock</em> will be boosted higher then that of <em>indie</em>.<br>
The completion field values that share the longest prefix with <em>like a roling st</em> will be additionally boosted higher.</p>
<h3 dir="auto">Payload</h3>
<p dir="auto">You can retrieve any document field values along with its completions using the <code class="notranslate">payload</code> option.<br>
The following returns the <em>url</em> field with each suggestion entry:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="POST {INDEX}/_suggest
{
 &quot;suggest-namespace&quot; : {
  &quot;prefix&quot; : &quot;titl&quot;,
  &quot;completion&quot; : {
   &quot;field&quot; : &quot;title_suggest&quot;,
   &quot;payload&quot; : [&quot;url&quot;]
  }
 }
}"><pre class="notranslate">POST {INDEX}/_suggest
{
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
  <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>titl<span class="pl-pds">"</span></span>,
  <span class="pl-s"><span class="pl-pds">"</span>completion<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> {
   <span class="pl-s"><span class="pl-pds">"</span>field<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>title_suggest<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>payload<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [<span class="pl-s"><span class="pl-pds">"</span>url<span class="pl-pds">"</span></span>]
  }
 }
}</pre></div>
<p dir="auto">The response format is as follows:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
 ...
 &quot;suggest-namespace&quot; : [ 
  {
   &quot;prefix&quot; : &quot;sugg&quot;,
   &quot;offset&quot; : 0,
   &quot;length&quot; : 4,
   &quot;options&quot; : [ 
    {
     &quot;text&quot; : &quot;suggestion&quot;,
     &quot;score&quot; : 34.0, 
     &quot;payload&quot;: {
       &quot;url&quot; : [ &quot;url_1&quot; ]
     }
    }
   ]
  } 
 ]
}"><pre class="notranslate">{
 ...
 <span class="pl-s"><span class="pl-pds">"</span>suggest-namespace<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [ 
  {
   <span class="pl-s"><span class="pl-pds">"</span>prefix<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>sugg<span class="pl-pds">"</span></span>,
   <span class="pl-s"><span class="pl-pds">"</span>offset<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 0,
   <span class="pl-s"><span class="pl-pds">"</span>length<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 4,
   <span class="pl-s"><span class="pl-pds">"</span>options<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [ 
    {
     <span class="pl-s"><span class="pl-pds">"</span>text<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> <span class="pl-s"><span class="pl-pds">"</span>suggestion<span class="pl-pds">"</span></span>,
     <span class="pl-s"><span class="pl-pds">"</span>score<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> 34.0, 
     <span class="pl-s"><span class="pl-pds">"</span>payload<span class="pl-pds">"</span></span>: {
       <span class="pl-s"><span class="pl-pds">"</span>url<span class="pl-pds">"</span></span> <span class="pl-c1">:</span> [ <span class="pl-s"><span class="pl-pds">"</span>url_1<span class="pl-pds">"</span></span> ]
     }
    }
   ]
  } 
 ]
}</pre></div>