You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Sergio Stateri <st...@gmail.com> on 2013/09/02 13:53:05 UTC

setQuery in SolrJ

hi,

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
at
org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:424)
at
org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)
at
org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at
com.teste.SearchRoteirosFromCollection.extrairEApresentarResultados(SearchRoteirosFromCollection.java:65)
...


But If I search one a word od put * between two words, the search works
fine.


Thanks in advance,


-- 
Sergio Stateri Jr.
stateri@gmail.com

Re: setQuery in SolrJ

Posted by François Schiettecatte <fs...@gmail.com>.
Shouldn't the search be more like this if you are searching in the 'descricaoRoteiro' field:

	descricaoRoteiro:(BPS 8D BEACH*)

or in your example you have a space in between 'descricaoRoteiro' and 'BPS':

	descricaoRoteiro:BPS 8D BEACH*

François


On Sep 2, 2013, at 8:08 AM, Dmitry Kan <so...@gmail.com> wrote:

> Hi,
> 
> What's your default query field in solrconfig.xml?
> 
> <requestHandler name="/select" class="solr.SearchHandler">
> <str name="df">[WHAT IS IN HERE?]</str>
> 
> I think what's happening is that the query:
> 
> (descricaoRoteiro: BPS 8D BEACH*)
> 
> gets interpreted as:
> 
> descricaoRoteiro:BPS (8D BEACH*)
> 
> then on the (8D BEACH*) a default field name is applied.
> 
> You can use debugQuery parameter to see how the query was parsed.
> 
> HTH,
> Dmitry
> 
> 
> On Mon, Sep 2, 2013 at 2:53 PM, Sergio Stateri <st...@gmail.com> wrote:
> 
>> hi,
>> 
>> 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
>> at
>> 
>> org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:424)
>> at
>> 
>> org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)
>> at
>> 
>> org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
>> at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
>> at
>> 
>> com.teste.SearchRoteirosFromCollection.extrairEApresentarResultados(SearchRoteirosFromCollection.java:65)
>> ...
>> 
>> 
>> But If I search one a word od put * between two words, the search works
>> fine.
>> 
>> 
>> Thanks in advance,
>> 
>> 
>> --
>> Sergio Stateri Jr.
>> stateri@gmail.com
>> 


Re: setQuery in SolrJ

Posted by Dmitry Kan <so...@gmail.com>.
Hi,

What's your default query field in solrconfig.xml?

<requestHandler name="/select" class="solr.SearchHandler">
<str name="df">[WHAT IS IN HERE?]</str>

I think what's happening is that the query:

(descricaoRoteiro: BPS 8D BEACH*)

gets interpreted as:

descricaoRoteiro:BPS (8D BEACH*)

then on the (8D BEACH*) a default field name is applied.

You can use debugQuery parameter to see how the query was parsed.

HTH,
Dmitry


On Mon, Sep 2, 2013 at 2:53 PM, Sergio Stateri <st...@gmail.com> wrote:

> hi,
>
> 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
> at
>
> org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:424)
> at
>
> org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)
> at
>
> org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
> at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
> at
>
> com.teste.SearchRoteirosFromCollection.extrairEApresentarResultados(SearchRoteirosFromCollection.java:65)
> ...
>
>
> But If I search one a word od put * between two words, the search works
> fine.
>
>
> Thanks in advance,
>
>
> --
> Sergio Stateri Jr.
> stateri@gmail.com
>

Re: setQuery in SolrJ

Posted by Shawn Heisey <so...@elyograg.org>.
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