search: make search expr heuristic more strict to avoid confusion
Created by: rvantonder
Fixes https://github.com/sourcegraph/sourcegraph/issues/34018.
This is a bit involved, I go into it in the issue comment above. But basically, the heuristic that decides how to parenthesize an unparenthesized ambiguous query will become more particular about when it applies, based on the order in which terms occur. You can read the issue description for details, but basically, before:
-
We have a (desirable) heuristic that
-
repo:a b or d file:c
=> -
repo:a (b or d) file:c
=> repo:a file:c (b or d)
- Since this is a "best" guess according to tastes based on the original query
-
-
Unfortunately, the same heuristic applies when the original ambiguous query is:
-
repo:a b or file:c d
=> -
repo:a b or d file:c
(uh oh) => -
repo:a (b or d) file:c
(as before) => -
repo:a file:c (b or d)
D:
-
Because the heuristic doesn't care about the ordering of repo
or file
across the original parse tree (repo:a b) or (file:c d)
when inspecting sub expressions. This PR fixes the "bad", second case, in general. To make the heuristic now care about ordering, I added a function tho report term ordering based on their position in the query string.
I still wish I could surface this in the frontend as "your query is interpreted as such and such
". Maybe it's possible with an alert, may look into it later.
Test plan
Updated tests.