ElasticSearch How to sort by document/field weight?

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?

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