# ElasticSearch How to sort by document/field weight?

**URL:** <https://discuss.elastic.co/t/elasticsearch-how-to-sort-by-document-field-weight/221834>\
**Category:** Elasticsearch\
**Created:** [March 3, 2020, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-sort-by-document-field-weight/221834 "2020-03-03T08:41:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniruddha.raje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aniruddha.raje/32/52148_2.png) [@aniruddha.raje](https://discuss.elastic.co/u/aniruddha.raje)\
**Post date:** [March 3, 2020, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-sort-by-document-field-weight/221834/1 "2020-03-03T08:41:53Z")

</div>

I am trying to query and sort the search results by the weights of the documents,  
  
I used the following queries to create and query the data,  
  
ElasticSearch version: 7.6,  
  
Kibana Version: 7.2

```
DELETE movies

PUT movies
{
  "mappings": {
      "properties": {
        "name": {
          "type": "completion"
        },
        "year": {
          "type": "keyword"
        }
      }
    }
}

POST _bulk
{ "index" : { "_index" : "movies", "_id" : "1" } }
{ "name" : "Spider-Man: Far From Home", "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "2" } }
{ "name" : "Avengers: Endgame" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "3" } }
{ "name" : "Captain Marvel" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "4" } }
{ "name" : "Ant-man and the Wasp" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "5" } }
{ "name" : "Avengers: Infinity War" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "6" } }
{ "name" : "Black Panther" , "year" : "2018" }
{ "index" : { "_index" : "movies", "_id" : "7" } }
{ "name" : "Thor: Ragnarok" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "8" } }
{ "name" : "Spider-Man: Homecoming" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "9" } }
{ "name" : "Guardians of the Galaxy Vol 2" , "year" : "2017" }
{ "index" : { "_index" : "movies", "_id" : "10" } }
{ "name" : "Doctor Strange" , "year" : "2016" }
{ "index" : { "_index" : "movies", "_id" : "11" } }
{ "name" : "Guardians of the Galaxy Vol 2" , "year" : "2019"}
{ "index" : { "_index" : "movies", "_id" : "12" } }
{ "name" : "Captain America: Civil War" , "year" : "2016"}
{ "index" : { "_index" : "movies", "_id" : "13" } }
{ "name" : "Ant-Man" , "year" : "2015" }
{ "index" : { "_index" : "movies", "_id" : "14" } }
{ "name" : "Avengers: Age of Ultron" , "year" : "2015"}
{ "index" : { "_index" : "movies", "_id" : "15" } }
{ "name" : "Guardians of the Galaxy" , "year" : "2014" }
{ "index" : { "_index" : "movies", "_id" : "16" } }
{ "name" : "Captain America: The Winter Soldier" , "year" : "2014" }
{ "index" : { "_index" : "movies", "_id" : "17" } }
{ "name" : "Thor: The Dark World" , "year" : "2013" }
{ "index" : { "_index" : "movies", "_id" : "18" } }
{ "name" : "Iron Man 3" ,"year" : "2013" }
{ "index" : { "_index" : "movies", "_id" : "19" } }
{ "name" : "Marvel's The Avengers" , "year" : "2012"}
{ "index" : { "_index" : "movies", "_id" : "20" } }
{ "name" : "Captain America: The First Avenger" , "year" : "2011"}
{ "index" : { "_index" : "movies", "_id" : "21" } }
{ "name" : "Thor" , "year" : "2011"}
{ "index" : { "_index" : "movies", "_id" : "22" } }
{ "name" : "Iron Man 2", "year" : "2010" }
{ "index" : { "_index" : "movies", "_id" : "23" } }
{ "name" : "The Incredible Hulk", "year" : "2008" }
{ "index" : { "_index" : "movies", "_id" : "24" } }
{ "name" : "Iron Man" , "year" : "2008"}

PUT movies/_doc/22
{
  "name": {
    "input": ["Iron Man 2"],
    "weight": 2
  },
  "year": 2010
}

GET movies/_search
{
  "query": {
    "match": {
      "name": "iron man"
    }
  }
}

```

**I get the following output -**

```
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.754019,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "18",
        "_score" : 1.754019,
        "_source" : {
          "name" : "Iron Man 3",
          "year" : "2013"
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "24",
        "_score" : 1.754019,
        "_source" : {
          "name" : "Iron Man",
          "year" : "2008"
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "22",
        "_score" : 1.754019,
        "_source" : {
          "name" : {
            "input" : [
              "Iron Man 2"
            ],
            "weight" : 2
          },
          "year" : 2010
        }
      }
    ]
  }
}

```

My expected output is `Iron Man 2` coming on top as it's weight is higher,  
  
How do I achieve this using the query?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 31, 2020, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-how-to-sort-by-document-field-weight/221834/2 "2020-03-31T08:41:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
