title: How can I keep my, distance representing, score field and map it into my resulting entity?
body: <p>I am using the following query:</p>
  
  <pre class="lang-java prettyprint-override"><code>NativeSearchQuery nsq = new NativeSearchQueryBuilder()
      .withQuery(qb)
      .withPageable(PageRequest.of(page, PAGE_SIZE))
      .withSort(sb)
      .build();
  </code></pre>
  
  <p>With the following SortBuilder:</p>
  
  <pre class="lang-java prettyprint-override"><code>SortBuilders.geoDistanceSort("customer.address.geoLocation",
           customer.getAddress().getGeoLocation().toGeoPoint())
      .order(SortOrder.ASC)
      .unit(DistanceUnit.KILOMETERS);
  </code></pre>
  
  <p>Which is producing the desired sort query:</p>
  
  <pre><code>"sort": [
    {
      "_geo_distance" : {
        "customer.address.geoLocation" : [
          {
            "lat" : 40.4221663,
            "lon" : -3.7148336
          }
        ],
        "unit" : "km",
        "distance_type" : "arc",
        "order" : "asc",
        "validation_method" : "STRICT",
        "ignore_unmapped" : false
      }
    }
  ]
  </code></pre>
  
  <p>Aditionaly each resulting document is returning the distance to to the reference parameter in km:</p>
  
  <pre><code>"sort": [2.4670609224864997]
  </code></pre>
  
  <p>I need this value as part as my domain but I simply cannot push it into my object. I tried a simple approach as definig it into my domain as what it seems to be a <code>float[]</code> but I keep getting null. I'm using Jackson for (de)serializing.</p>
  
  <pre class="lang-java prettyprint-override"><code>  private float[] sort;
  
    public float[] getSort() {
      return this.sort;
    }
  
    public void setSort(float[] score) {
      this.sort = score;
    }
  </code></pre>
  
  <p>My repository:</p>
  
  <pre><code>  public interface ElasticsearchProductRepository 
          extends ElasticsearchRepository&lt;Product, String&gt; {
      Page&lt;Product&gt; search(SearchQuery searchQuery);
    }
  </code></pre>
  
  <p>Product:</p>
  
  <pre><code>@Entity
  @Table(name = "product")
  @Document(indexName = "product", createIndex = true, type = "_doc")
  @Setting(settingPath = "elasticsearch/product-index.json")
  @DynamicTemplates(mappingPath = "elasticsearch/product-dynamic-templates.json")
  public class Product {
  
  ...
  
    @org.springframework.data.annotation.Id
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonView(ResponseView.class)
    private long id;
  
    @Field(analyzer = "autocomplete", type = FieldType.Text, searchAnalyzer="standard")
    @JsonView(CreationRequestView.class)
    private String productName;
    private float[] sort;
  
  ...
  
    public float[] getSort() {
      return this.sort;
    }
  
    public void setSort(float[] score) {
      this.sort = score;
    }
  
  }
  </code></pre>
  
  <p>I added the following scripted field:</p>
  
  <pre><code>  "script_fields": {
      "distance": {
        "script": {
          "lang":   "painless",
          "source": "doc['sort']",
          "params": {
          }
        }
      }
  
  }
  </code></pre>
  
  <p>But it is not finding the sort field.</p>
  
  <p><code>"reason": "No field found for [sort] in mapping with types []"</code></p>
  
  <p>It seems that doc references doc._source in reality... how can I reference this great piece of data from an Elastic script?</p>