使用ElasticSearch

时间:2019-10-17 07:25:52

标签: elasticsearch

我正在尝试在ElasticSearch查询中实现自定义评分。

在描述问题之前,我将描述我的用例。

我有一个实体Item,其中包含几个字段:

  • types(关键字):类型列表
  • themes(关键字):类型列表
  • location:商品的位置

我的查询包含:

  • 首先定位商品的子集。例如,具有TYPE1但没有TYPE2TYPE3的项目
  • 第二个定义规则以计算分数。具有TYPE4的项目必须加2。具有THEME1的项目必须加3。提供的多边形内的项目必须增加4。

这是我尝试过的查询:

{
  "from": 0,
  "size": 10,
  "query": {
    "function_score": {
      "boost": 1,
      "boost_mode": "multiply",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "types": {
                  "value": "TYPE1"
                }
              }
            },
            {
              "not": {
                "filter": {
                  "term": {
                    "types": {
                      "value": [
                        "TYPE2",
                        "TYPE3"
                      ]
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "themes": "THEME1"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "term": {
              "types": "TYPE4"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "geo_polygon": {
              "location": {
                "points": [ ... ]
              }
            }
          },
          "weight": 4
        }
      ]
    }
  },
  "sort": [
    "_score"
  ]
}

在此请求下,我收到此错误:

"type": "parsing_exception",
"reason": "no [query] registered for [not]",

因此,我认为问题出在not块级别...

如果我删除了该块,则请求成功,但分数不是预期的分数(所有分数均为0)。

我启用了explain模式,可以看到以下内容:

"_explanation": {
    "value": 0,
    "description": "function score, product of:",
    "details": [
      {
        "value": 0,
        "description": "ConstantScore(types:TYPE1)^0.0",
        "details": []
      },
      {
        "value": 1,
        "description": "min of:",
        "details": [
          {
            "value": 1,
            "description": "No function matched",
            "details": []
          },
          {
            "value": 3.4028235e+38,
            "description": "maxBoost",
            "details": []
          }
        ]
      }
    ]

所以我的问题是:

  • 该查询如何使用not块?
  • 如何根据function_score计算分数?

感谢您的帮助, 蒂埃里

0 个答案:

没有答案