0

I want my search tool to have a similar behaviour to Google Search when all of the elements entered by the user are excluded (eg.: user input is -obama). In those cases, Google returns an empty result. In my current code, my program just makes an empty Solr query, which causes error in Solr.

I know that you can enter *:* to get all the results, but what should I fill in my Solr query so that Solr will return an empty search result?

EDIT:

Just to make it clearer, the thing is that when I have something like -obama, I want Solr to return an empty search result. If you google -obama, that's what you get, but if you put -obama on Solr, it seems that the result is everything (all the documents), except for the ones that have "obama"

2 Answers2

0

If your query looks like below, it returns empty result for me:

solr/select/?q=-obama&qf=description&defType=dismax

It is trying to run a negative query on no search query, hence no results returned.

Here is response I get:

<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">0</int>
<lst name="params">
<str name="q">-obama</str>
<str name="qf">description</str>
<str name="defType">dismax</str>
</lst>
</lst>
<result name="response" numFound="0" start="0"/>
</response>
Aujasvi Chitkara
  • 939
  • 1
  • 5
  • 13
0

You shouldn't query solr when there is no term being looked for (and I seriously doubt google looks over it's searchable indexes when a search term is empty). This logic should be built into whatever mechanism you use to parse the user supplied query terms before constructing the solr query. Lets say the user's input is represented as a simple string where each word is treated as a unique query term. You would want to split the string on spaces into an array of strings, map over the array and remove strings prefixed by "-", and then construct the query terms from what remains in the array. If flattening the array yields an empty string, return an empty array instead of querying solr at all.

Jake
  • 29
  • 3
  • Yes, I made an edit in my question to make it clearer – Lawrence Lin Murata Aug 08 '14 at 18:31
  • If you parse out all terms prefixed with "-" before running your query, even if the q parameter would be set to the empty string, you will get the results you want. What request handler are you using? if it is select, this will work if you use a url like: /select?wt=json&indent=true&q= – Jake Aug 09 '14 at 21:53