Skip to content

Instantly share code, notes, and snippets.

@randomstatistic
Last active May 24, 2016 16:39
Show Gist options
  • Save randomstatistic/bf1f6816f4151d2491cb to your computer and use it in GitHub Desktop.
Save randomstatistic/bf1f6816f4151d2491cb to your computer and use it in GitHub Desktop.
Which OR'ed field matched a given solr doc?
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.)
@rsullivan00
Copy link

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.

@randomstatistic
Copy link
Author

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