On 9/2/2013 5:53 AM, Sergio Stateri wrote:
> How can I looking for an exact phrase in query.setQuery method (SolrJ)?
> 
> Like this:
> 
> SolrQuery query = new SolrQuery();
> query.setQuery( "(descricaoRoteiro: BPS 8D BEACH*)" );
> query.set("start", "200");
> query.set("rows", "10");
> query.addField("descricaoRoteiro");
> QueryResponse rsp = server.query( query );
> 
> 
> When I run this code, the following exception is thrown:
> 
> Exception in thread "main"
> org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: no
> field name specified in query and no default specified via 'df' param

Your question (how to do a phrase search) has nothing to do with the
exception you are seeing.

Dmitry asked what's in your df parameter.  The exception is saying that
you don't *have* a df parameter.  The df parameter means "default
field."  The "8D" and "BEACH*" parts of your query have no field name,
so Solr must figure out field what to use.  The df parameter takes care
of that.

There is a default search field setting in schema.xml that is deprecated
in Solr 4.x.  It still works, but it is not recommended, because it will
be removed in 5.0.

Phrase queries are accomplished with double quotes.  Here's my best
guess about what to do for your query.  This will ensure that the search
terms all exist in the index and that they appear in that specific
order.  If you need to allow for phrase slop, put ~n between those last
two quotes, where n is a number indicating how much slop to allow:

query.setQuery("descricaoRoteiro:\"BPS 8D BEACH*\"");

Because Java uses double quotes for string delimiters, I have used the
backslash to escape the quotes that I want to be in the string.

Is the * after beach meant to be a literal * or a wildcard?  You can't
mix a phrase query with wildcards - putting the * inside the quotes will
cause Solr to interperet it as a literal part of your query.

If you need to search for the wildcard, you probably want the following.
 The problem with this is that it will make no guarantees about the
order in which the search terms appear, just that they all exist:

query.setQuery("descricaoRoteiro:(BPS 8D BEACH*)");
query.set("q.op","AND");

The addField method that you are using adds that value to the "fl"
parameter, which controls the field list that Solr returns in the
result.  If you want all stored fields in your result, you'll need to
remove that line.

Here's some sample code similar to yours using the phrase query option
and removing the addField:

SolrQuery query = new SolrQuery();
query.setQuery("descricaoRoteiro:\"BPS 8D BEACH*\"");
query.setRows(10);
query.setStart(200);
QueryResponse rsp = server.query(query);

long numFound = rsp.getResults().getNumFound();
System.out.println("numFound: " + numFound);

String desc;
for (SolrDocument d : rsp.getResults()) {
  desc = (String) d.get("descricaoRoteiro");
  System.out.println("Desc: " + desc);
}

Thanks,
Shawn

Reply via email to