Skip to content

Instantly share code, notes, and snippets.

@randomstatistic
Last active May 24, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

This might have some promise, although it's super ugly, as you said.

I tried

&q=lastName:James&fl=lastName,score,lastNameMatch:query({!edismax qf=lastName v=James},0)

And get documents like

        <doc>
            <arr name="lastName">
                <str>JAMES</str>
            </arr>
            <float name="score">7.838296</float>
            <float name="lastNameMatch">7.838296</float>
        </doc>

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:

&q=lastName:awefawef dobYear:1950&fl=lastName,dobYear,score,lastNameMatch:query({!edismax qf=lastName v=awefawef},0),dobYearMatch:query({!edismax qf=dobYear v=1950},0)

Result:

        <doc>
            <int name="dobYear">1950</int>
            <arr name="lastName">
                <str>DAY</str>
            </arr>
            <float name="score">1.1346848</float>
            <float name="dobYearMatch">5.9483047</float>
        </doc>

@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