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

How do you do this sort #323

Open
zhmm opened this issue May 22, 2020 · 2 comments
Open

How do you do this sort #323

zhmm opened this issue May 22, 2020 · 2 comments

Comments

@zhmm
Copy link

zhmm commented May 22, 2020

like query:
GET xxx/_search { "sort": { "_script": { "script": "Math.random()", "type": "number", "order": "asc" } }, "query": { } }
I didn't find the relevant documents, and I don't know how to implement such a random sort, thank you

@maluramichael
Copy link

Any news on this? Would need something like this too.

  "sort": [
    {
      "_script": {
        "script": {
          "lang": "painless",
          "inline": "doc[\"in_city_map\"].value.contains(\"12e1f7c4-42d3-11ec-aa10-0242ac110003\") ? 1 : 0"
        },
        "type": "number",
        "order": "asc"
      }
    }
  ],

@maluramichael
Copy link

maluramichael commented Nov 18, 2021

Alright fixed it myself

$search->addSort(
    new ScriptSort(
        sprintf('doc["in_city_map"].value.contains("%s") ? 1 : 0', $city->getId()),
        FieldSort::DESC
    )
);
class ScriptSort implements BuilderInterface
{
    use ParametersTrait;

    const ASC  = 'asc';
    const DESC = 'desc';

    /**
     * @var string
     */
    private $source;

    /**
     * @var string
     */
    private $order;

    /**
     * @var string
     */
    private $type;

    /**
     * @var BuilderInterface
     */
    private $nestedFilter;

    /**
     * @param string $source Source code.
     * @param string|null $order Order direction.
     * @param array $params Params that can be set to field sort.
     */
    public function __construct(string $source, ?string $order = FieldSort::ASC, ?string $type = 'number', array $params = [])
    {
        $this->source = $source;
        $this->order  = $order;
        $this->type   = $type;
        $this->setParameters($params);
    }

    /**
     * @return string
     */
    public function getSource(): string
    {
        return $this->source;
    }

    /**
     * @param string|null $source
     *
     * @return $this
     */
    public function setSource(?string $source): ScriptSort
    {
        $this->source = $source;

        return $this;
    }

    /**
     * @return string
     */
    public function getOrder(): ?string
    {
        return $this->order;
    }

    /**
     * @param string|null $order
     *
     * @return $this
     */
    public function setOrder(?string $order): ScriptSort
    {
        $this->order = $order;

        return $this;
    }

    /**
     * Returns element type.
     *
     * @return string
     */
    public function getType()
    {
        return 'sort';
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        if ($this->order) {
            $this->addParameter('order', $this->order);
        }

        if ($this->nestedFilter) {
            $this->addParameter('nested', $this->nestedFilter->toArray());
        }

        $output = [
            '_script' => [
                'script' => [
                    'inline' => $this->source,
                    'lang'   => 'painless',
                ],
                'type'   => $this->type,
                'order'  => $this->order,
            ],
        ];

        return $output;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants