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 Alok Bhandari <al...@gmail.com> on 2016/01/13 12:21:53 UTC

How to achieve exact string match query which includes spaces and quotes

Hello ,

I am using Solr 5.2.

I have a field defined as "string" field type. It have some values in it
like 

DOC-1 => abc ".. I am " not ? test
DOC-2 => abc ".. 
This is the single string , I want to query all documents which exactly
match this string i.e. it should return me only DOC-1 when I query for 'abc
".. I am " not ? test' and it should return me only DOC-2 if I query for
'abc"...'.

Please let me know how I can achieve this , which defType I should use.

Thanks.



--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Scott Stults <ss...@opensourceconnections.com>.
This might be a good case for the Raw query parser (I haven't used it
myself).

https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-RawQueryParser


k/r,
Scott

On Wed, Jan 13, 2016 at 12:05 PM, Erick Erickson <er...@gmail.com>
wrote:

> what _does_ matter is getting all that through the parser which means
> you have to enclose things in quotes and escape them.
>
> For instance, consider this query  stringFIeld:abc "i am not"
>
> this will get parsed as
> stringField:abc defaultTextField:"i am not".
>
> To get around this you need to make sure the entire search gets
> through the parser as a _single_ token by enclosing in quotes. But
> then of course you have confusion because you have quotes in your
> search term so you need to escape those, something like
> stringField:"abc \"i am not\""
>
> Here's a list for Lucene 5
>
> https://lucene.apache.org/core/5_1_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Escaping_Special_Characters
>
> Best,
> Erick
>
> On Wed, Jan 13, 2016 at 3:39 AM, Binoy Dalal <bi...@gmail.com>
> wrote:
> > No.
> >
> > On Wed, 13 Jan 2016, 16:58 Alok Bhandari <
> alokomprakashbhandari@gmail.com>
> > wrote:
> >
> >> Hi Binoy thanks.
> >>
> >> But does it matter which query-parser I use , shall I use "lucene"
> parser
> >> or
> >> "edismax" parser.
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4250405.html
> >> Sent from the Solr - User mailing list archive at Nabble.com.
> >>
> > --
> > Regards,
> > Binoy Dalal
>



-- 
Scott Stults | Founder & Solutions Architect | OpenSource Connections, LLC
| 434.409.2780
http://www.opensourceconnections.com

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Alok Bhandari <al...@gmail.com>.
Hello Binoy ,

I found that if I am using a StringField and index it using java
code/solr-admin it adds a \ before " , 
i.e. lest say I have string ==> test " , then it gets indexed as test \".
For all other special chars it does not do anything , so the trick which
worked for me is 
while searching I replace " with \" using this code text.replaceAll("\"",
"\\\\\"").

This makes sure that it matches the intended string , also if I use
ClientUtils then I need to make changes in indexing code also to escape
special chars as using ClientUtils to make exact search work. 

So I found just replacing " with \" does the trick for me.

Thanks.



--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4252522.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Binoy Dalal <bi...@gmail.com>.
You should use the client utils method.
That way you're set even if you encounter special characters like + or ()
etc.

On Thu, 21 Jan 2016, 13:40 Alok Bhandari <al...@gmail.com>
wrote:

> Thanks Erick for your reply. Because of some medical reason I was out of
> office for a week.
>
>  ClientUtils.escapeQueryChars method from solrj client should be used? or
> you think its better to escape only quote " character.
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4252217.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>
-- 
Regards,
Binoy Dalal

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Alok Bhandari <al...@gmail.com>.
Thanks Erick for your reply. Because of some medical reason I was out of
office for a week.

 ClientUtils.escapeQueryChars method from solrj client should be used? or
you think its better to escape only quote " character.



--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4252217.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Erick Erickson <er...@gmail.com>.
what _does_ matter is getting all that through the parser which means
you have to enclose things in quotes and escape them.

For instance, consider this query  stringFIeld:abc "i am not"

this will get parsed as
stringField:abc defaultTextField:"i am not".

To get around this you need to make sure the entire search gets
through the parser as a _single_ token by enclosing in quotes. But
then of course you have confusion because you have quotes in your
search term so you need to escape those, something like
stringField:"abc \"i am not\""

Here's a list for Lucene 5
https://lucene.apache.org/core/5_1_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Escaping_Special_Characters

Best,
Erick

On Wed, Jan 13, 2016 at 3:39 AM, Binoy Dalal <bi...@gmail.com> wrote:
> No.
>
> On Wed, 13 Jan 2016, 16:58 Alok Bhandari <al...@gmail.com>
> wrote:
>
>> Hi Binoy thanks.
>>
>> But does it matter which query-parser I use , shall I use "lucene" parser
>> or
>> "edismax" parser.
>>
>>
>>
>> --
>> View this message in context:
>> http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4250405.html
>> Sent from the Solr - User mailing list archive at Nabble.com.
>>
> --
> Regards,
> Binoy Dalal

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Binoy Dalal <bi...@gmail.com>.
No.

On Wed, 13 Jan 2016, 16:58 Alok Bhandari <al...@gmail.com>
wrote:

> Hi Binoy thanks.
>
> But does it matter which query-parser I use , shall I use "lucene" parser
> or
> "edismax" parser.
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4250405.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>
-- 
Regards,
Binoy Dalal

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Alok Bhandari <al...@gmail.com>.
Hi Binoy thanks.

But does it matter which query-parser I use , shall I use "lucene" parser or
"edismax" parser.



--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402p4250405.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: How to achieve exact string match query which includes spaces and quotes

Posted by Binoy Dalal <bi...@gmail.com>.
Just query the string field and nothing else.
String fields only return on exact match.

On Wed, 13 Jan 2016, 16:52 Alok Bhandari <al...@gmail.com>
wrote:

> Hello ,
>
> I am using Solr 5.2.
>
> I have a field defined as "string" field type. It have some values in it
> like
>
> DOC-1 => abc ".. I am " not ? test
> DOC-2 => abc "..
> This is the single string , I want to query all documents which exactly
> match this string i.e. it should return me only DOC-1 when I query for 'abc
> ".. I am " not ? test' and it should return me only DOC-2 if I query for
> 'abc"...'.
>
> Please let me know how I can achieve this , which defType I should use.
>
> Thanks.
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/How-to-achieve-exact-string-match-query-which-includes-spaces-and-quotes-tp4250402.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>
-- 
Regards,
Binoy Dalal