Last active
May 24, 2016 16:39
-
-
Save randomstatistic/bf1f6816f4151d2491cb to your computer and use it in GitHub Desktop.
Which OR'ed field matched a given solr doc?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The idea is to add calculated fields to the “fl” of your query that match the components of your query. | |
Solr 4.0+ allows calculated functions in your field list using a <field name>:<function> syntax, and one of the available functions is “query”. | |
This seems like a vaguely unpleasant way to determine which clauses matched, but I could see it working. I think it would go something like: | |
q=colA:foo OR colB:bar&fl=*,score,matchedColA:query({!standard q=“colA:foo”},0),matchedColB:query({!standard q=“colB:bar”},0) | |
Presumably the field matchedColA would be non-zero if colA:foo matched on that document, and matchedColB would be non-zero | |
if colB:bar matched. | |
(I’m actually not sure if “standard” works as the name of the default query parser, but whatever, the idea is that it needs to match the relevant bit of your query.) |
Do you see any reasons why I shouldn't rely on this? It doesn't seem to slow down my queries any noticeable amount in manual testing.
I recall we chatted about this in IRC, but in the interests of documentation:
The only concern I recall expressing was the extra work running the function queries, but I believe that extra work is only performed on returned documents, so it should be minimal unless you're setting rows= to some very high number. (In which case, you're probably going to have issues regardless)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This might have some promise, although it's super ugly, as you said.
I tried
And get documents like
For some reason the sub-score is put into the lastNameMatch field, which was accidental, but might be a good way to get a nonzero value.
And an example with two OR fields, intentionally with no last name matches:
Result: