0

I'm trying to sort my query in elasticsearch where the query will prioritize documents with specific _ids to appear first but it won't filter the entire query based on the _ids it's just prioritizing them.

Here's an example of what I've tried as an attempt:

{"query":{"constant_score":{"filter":{"terms":{"_id":[2,3,4]}},"boost":2}}}

So the above would be included along with other queries however the query just returns the exact matches and not the rest of the results.

Any ideas as to how this can be done so that it just prioritizes the documents with the ids but doesn't filter the entire query?

4

2 回答 2

4

Try this (and instead of that match_all() there you can use a query to actually filter the results):

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "terms": {
              "_id": [
                2,
                3,
                4
              ]
            }
          },
          "weight": 2
        }
      ]
    }
  }
}
于 2016-11-24T06:12:54.200 回答
0

If you need to return in exact order as you need go with

 "sort": [
    {
      "_script": {
        "script": "doc['id'] != null ? sortOrder.indexOf(doc['id'].value.toInteger()) : 0",
        "type": "number",
        "params": {
          "sortOrder": [
            2,3,4
          ]
        },
        "order": "desc"
      }
    },
    "_score"
  ]

P.S. As @Val mentioned wityh _id this will not work, so you would need to store id field as separate.

If you need move documents to top look to function_score

于 2016-11-24T03:13:21.063 回答