Skip to content

Fix: Allow non-score sorts in pinned retriever sub-retrievers #128323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mridula-s109
Copy link
Contributor

Summary

Fixes an issue where the pinned retriever was too restrictive with sort criteria in sub-retrievers, only allowing score-based sorting. This change allows custom sort criteria while maintaining the pinned document functionality.

Changes

  • Modified validateSort in PinnedRetrieverBuilder to only check the first sort criterion
  • Added test case to verify pinned retriever works with custom sort criteria
  • Fixed code style issues

Testing

  • All unit tests pass
  • YAML REST tests pass
  • Manually verified pinned retriever works with custom sorts

Notes

  • The pinned document will still appear first in results
  • Additional sort criteria are now allowed after the first score-based sort

Test Results

Tested with the following query:

GET books/_search
{
  "retriever": {
    "pinned": {
      "retriever": {
        "standard": {
          "query": {
            "multi_match": {
              "query": "alien"
            }
          },
          "sort": [
            { "_score": "desc" },
            { "publication_year": "desc" }
          ]
        }
      },
      "docs": [
        { "_id": "1", "_index": "books" }
      ]
    }
  }
}

Response:

{
  "took": 3,
  "hits": {
    "total": { "value": 2, "relation": "eq" },
    "max_score": 1.7014122e+38,
    "hits": [
      {
        "_id": "1",
        "_score": 1.7014122e+38,
        "_source": {
          "title": "Neuromancer",
          "author": "William Gibson",
          "publication_year": 1984,
          "genre": "science_fiction"
        }
      },
      {
        "_id": "3",
        "_score": 0.7590337,
        "_source": {
          "title": "Alien: River of Pain",
          "author": "Tim Lebbon",
          "publication_year": 2014,
          "genre": "science_fiction"
        }
      }
    ]
  }
}

@mridula-s109 mridula-s109 requested review from kderusso, a team and Copilot May 22, 2025 15:46
@mridula-s109 mridula-s109 added auto-backport Automatically create backport pull requests when merged :SearchOrg/Relevance Label for the Search (solution/org) Relevance team v8.19.0 v9.1.0 labels May 22, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR relaxes the pinned retriever's sort criteria by allowing additional non-score sort fields after ensuring that the first sort criterion remains score-based. Key changes include modifying the sort validation logic in PinnedRetrieverBuilder and updating the associated tests to confirm the behavior.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java Updated sort validation to only check that the first sort is score-based
x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java Adjusted tests to account for the new validated behavior and added assertions to verify sort order

mridula-s109 and others added 2 commits May 22, 2025 16:47
…earch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java

Co-authored-by: Copilot <[email protected]>
…earch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Co-authored-by: Copilot <[email protected]>
@mridula-s109 mridula-s109 enabled auto-merge (squash) May 22, 2025 15:48
@elasticsearchmachine elasticsearchmachine added Team:SearchOrg Meta label for the Search Org (Enterprise Search) Team:Search - Relevance The Search organization Search Relevance team labels May 22, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/search-eng (Team:SearchOrg)

@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/search-relevance (Team:Search - Relevance)

@elasticsearchmachine
Copy link
Collaborator

Hi @mridula-s109, I've created a changelog YAML for you.

Copy link
Member

@kderusso kderusso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM.

This is a good bugfix to have, but note that this doesn't get at what we were hoping for - which was the ability to pin a single document at the top of an otherwise sorted list of results (think, pinning a result at the top of a result set that's otherwise sorted by publication date). Looking into it it's a larger issue to support that because it's a fundamental limitation of the pinned query DSL, which the retriever defers to. More advanced sub-sorting options would be a (non trivial) enhancement request CC: @serenachou

@mridula-s109 mridula-s109 requested a review from Copilot May 27, 2025 16:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@mridula-s109 mridula-s109 requested a review from Copilot May 28, 2025 14:51
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the pinned retriever to only enforce score-based sorting on the first criterion, allowing additional custom sorts thereafter.

  • Only the first sort is validated to be by _score
  • Test added to verify multiple-sort support
  • Changelog entry for the bug fix

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java Simplified validateSort to only check the first sort criterion
x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java Updated tests to allow custom secondary sorts and assert their presence
docs/changelog/128323.yaml Added changelog entry for non-score secondary sort support
Comments suppressed due to low confidence (1)

x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java:223

  • Add a test case where the first sort is the pinned-docs sort field (ShardDocSortField.NAME) followed by _score to ensure that validateSort allows the injected shard-doc sort.
e = expectThrows(IllegalArgumentException.class, () -> builder.finalizeSourceBuilder(fieldFirstSource));

@mridula-s109 mridula-s109 merged commit cc461af into elastic:main May 30, 2025
18 checks passed
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.19

mridula-s109 added a commit to mridula-s109/elasticsearch that referenced this pull request May 30, 2025
…c#128323)

* Fix scoring and sort handling in pinned retriever

* Remove books.es from version control and add to .gitignore

* Remove books.es from version control and add to .gitignore

* Remove books.es entries from .gitignore

* fixed the mess

* Update x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java

Co-authored-by: Copilot <[email protected]>

* Update x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
joshua-adams-1 pushed a commit to joshua-adams-1/elasticsearch that referenced this pull request Jun 3, 2025
…c#128323)

* Fix scoring and sort handling in pinned retriever

* Remove books.es from version control and add to .gitignore

* Remove books.es from version control and add to .gitignore

* Remove books.es entries from .gitignore

* fixed the mess

* Update x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java

Co-authored-by: Copilot <[email protected]>

* Update x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
Samiul-TheSoccerFan pushed a commit to Samiul-TheSoccerFan/elasticsearch that referenced this pull request Jun 5, 2025
…c#128323)

* Fix scoring and sort handling in pinned retriever

* Remove books.es from version control and add to .gitignore

* Remove books.es from version control and add to .gitignore

* Remove books.es entries from .gitignore

* fixed the mess

* Update x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilderTests.java

Co-authored-by: Copilot <[email protected]>

* Update x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/retriever/PinnedRetrieverBuilder.java

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged >bug :SearchOrg/Relevance Label for the Search (solution/org) Relevance team Team:Search - Relevance The Search organization Search Relevance team Team:SearchOrg Meta label for the Search Org (Enterprise Search) v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants