elasticsearchchewy-gem

Elasticsearch prioritize specific _ids but don't filter?


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?


Solution

  • 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
            }
          ]
        }
      }
    }