没有为[function_score]注册任何[查询]

时间:2019-04-18 06:15:45

标签: elasticsearch

我想创建一个自定义评分功能以获得更好的结果。

我想查询tenant_id为15和customer_org_type为OA的位置。现在,如果找到了带有模糊性3的print_address,则给定权重5,如果找到了带有模糊性3的registered_name,给权重10。但是没有为[function_score]错误注册[query]。

GET addresses-index/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "function_score": {
      "query": {"bool": {}},
      "boost": "5",
      "functions": [
        {
          "query": {
            "bool": {
              "filter": {
                "bool": {
                  "must": [
                    {"match": {"tenant_id": "15"}},
                    {"match": {"customer_org_type": "LP"}}
                  ]
                }
              },
              "must": [
                {
                  "match": [
                    {
                      "print_address": {
                        "query": "FULL ADDRESS HERE",
                        "fuzziness": 3,
                        "random_score": {},
                        "weight": 5
                      }
                    },
                    {
                      "registered_name": {
                        "query": "NAME OF THE COMPANY",
                        "fuzziness": 3,
                        "random_score": {},
                        "weight": 10
                      }
                    }
                  ]
                }
              ]
            }
          }  
        }
      ],
      "max_boost": 42,
      "score_mode": "max",
      "boost_mode": "multiply",
      "min_score": 42
    }
  }
}

以下是我用作参考的文档中给出的查询。

GET /_search
{
    "query": {
        "function_score": {
          "query": { "match_all": {} },
          "boost": "5", 
          "functions": [
              {
                  "filter": { "match": { "test": "bar" } },
                  "random_score": {}, 
                  "weight": 23
              },
              {
                  "filter": { "match": { "test": "cat" } },
                  "weight": 42
              }
          ],
          "max_boost": 42,
          "score_mode": "max",
          "boost_mode": "multiply",
          "min_score" : 42
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您遇到错误,因为在function_score下您将查询用作"query": {"bool": {}}。您的查询应如下:

{
  "from": 0,
  "size": 10,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "tenant_id": "15"
              }
            },
            {
              "match": {
                "customer_org_type": "LP"
              }
            }
          ]
        }
      },
      "boost": "5",
      "functions": [
        {
          "filter": {
            "match": {
              "print_address": {
                "query": "FULL ADDRESS HERE",
                "fuzziness": 3
              }
            }
          },
          "random_score": {},
          "weight": 5
        },
        {
          "filter": {
            "match": {
              "registered_name": {
                "query": "NAME OF THE COMPANY",
                "fuzziness": 3
              }
            }
          },
          "random_score": {},
          "weight": 10
        }
      ],
      "max_boost": 42,
      "score_mode": "max",
      "boost_mode": "multiply",
      "min_score": 42
    }
  }
}