# Combining pinned and query rules in Elasticsearch DSL – is it possible?

**URL:** <https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265>\
**Category:** Elasticsearch\
**Created:** [April 17, 2025, 6:02pm UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265 "2025-04-17T18:02:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shell\_Dias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shell_dias/32/136785_2.png) [@Shell\_Dias](https://discuss.elastic.co/u/Shell_Dias)\
**Post date:** [April 17, 2025, 6:02pm UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265/1 "2025-04-17T18:02:53Z")

</div>

Is it possible to use both `pinned` query and `query rules` together in the same DSL request? I have a case where some documents need to be permanently pinned at the top (through rules), but there are dynamic situations where I need to temporarily add additional document IDs to be pinned at the top as well. These extra documents are not part of the original rule logic. Can these approaches be combined in a single query, or is there a recommended way to achieve this behavior?

---

<div class="post-metadata">

**Author:** ![Shell\_Dias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shell_dias/32/136785_2.png) [@Shell\_Dias](https://discuss.elastic.co/u/Shell_Dias)\
**Post date:** [April 28, 2025, 12:58pm UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265/2 "2025-04-28T12:58:46Z")

</div>

Hello?

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [April 29, 2025, 10:02am UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265/3 "2025-04-29T10:02:20Z")

</div>

Hey there!

There are a couple of options.

If you want the rule documents to come first, you can set the pinned query as your organic query. If you want the pinned documents to come first, you could consider adding an always match rule. You could do this in a different rule set if you only want it sometimes, and just add it to the rule query or retriever as needed. Rulesets are applied in the order which they are specified.

We are also working on a pinned retriever that will be able to be used alongside the rule retriever, but it’s still in progress.

Hope that helps!!

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [May 1, 2025, 1:19pm UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265/4 "2025-05-01T13:19:16Z")

</div>

An update: The pinned retriever [PR](https://github.com/elastic/elasticsearch/pull/126401) has been merged, and will be available in Serverless soon and an upcoming future stack release!

---

<div class="post-metadata">

**Author:** ![Shell\_Dias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shell_dias/32/136785_2.png) [@Shell\_Dias](https://discuss.elastic.co/u/Shell_Dias)\
**Post date:** [May 2, 2025, 2:29am UTC](https://discuss.elastic.co/t/combining-pinned-and-query-rules-in-elasticsearch-dsl-is-it-possible/377265/5 "2025-05-02T02:29:06Z")

</div>

Thkkks
