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 "girish.vignesh" <gi...@gmail.com> on 2018/09/04 10:10:35 UTC

Is it possible to escape some texts from Solr query?

One of the ID attribute which we are using to query is AND so our looks like

http://localhost:8983/solr//select?fq=id:AND&wt=json

This throws below mentioned exception

error: { metadata: [ "error-class", "org.apache.solr.common.SolrException",
"root-error-class", "org.apache.solr.parser.ParseException" ], msg:
"org.apache.solr.search.SyntaxError: Cannot parse 'id:AND': Encountered "
"AND "" at line 1

I can escape AND by using below mentioned queries
http://localhost:8983/solr//select?fq=entityId:\AND&wt=json
http://localhost:8983/solr//select?fq=entityId:%22AND%22&wt=json

Question: I do not want to handle this while making a query. Is there any
other way to handle this at Solr config level using tokenizer or some other
way. This way I do not have to handle this at multiple places.



--
Sent from: http://lucene.472066.n3.nabble.com/Solr-User-f472068.html

Re: Is it possible to escape some texts from Solr query?

Posted by Erick Erickson <er...@gmail.com>.
Alexandre's response is probably a better solution, assuming that the
field in question isn't complex, for instance:
http://localhost:8983/solr/techproducts/query?q={!term f=id}AND

Be aware that this requires that the value for the field (AND in this
case) be exactly as it appears in the index as no analysis is done at
all on the term when you use the term query parser. And you can
mix-n-match this with other parsers, something like:
.../query?q=name:Corsair AND {!term f=id}AND

Best,
Erick
On Tue, Sep 4, 2018 at 8:24 AM Alexandre Rafalovitch <ar...@gmail.com> wrote:
>
> As Erick said, you need to be looking into Query Parsers. There are
> many, depending on what search you are actually allowing your users to
> do. You probably know lucene/default, dismax and edismax, but there
> are many more: http://lucene.apache.org/solr/guide/7_4/other-parsers.html
>
> For example, you could allow a lot of flexibility but choose specific
> operators allowed with Simple Query Parser:
> http://lucene.apache.org/solr/guide/7_4/other-parsers.html#simple-query-parser
>
> Or, in the other extreme, you could allow to search against a single
> field only with Field Query Parser:
> http://lucene.apache.org/solr/guide/7_4/other-parsers.html#field-query-parser
>
> You may also benefit from dereferencing to ensure the full query is
> not overridden:
> http://lucene.apache.org/solr/guide/7_4/local-parameters-in-queries.html#parameter-dereferencing
> , possibly combined with invariants for the handler definition:
> http://lucene.apache.org/solr/guide/7_4/requesthandlers-and-searchcomponents-in-solrconfig.html#searchhandlers
>
> Regards,
>    Alex.
>
> On 4 September 2018 at 11:11, Erick Erickson <er...@gmail.com> wrote:
> > This is a query _parsing_ issue, way before a tokenizer ever gets to
> > it. The problem of course is that AND is an operator in the query
> > language, so your problem is how to distinguish it from the value of a
> > field.
> >
> > You can always quote the input for id, as in id:"AND" which you could
> > do for everything you put in that field. You'll have he same problem
> > with OR and NOT BTW.
> >
> > Best,
> > Erick
> > On Tue, Sep 4, 2018 at 3:10 AM girish.vignesh <gi...@gmail.com> wrote:
> >>
> >> One of the ID attribute which we are using to query is AND so our looks like
> >>
> >> http://localhost:8983/solr//select?fq=id:AND&wt=json
> >>
> >> This throws below mentioned exception
> >>
> >> error: { metadata: [ "error-class", "org.apache.solr.common.SolrException",
> >> "root-error-class", "org.apache.solr.parser.ParseException" ], msg:
> >> "org.apache.solr.search.SyntaxError: Cannot parse 'id:AND': Encountered "
> >> "AND "" at line 1
> >>
> >> I can escape AND by using below mentioned queries
> >> http://localhost:8983/solr//select?fq=entityId:\AND&wt=json
> >> http://localhost:8983/solr//select?fq=entityId:%22AND%22&wt=json
> >>
> >> Question: I do not want to handle this while making a query. Is there any
> >> other way to handle this at Solr config level using tokenizer or some other
> >> way. This way I do not have to handle this at multiple places.
> >>
> >>
> >>
> >> --
> >> Sent from: http://lucene.472066.n3.nabble.com/Solr-User-f472068.html

Re: Is it possible to escape some texts from Solr query?

Posted by Alexandre Rafalovitch <ar...@gmail.com>.
As Erick said, you need to be looking into Query Parsers. There are
many, depending on what search you are actually allowing your users to
do. You probably know lucene/default, dismax and edismax, but there
are many more: http://lucene.apache.org/solr/guide/7_4/other-parsers.html

For example, you could allow a lot of flexibility but choose specific
operators allowed with Simple Query Parser:
http://lucene.apache.org/solr/guide/7_4/other-parsers.html#simple-query-parser

Or, in the other extreme, you could allow to search against a single
field only with Field Query Parser:
http://lucene.apache.org/solr/guide/7_4/other-parsers.html#field-query-parser

You may also benefit from dereferencing to ensure the full query is
not overridden:
http://lucene.apache.org/solr/guide/7_4/local-parameters-in-queries.html#parameter-dereferencing
, possibly combined with invariants for the handler definition:
http://lucene.apache.org/solr/guide/7_4/requesthandlers-and-searchcomponents-in-solrconfig.html#searchhandlers

Regards,
   Alex.

On 4 September 2018 at 11:11, Erick Erickson <er...@gmail.com> wrote:
> This is a query _parsing_ issue, way before a tokenizer ever gets to
> it. The problem of course is that AND is an operator in the query
> language, so your problem is how to distinguish it from the value of a
> field.
>
> You can always quote the input for id, as in id:"AND" which you could
> do for everything you put in that field. You'll have he same problem
> with OR and NOT BTW.
>
> Best,
> Erick
> On Tue, Sep 4, 2018 at 3:10 AM girish.vignesh <gi...@gmail.com> wrote:
>>
>> One of the ID attribute which we are using to query is AND so our looks like
>>
>> http://localhost:8983/solr//select?fq=id:AND&wt=json
>>
>> This throws below mentioned exception
>>
>> error: { metadata: [ "error-class", "org.apache.solr.common.SolrException",
>> "root-error-class", "org.apache.solr.parser.ParseException" ], msg:
>> "org.apache.solr.search.SyntaxError: Cannot parse 'id:AND': Encountered "
>> "AND "" at line 1
>>
>> I can escape AND by using below mentioned queries
>> http://localhost:8983/solr//select?fq=entityId:\AND&wt=json
>> http://localhost:8983/solr//select?fq=entityId:%22AND%22&wt=json
>>
>> Question: I do not want to handle this while making a query. Is there any
>> other way to handle this at Solr config level using tokenizer or some other
>> way. This way I do not have to handle this at multiple places.
>>
>>
>>
>> --
>> Sent from: http://lucene.472066.n3.nabble.com/Solr-User-f472068.html

Re: Is it possible to escape some texts from Solr query?

Posted by Erick Erickson <er...@gmail.com>.
This is a query _parsing_ issue, way before a tokenizer ever gets to
it. The problem of course is that AND is an operator in the query
language, so your problem is how to distinguish it from the value of a
field.

You can always quote the input for id, as in id:"AND" which you could
do for everything you put in that field. You'll have he same problem
with OR and NOT BTW.

Best,
Erick
On Tue, Sep 4, 2018 at 3:10 AM girish.vignesh <gi...@gmail.com> wrote:
>
> One of the ID attribute which we are using to query is AND so our looks like
>
> http://localhost:8983/solr//select?fq=id:AND&wt=json
>
> This throws below mentioned exception
>
> error: { metadata: [ "error-class", "org.apache.solr.common.SolrException",
> "root-error-class", "org.apache.solr.parser.ParseException" ], msg:
> "org.apache.solr.search.SyntaxError: Cannot parse 'id:AND': Encountered "
> "AND "" at line 1
>
> I can escape AND by using below mentioned queries
> http://localhost:8983/solr//select?fq=entityId:\AND&wt=json
> http://localhost:8983/solr//select?fq=entityId:%22AND%22&wt=json
>
> Question: I do not want to handle this while making a query. Is there any
> other way to handle this at Solr config level using tokenizer or some other
> way. This way I do not have to handle this at multiple places.
>
>
>
> --
> Sent from: http://lucene.472066.n3.nabble.com/Solr-User-f472068.html