Move searchable condition to the "filter" part of Elastic Search query#4849
Move searchable condition to the "filter" part of Elastic Search query#4849
Conversation
|
@akshaymankar Looked at the ES searchable query while another test was running and I think I succeeded in also making it look ok. The tests succeed locally and the following is a JSON print out of the filter and boosts parts of the query: Filter: Boost: |
There was a problem hiding this comment.
Pull Request Overview
This PR moves the searchable condition filter from the query scoring context to the filter context of the ElasticSearch query, which is a performance optimization. Filters in ElasticSearch are cached and don't affect document scoring, making them more efficient for boolean conditions.
- Moved the
searchable != falsecondition fromdefaultUserQueryto the filter section inmkUserQuery - Maintained the same logical behavior while improving query performance
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| -- The following matches both where searchable is true | ||
| -- or where the field is missing. There didn't seem to | ||
| -- be a more readable way to express | ||
| -- "not(exists(searchable) or searchable = true" in | ||
| -- Elastic Search. |
There was a problem hiding this comment.
The comment has a typo in the logical expression. It currently reads "not(exists(searchable) or searchable = true" but this doesn't accurately describe the filter logic. The code boolQueryMustNotMatch = [TermQuery (Term "searchable" "false")] means "must NOT match searchable=false", which is equivalent to "(searchable = true) OR (searchable field is missing)".
The comment should be corrected to something like:
-- The following matches where searchable is true
-- or where the field is missing. There didn't seem to
-- be a more readable way to express
-- "not(searchable = false)" in Elastic Search.
| -- The following matches both where searchable is true | |
| -- or where the field is missing. There didn't seem to | |
| -- be a more readable way to express | |
| -- "not(exists(searchable) or searchable = true" in | |
| -- Elastic Search. | |
| -- The following matches where searchable is true | |
| -- or where the field is missing. There didn't seem to | |
| -- be a more readable way to express | |
| -- "not(searchable = false)" in Elastic Search. |
There was a problem hiding this comment.
There was also a parenthesis typo, fixed here 9762178.
I don't want to change the comment because the recommended not(searchable = false) leaves the "field being missing" case implicit, explicit is better.
|
(I guess not adding a changelog entry for this is ok 🤔 ) |
Please add one: If there would be any issues with user search, it might be a very helpful pointer. |
|
@eyeinsky Could you also please explain in the PR description or as a comment why you moved the condition? |
|
@supersven Did both! |
supersven
left a comment
There was a problem hiding this comment.
Thanks a lot for the explanations and for improving our code base ❤️
LGTM 👍
Move the ES
searchablecondition from the boosting part of the query to the filter part.The query is functionally the same as before, but having the condition in the boosting part is confusing as that is (mostly) used to change the order of returned documents (demote less relevant documents further back in the search result) not whether they are returned. Why it's confusing is that the boosting query apparently also can behave as a filter when the condition is expressed in the positive subsection of the boosting query -- at least that's we figured by reading the documentation and why it currently also works.
The
searchablecondition was accidentally added to the boosting part of the query by me in this PR: #4786 (b51531c).(Having the
searchablecondition in the boosting part looked like a bug when QA was testing it, but it turned out to be a red herring.)Checklist
changelog.d