Jpepper
Jpepper

Reputation: 437

ElasticSearch function_score throwing parsing_exception

I have having annoying trouble trying to get function_score to work for a price field. Originally I tried to used a scaled_float field. However it did not like that. So I changed my price field to a long with padding for decimal position. So I have a field with "15000" for $150.00.

Here is my query to /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
            }
         ]
      }
   }
}

Here is the response

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

Mapping for /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
                            }
                        }
                    }
                }
            }
        }
    }
}

Here is data I pushed in

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

Upvotes: 0

Views: 1991

Answers (1)

Val
Val

Reputation: 217254

The problem is that in your document the field priceis called price:(i.e. you have a colon in the name. Those details matter.

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

Upvotes: 2

Related Questions