Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to retrieve the field name(s) that matched the query in Solr?

Tags:

solr

solrnet

I would like to dynamically show to the user which field matched the query that was sent to Solr. For example, if I had a document

document
  field1: "yay"
  field2: "nay"
  dynamic_field_hurr_*:
    one: "yay"
    two: "nay"

and i queried for "yay", would it be possible for me to know that yay was found in field1 and dynamic_field_hurr_one?

I feel like I've been through the whole documentation, and thought I should use highlighting for this, but I can't get it to work on dynamic fields. On normal fields it works fine!

A little background: I'm using Solr.Net and in the class I map to my document I have a IDictionary<string, string> to dynamically add additional information. After some reading up, I figured dictionaries mapped to dynamicfields, and it works perfectly, except for highlighting.

I also tried copying all data from my dynamic field into a text field, but I don't think there's a way to copy the "actual field name"? I can only get Solr to copy the value, which I guess makes sense.

Any ideas?

like image 827
cwap Avatar asked Nov 27 '13 20:11

cwap


2 Answers

just for completeness, there is another way to get this info without using highlighting (highlighting imposes some conditions such as forcing stored fields)

  1. add debugQuery=true to your query
  2. get /response/debug/explain info
  3. parse it, to find out in what field it matched. Using debug.explain.structured=true changes the format of the element, it might be easier to parse
like image 40
Persimmonium Avatar answered Sep 19 '22 18:09

Persimmonium


There is an indirect way of doing this using a very interesting tool , hit highlighting.

Do this with default solr schema to understand solution:

http://localhost:8983/solr/collection1/select?q=iPod&wt=xml&indent=true&hl=on&hl.fl=name,description

Look at :

<lst name="highlighting">
<lst name="IW-02">
<arr name="name">
<str><em>iPod</em> & <em>iPod</em> Mini USB 2.0 Cable</str>
</arr>
</lst>
<lst name="F8V7067-APL-KIT">
<arr name="name">
<str>Belkin Mobile Power Cord for <em>iPod</em> w/ Dock</str>
</arr>
</lst>
<lst name="MA147LL/A">
<arr name="name">
<str>
Apple 60 GB <em>iPod</em> with Video Playback Black
</str>
</arr>
</lst>
</lst>

So , here you have the is the field that matched. You can choose to ignore rest of the actual highlighting part and use only the field names that matched for your purpose.

So you would ask for something like this in your case &hl=on&hl.fl=dynamic_field_hurr_one,dynamic_field_hurr_two

like image 172
Arun Avatar answered Sep 21 '22 18:09

Arun