1

I am using elastica and below is my query :

    $query = new Query();

    $query->setSize(5);

    $qb = new \Elastica\Query\Ids();

    $qb->addId("id_5");
    $qb->addId("id_3");
    $qb->addId("id_4");
    $qb->addId("id_1");
    $qb->addId("id_2");

    return $query->setQuery($qb)

I want the result return in the same order as what I passed in for example in this case it will be "id_5, id_3, id_4, id_1, id_2"

however what i get is the sorting is not the same as what I wanted

1
  • Do you have id as part of document? Commented Mar 3, 2016 at 11:08

1 Answer 1

1

I see this as two solution 1. if you have id as part of document then you can do

POST indexName/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": "sortOrder.indexOf(doc['Id'].value)",
        "params": {
          "sortOrder": [
            "AVMX9sHTyNVr4SjF3oRt",
            "AVMTc1fSyNVr4SjF3WpF",
            "AVMYuGLOyNVr4SjF3rKm"
          ]
        },
        "order": "asc"
      }
    }
  ],
  "query": {
    "ids": {
      "values": [
        "AVMX9sHTyNVr4SjF3oRt",
            "AVMTc1fSyNVr4SjF3WpF",
            "AVMYuGLOyNVr4SjF3rKm"
      ]
    }
  }
}

If not then you need to update mapping for your type to be

{
  "mappings": {
    "YOUR_TYPE": {
      "_id": {
        "index": "not_analyzed"
      }
    }
  }
}

Then you can use "script": "sortOrder.indexOf(doc['_id'].value)"

Sign up to request clarification or add additional context in comments.

3 Comments

how can i do this with elastica?
i had this error failed to run inline script [sortOrder.indexOf(doc["Id"].value)] using lang [groovy]
Can you post example of any of you documetns in index?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.