Elastic Search Function_Score Query with Query_String Elastic Search Function_Score Query with Query_String elasticsearch elasticsearch

Elastic Search Function_Score Query with Query_String


You appear to have an extra query in your search (giving a total of three), which is giving you an unwanted top-level. You need to remove the top-level query and replace it with function_score as the top level key.

res = es.search(index="article-index", fields="url", body={"function_score": {    "query": {        { "query_string": {"query": keywordstr} }    },    "functions": {        "DECAY_FUNCTION": {            "recencyboost": {                "origin": "0",                "scale": "20"            }        }    },    "score_mode": "multiply"})

Note: score_mode defaults to "multiply", as does the unused boost_mode, so it should be unnecessary to supply it.


You cant use dictionary as a key in the dictionary. You are doing this in the following segment of the code:

"query": {    {"query_string": {"query": keywordstr}}},

Following should work fine

"query": {    "query_string": {"query": keywordstr}},


use it like this

     query: {        function_score: {          query: {            filtered: {              query: {                bool: {                   must: [                      {                        query_string: {                          query: shop_search,                          fields: [ 'shop_name']                        },                        boost: 2.0                      },                      {                        query_string: {                          query: shop_search,                          fields: [ 'shop_name']                        },                        boost: 3.0                      }                  ]                }            },            filter: {      //          { term: { search_city:  }}            }          },          exp: {            location: {                 origin: { lat:  12.8748964,                lon: 77.6413239              },              scale: "10000m",              offset: "0m",              decay: "0.5"            }          }  //        score_mode: "sum"        }