# Getting proper sorting of product in category

**URL:** <https://discuss.elastic.co/t/getting-proper-sorting-of-product-in-category/194414>\
**Category:** Elasticsearch\
**Created:** [August 8, 2019, 11:19am UTC](https://discuss.elastic.co/t/getting-proper-sorting-of-product-in-category/194414 "2019-08-08T11:19:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![g\_tsvetanov](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@g\_tsvetanov](https://discuss.elastic.co/u/g_tsvetanov)\
**Post date:** [August 8, 2019, 11:19am UTC](https://discuss.elastic.co/t/getting-proper-sorting-of-product-in-category/194414/1 "2019-08-08T11:19:27Z")

</div>

Hello,

I'm trying to make more fancy sorting but I faced an issue by issue maybe because I'm newbie to the elasticsearch. Let me explain the situation:  
I have a category with ID 1 for example but when someone opened it the category must list all products from it and it's subcategories. They must be sorted by first match.

Here is a example request via Bulk API with my data:

```auto
{"index":{"_index":"products","_type":"product","_id":431}},
{"ProductID":431,"ProductPrice":11.79,"Categories":[{"CategoryId":3057,"Position":156},{"CategoryId":3061,"Position":1}]},
{"index":{"_index":"products","_type":"product","_id":432}},
{"ProductID":432,"ProductPrice":9.19,"Categories":[{"CategoryId":3061,"Position":43}]}

```

Here is my mapping:

```auto
{
  "products": {
    "mappings": {
      "product": {
        "properties": {
          "Categories": {
            "type": "nested",
            "dynamic": "strict",
            "properties": {
              "CategoryId": {
                "type": "long"
              },
              "Position": {
                "type": "long"
              }
            }
          },
          "ProductID": {
            "type": "long"
          }
        }
      }
    }
  }
}

```

My query to get results is something like this:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "Categories",
            "query": {
              "function_score": {
                "script_score": {
                  "script": {
                    "lang": "painless",
                    "source": "double boost_factor = 9999; if (ctx._source.containsKey('Categories')) { for (item in ctx._source.Categories) { if (item.CategoryId == params.key) { boost_factor = item[params.value]; break; } } if (boost_factor == 9999) { boost_factor = 1337; } } else { boost_factor = 7331; } return boost_factor;",
                    "params": {
                      "key": "CategoryId",
                      "value": "Position"
                    }
                  }
                },
                "query": {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "Categories.CategoryId": 3056
                        }
                      },
                      {
                        "match": {
                          "Categories.CategoryId": 3057
                        }
                      },
                      {
                        "match": {
                          "Categories.CategoryId": 3061
                        }
                      }
                    ]
                  }
                },
                "score_mode": "max",
                "boost_mode": "replace"
              }
            },
            "score_mode": "max"
          }
        }
      ]
    }
  },
  "sort": {
    "_score": {
      "order": "asc"
    },
    "_id": {
      "order": "asc"
    }
  }
}

```

When I run the query I'm always getting the same result:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... boost_factor = 9999; if (ctx._source.containsKey(' ...",
          " ^---- HERE"
        ],
        "script": "double boost_factor = 9999; if (ctx._source.containsKey('Categories')) { for (item in ctx._source.Categories) { if (item.CategoryId == params.key) { boost_factor = item[params.value]; break; } } if (boost_factor == 9999) { boost_factor = 1337; } } else { boost_factor = 7331; } return boost_factor;",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "products",
        "node": "R6TZu3XNTleJDewXtUbnuw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "script_score: the script could not be loaded",
          "index_uuid": "aFCVPFpNREW_ba8jF-lI8g",
          "index": "products",
          "caused_by": {
            "type": "script_exception",
            "reason": "compile error",
            "script_stack": [
              "... boost_factor = 9999; if (ctx._source.containsKey(' ...",
              " ^---- HERE"
            ],
            "script": "double boost_factor = 9999; if (ctx._source.containsKey('Categories')) { for (item in ctx._source.Categories) { if (item.CategoryId == params.key) { boost_factor = item[params.value]; break; } } if (boost_factor == 9999) { boost_factor = 1337; } } else { boost_factor = 7331; } return boost_factor;",
            "lang": "painless",
            "caused_by": {
              "type": "illegal_argument_exception",
              "reason": "Variable [ctx] is not defined."
            }
          }
        }
      }
    ],
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "... boost_factor = 9999; if (ctx._source.containsKey(' ...",
        " ^---- HERE"
      ],
      "script": "double boost_factor = 9999; if (ctx._source.containsKey('Categories')) { for (item in ctx._source.Categories) { if (item.CategoryId == params.key) { boost_factor = item[params.value]; break; } } if (boost_factor == 9999) { boost_factor = 1337; } } else { boost_factor = 7331; } return boost_factor;",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [ctx] is not defined."
      }
    }
  },
  "status": 400
}

```

I don't know why I'm always getting the following error "**Variable [ctx] is not defined.**" because in every example I saw (even in official documentation) everyone used "ctx".

I've looked at this example [Field\_value\_factor on nested field with dynamic mapping](https://discuss.elastic.co/t/field-value-factor-on-nested-field-with-dynamic-mapping/158501) but I can't see the working state of the query.

Any help will be much appreciated thank you in advance!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 5, 2019, 11:19am UTC](https://discuss.elastic.co/t/getting-proper-sorting-of-product-in-category/194414/2 "2019-09-05T11:19:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
