SolrJ solr query for boolean params getting undefined field exception

769 views Asked by At

Hi i am using a slorj api to query solr indexes. But i am getting some exception when i add following query to SolrQuery object.

When i run following query in browser it is working fine http://localhost:8983/solr/hellosolr/select?q=fkey:book+OR+bookstore+AND+whword:what&fl=fanswer it is working fine but when i run the same query using SolrQuery i am getting following exception

SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "fkey:book+OR+bookstore+AND+whword:what");
solrQuery.set("fl", "fanswer");

Exception-

org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/hellosolr: org.apache.solr.search.SyntaxError: Cannot parse 'fkey:book+OR+bookstore+AND+whword:what': Encountered " ":" ": "" at line 1, column 39.
Was expecting one of:
    <EOF> 
    <AND> ...
    <OR> ...
    <NOT> ...
    "+" ...

Please tell me how i can write above html query using SolrQuery java api.

1

There are 1 answers

1
TMBT On

Your error message is a syntax error. Take out the + signs.

For the exception, try this query instead:

(fkey:book OR fkey:bookstore) AND whword:what

or you can write it like this (note the parentheses):

(fkey:book bookstore) AND whword:what

If you have OR defined as your default, then Solr will insert it between book and bookstore for you. Otherwise it will do an AND. I believe OR is the default. Check your solrconfig.xml to be sure.

If you don't specify a field in front of a search term, Solr will use the default field (it's text in my version, for example). If Solr can't find it, you'll get the undefined field [name] error.

Now, why your bad query is working from the admin panel, I don't know, but this should solve your SolrJ problem.

Assuming you're using the /select handler, you can go to solrconfig.xml and change the default:

<requestHandler name="/select" class="solr.SearchHandler">
    <lst name="defaults">
    <str name="echoParams">explicit</str>
    <int name="rows">10</int>
    <str name="df">blah</str>
</lst>