Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notes on script sorting to the docs #145

Open
nagyt234 opened this issue Oct 25, 2017 · 3 comments
Open

Add notes on script sorting to the docs #145

nagyt234 opened this issue Oct 25, 2017 · 3 comments
Labels
type:docs Documentation

Comments

@nagyt234
Copy link

It would be good to have the possibility to use script sorting of ElasticSearch. E.g.:

{
  "sort": {
    "_script": {
      "type": "number",
      "script": "return rdoc['nutrient_220_rda_rate'].value + doc['nutrient_221_rda_rate'].value",
      "lang": "groovy",
      "order": "asc"
    }
  }
}

It is possible to use script field:

$query->fields = array_merge($allFields, [
    'rda_sum' => "doc['nutrient_220_rda_rate'].value + doc['nutrient_221_rda_rate'].value",
]);

It would be the best if we could use such a script field in the orderBy method.

@j2443070
Copy link

Any chance for this feature?

@bizley
Copy link
Member

bizley commented Apr 22, 2022

All PRs are welcome.

@beowulfenator
Copy link
Collaborator

This has actually been implemented. One caveat: you can not sort on a script field by providing its name. You actually need to provide the script. Here's the example from the official docs:

{
  "query": {
    "term": {
      "sold": "true"
    }
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "doc['theatre'].value.length() * params.factor",
        "params": {
          "factor": 1.1
        }
      },
      "order": "asc"
    }
  }
}

Let's rewrite this using yii2-elasticsearch.

$query->where(['sold' => 'true'])->orderBy(['_script' => [
    'lang' => 'painless'
    'source' => "doc['theatre'].value.length() * params.factor",
    'params' => [
        'factor' => 1.1
    ]
]]);

Note how the value for the _script key is an array, not a string.

This needs to be put into the docs.

@beowulfenator beowulfenator changed the title Add new feature: script sorting Add notes on script sorting to the docs Apr 16, 2023
@beowulfenator beowulfenator added type:docs Documentation and removed type:enhancement Enhancement labels Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

5 participants