title: Add sparse vector as field in Elasticsearch
body: <p>Recently Elasticsearch has allowed to use vectors and sparse vectors in queries. Following <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl-script-score-query.html#script-score-query-ex-request" rel="nofollow noreferrer">their documentation</a>, I come across an error (see <a href="https://stackoverflow.com/questions/57609746/elasticsearch-runtime-error-for-query-using-sparse-vectors">full details on this question</a>), essentially:</p>
  <pre><code>&quot;type&quot; : &quot;illegal_argument_exception&quot;,
  &quot;reason&quot; : &quot;Variable [embedding] is not defined.&quot;
  </code></pre>
  <p>It seems like &quot;embedding&quot; is not a successful field.</p>
  <p>I am uploading my documents to Elasticsearch as follows:</p>
  <ol>
  <li>I create a json file for each doc</li>
  <li>I load json files in Python <code>db_object = json.load(fp)</code></li>
  <li>I pass these objetcs to Elasticsearch: <code>es.index(index=my_index, doc_type='sentence', id=db_object['name'], body=db_object)</code></li>
  </ol>
  <p>This is the structure of my json files (note that embedding is a dictionary, as it is an sparse vector).</p>
  <pre><code>{&quot;name&quot;: &quot;doc_name&quot;, &quot;field_1&quot;: &quot;doc_id&quot;, &quot;field_2&quot;: &quot;a_keyword&quot;, &quot;text&quot;: &quot;a rather long text&quot;, &quot;embedding&quot;: {&quot;4655&quot;: 0.040158602078116556, &quot;4640&quot;: 0.040158602078116556}}
  </code></pre>
  <p>However, if I make a simple test query, embedding does not seem to work. Take this query structure:</p>
  <pre><code>curl -X GET &quot;localhost:9200/_search?pretty&quot; -H 'Content-Type: application/json' -d'
  {
    &quot;query&quot;: {
      &quot;query_string&quot; : {
        &quot;query&quot; : &quot;0.040158602078116556&quot;,
        &quot;default_field&quot; : &quot;embedding&quot;
      }
    }
  }'
  </code></pre>
  <p>If I leave <code>embedding</code> in &quot;default_field&quot;, no docs are found.</p>
  <p>If I try <code>4655</code> as the field (note this is the key in the embedding dictionary corresponding to the number I have pasted in the query) nothing happens either.</p>
  <p>However, if the field is <code>embedding.4655</code> the query works. To me, this seems like a clear sign that &quot;embedding&quot; is not being understood as a single field that is an sparse vector, but rather that the dictionary keys combine with &quot;embedding&quot; to form a collection of independent fields with a numeric value.</p>
  <p>Another hint that something may not be right is that loading documents like this made Elasticsearch protest that there were too many fields, whereas I was expecting &quot;embedding&quot; to be one and only one field.</p>
  <p>Am I correct? Is there a flaw in the way I am uploading my documents regarding this special field?</p>