【问题标题】:ElasticSearch function_score throwing parsing_exceptionElasticSearch function_score 抛出 parsing_exception
【发布时间】:2018-10-07 17:28:56
【问题描述】:

我在尝试让 function_score 为价格字段工作时遇到了烦人的麻烦。最初我尝试使用 scaled_float 字段。然而它不喜欢那样。所以我将我的价格字段更改为 long 并填充小数位。所以我有一个带有“15000”的字段,价格为 150.00 美元。

这是我对 /products_v7/product/_search 的查询

    {
   "explain":true,
   "query":{
      "function_score":{
         "query":{
            "bool":{
               "should":[
                  {
                     "match_phrase":{
                        "title":{
                           "query":"test",
                           "slop":10
                        }
                     }
                  }
               ]
            }
         },
         "functions":[
            {
               "gauss":{
                  "price":{
                     "origin":"15000",
                     "scale":"2000"
                  }
               },
               "weight":2
            }
         ]
      }
   }
}

这是回复

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "unknown field [price]",
                "line": 1,
                "col": 0
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "products_v7",
                "node": "cd3yjjoSSxKxaJ-vCB8SgQ",
                "reason": {
                    "type": "parsing_exception",
                    "reason": "unknown field [price]",
                    "line": 1,
                    "col": 0
                }
            }
        ]
    },
    "status": 400
}

/products_v7/product/_mapping 的映射

{
    "products_v7": {
        "mappings": {
            "product": {
                "properties": {
                    "price:": {
                        "type": "long"
                    },
                    "sku": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "title": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

这是我推送的数据

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "products_v7",
                "_type": "product",
                "_id": "cSULBGMBog6d8NyO0gRH",
                "_score": 1,
                "_source": {
                    "sku": "126",
                    "title": "test 4",
                    "price:": 15000
                }
            },
            {
                "_index": "products_v7",
                "_type": "product",
                "_id": "fl0FBGMBog0jN_eMK89-",
                "_score": 1,
                "_source": {
                    "sku": "125",
                    "title": "test 3",
                    "price:": 13000
                }
            }
        ]
    }
}

【问题讨论】:

    标签: elasticsearch elastic-stack querydsl elasticsearch-dsl elasticsearch-query


    【解决方案1】:

    问题在于,在您的文档中,price 字段被称为 price:(即名称中有冒号。这些细节很重要。

    {
         "sku": "126",
         "title": "test 4",
         "price:": 15000
               ^
               |
            see here
    }
    

    【讨论】:

    • 是的!谢谢!其实是别人看的时候才发现的。他们一下子就找到了!有时我是盲人。我仍然有缩放浮动的未知问题。我会再试一次,也许会提出一个新问题。谢谢!
    • 太棒了,很高兴你知道了。
    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 2016-02-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2020-03-08
    • 2019-10-11
    相关资源
    最近更新 更多