3

I was doing search using elastic search using the code:

es.search(index="article-index", fields="url", body={
  "query": {
    "query_string": {
      "query": "keywordstr",
      "fields": [
        "text",
        "title",
        "tags",
        "domain"
      ]
    }
  }
})

Now I want to insert another parameter in the search scoring - "recencyboost".

I was told function_score should solve the problem

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

It gives me error that dictionary {"query_string": {"query": keywordstr}} is not hashable.

1) How can I fix the error?

2) How can I change the decay function such that it give higher weight to higher recency boost?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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}
},

Comments

-1

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"
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.