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?

4

3 回答 3

2

您的搜索中似乎有一个额外query的(总共三个),这给了您一个不需要的顶级。您需要删除顶级query并将其替换function_score为顶级密钥。

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

注意:score_mode默认为"multiply",未使用boost_mode的也是如此,因此应该没有必要提供它。

于 2014-03-17T00:03:47.423 回答
1

您不能将字典用作字典中的键。您在以下代码段中执行此操作:

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

以下应该可以正常工作

"query": {
    "query_string": {"query": keywordstr}
},
于 2015-02-16T20:26:35.320 回答
-1

像这样使用它

     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"
        }
于 2015-09-17T03:17:59.873 回答