带查询字符串的弹性搜索函数

2024-10-01 11:40:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我用弹性搜索代码进行搜索:

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

现在我想在搜索评分中插入另一个参数-“recencyboost”。在

我被告知功能性得分应该能解决问题

^{pr2}$

这给了我一个错误:字典{"query_string": {"query": keywordstr}}不可哈希。在

1)如何修复错误?在

2)如何改变衰变函数,使其赋予更高的近期提升权重?在


Tags: 代码texturlfieldssearchstringindexes
3条回答

你的搜索中似乎有一个额外的query(总共三个),这给了你一个不需要的顶层。您需要删除顶层query,并将其替换为^{}作为顶层键。在

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也是如此,因此不需要提供它。在

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

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

下面的工作应该很好

^{pr2}$

像这样用

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

相关问题 更多 >