1
votes

My solr implementation has query something like this

q=(field1:ipod and field2:black) OR (field3:'apple computers') OR (field4:working)

There are essentially 3 conditions here separated by 'OR'. When i run this query i get the results. In the results i would like to see which of the 3 conditions were met. For example in the results in addition to the returned data (custom fields), specifically I would like to have cond1=true, cond2=true, cond3=false for each of the document returned.

Assume that there are no analysis will be done on these fields(1 to 4). All fields are string. Solr 4.6.1. Distributed.

Solutions tried (but without satisfactory results):

  1. debugquery=true seems to give too much verbose information (explain field), that i am not interested in, i am looking for simple fields to be added. i can get these debug xml-ized for parsing, but complicated for normal processing.
  2. group by (group.query) option would help by providing the conditions in the group.query so that we get this grouped by conditions, not the best option for me but just tried it. But it does not seem to work in distributed mode (SOLR-5046)
  3. Adding field function query, not able to do simple field to string equality check like _cond1_:if(field1=='ipod' and field2=='black'). There is no such equality check possible in the function query i guess.

Any other options solutions here? should i write custom similarity or custom Doc transformer here?

1

1 Answers

0
votes

Taking hint from https://cwiki.apache.org/confluence/display/solr/Function+Queries

you could use the following fl param in your query:

fl=*,firstmatch:if(and(termfreq(field1,'ipod'),termfreq(field2,'black')),1,0),
secondmatch:if(termfreq(field3,'apple computer')),1,0),
thirdmatch:if(termfreq(field4,'working'),1,0)

(the new lines are only for clarity)

That should give you three fields with 1 or 0 for matches.

Basic idea is you can use function queries in fl field to get specific values. The names firstmatch, secondmatch and thirdmatch are simply aliases.

BTW, since you mention all your fields are strings, you will need exact matches everywhere.