【问题标题】:Elastic Search Function_Score Query with Query_String使用 Query_String 进行弹性搜索 Function_Score 查询
【发布时间】:2016-12-07 15:01:49
【问题描述】:

我正在使用代码使用弹性搜索进行搜索:

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

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

我被告知 function_score 应该可以解决问题

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

它给了我一个错误,字典 {"query_string": {"query": keywordstr}} 不可散列。

1) 我该如何解决这个错误?

2) 我怎样才能改变衰减函数,使其赋予更高的新近度提升更高的权重?

【问题讨论】:

    标签: python elasticsearch pyelasticsearch


    【解决方案1】:

    您的搜索中似乎有一个额外的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 也是如此,因此不需要提供它。

    【讨论】:

      【解决方案2】:

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

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

      以下应该可以正常工作

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

      【讨论】:

        【解决方案3】:

        这样使用

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

        【讨论】:

          猜你喜欢
          • 2021-02-28
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-01
          • 1970-01-01
          相关资源
          最近更新 更多