Featured results / Pinned query

Hi,

I'm stuck with a concept of 2 sets of search results combined into 1 set (that would make paginating simple). My simplified mapping looks like this:

'properties' => [
            'featured' => ['type' => 'boolean'],
            'title' => ['type' => 'text', 'index' => true],
            'location' => ['type' => 'geo_point'],
            'language_iso1' => ['type' => 'keyword'],
]

I would like to retrieve all featured = true results within distance of 20km and have them first in my results array and then retrieve all the other results (featured and not featured) sorted by the distance (usually, it might be also sorted by relevance if user chooses so).

I have tried pinned query, which is great but when sorting the results I can only sort by the whole set (pinned and organic ones, which reshuffles the pinned ones as well). Is there any way to sort only organic results?

"from" => 0, "size" => 10,
'query' => [
                "pinned" => [
                    "ids" => $array_of_ids,
                    "organic" => [
                        "bool" => [
                            "must" => [
                                ["multi_match" => [
                                    "query" => 'Alice',
                                    "fields" => ["title"]
                                ]],
                                ["terms" => ["language_iso1" => ['en']]],
                            ],
                        ]
                    ]
                ]
            ],
            "sort" => [
                ["_geo_distance" => [
                    "location" => [
                        "lat" => 50,
                        "lon" => 50
                    ],
                    "order" => "asc",
                    "unit" => "km",
                    "distance_type" => "arc",
                ]],
            ],

Or maybe is there any other way to achieve it? Thanks in advance for help.

1 Like

Pinned query results are only designed to work with relevance as the sort order. The thinking is that they are fixing a relevance problem which is a numerical value hidden from users. If users explicitly sort by something other than relevance which is clearly visible eg price or distance then their appearance at the top of the results could be misleading.
To achieve what you need it will likely require two queries - one for the organic and one for the promoted.

1 Like

To achieve what you need it will likely require two queries - one for the organic and one for the promoted.

I thought that's going to be the way to go. Thanks a lot for the answer :slight_smile:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.