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 tedsolr <ts...@sciquest.com> on 2014/10/01 23:32:55 UTC

Exact match on string field with special characters

I am trying to do SQL like aggregation (GROUP BY) with solr faceting. So I
use string fields for faceting - to try to get an exact match. However, it
seems like to run a facet query I have to surround the value with double
quotes. That poses issues when the field value is

green "bath" towels

-or-

red \cars

Those two special characters must be transformed somehow on indexing so I
can create the query:
(java)
...
String fg = fieldName + ":\"" + fieldValue + "\"";
query.addFacetField(fq);
...

Is there a way to request an exact match search without having to resort to
the quotes? I could possibly convert spaces to underscores at index time,
but I'd like to avoid munging that data because I'm using the string field
for display too! That saves time/searches when aggregating against 10 - 15
fields which takes a whole lot of facet searches to begin with.

Using Solr 4.9 




--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209.html
Sent from the Solr - User mailing list archive at Nabble.com.

RE: Exact match on string field with special characters

Posted by Michael Ryan <mr...@moreover.com>.
When you call addFacetField, the parameter you pass it should just be the fieldName. The fieldValue shouldn't come into play at all (unless I'm misunderstanding what you're trying to do).

If you ever do need to escape a value for a query, you can use org.apache.solr.client.solrj.util.ClientUtils.escapeQueryChars().

-Michael

-----Original Message-----
From: tedsolr [mailto:tsmith@sciquest.com] 
Sent: Wednesday, October 01, 2014 5:33 PM
To: solr-user@lucene.apache.org
Subject: Exact match on string field with special characters

I am trying to do SQL like aggregation (GROUP BY) with solr faceting. So I use string fields for faceting - to try to get an exact match. However, it seems like to run a facet query I have to surround the value with double quotes. That poses issues when the field value is

green "bath" towels

-or-

red \cars

Those two special characters must be transformed somehow on indexing so I can create the query:
(java)
...
String fg = fieldName + ":\"" + fieldValue + "\""; query.addFacetField(fq); ...

Is there a way to request an exact match search without having to resort to the quotes? I could possibly convert spaces to underscores at index time, but I'd like to avoid munging that data because I'm using the string field for display too! That saves time/searches when aggregating against 10 - 15 fields which takes a whole lot of facet searches to begin with.

Using Solr 4.9 




--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209.html
Sent from the Solr - User mailing list archive at Nabble.com.

RE: Exact match on string field with special characters

Posted by Michael Ryan <mr...@moreover.com>.
This should do what you want:

String fq = "Field1" + "\"" + org.apache.solr.client.solrj.util.ClientUtils.escapeQueryChars(value) + "\"";

-Michael

-----Original Message-----
From: tedsolr [mailto:tsmith@sciquest.com] 
Sent: Monday, October 06, 2014 10:49 AM
To: solr-user@lucene.apache.org
Subject: Re: Exact match on string field with special characters

I may have provided too much background story for my question. What I am trying to do at the core here, is an exact match on a single field. I do this programmatically by reading the field value from the facet query and setting it equal to the field name for a subsequent search.

if this is a sample facet query result ... (Field1 is defined as a string) [Field1:[HI! THIS IS A "VALUE" FOR \FIELD1\ (100)]

Then I need to run a search for that exact value. The problem is the double quotes and slashes when I try to construct the facet query ...
String fq = "Field1:" + "\"" + value + "\"";

The quotes play havoc with the concatenation, as do backslashes. I was wondering if there's a way to build the search without having to manually construct it in code. The only thing I can come up with is to transform the field data at index time by replacing double quotes and backslashes. I don't strip special chars because I'm using the facet values for display. This problem may be specific to SolrJ. Thanks!




--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209p4162907.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Exact match on string field with special characters

Posted by tedsolr <ts...@sciquest.com>.
I may have provided too much background story for my question. What I am
trying to do at the core here, is an exact match on a single field. I do
this programmatically by reading the field value from the facet query and
setting it equal to the field name for a subsequent search.

if this is a sample facet query result ... (Field1 is defined as a string)
[Field1:[HI! THIS IS A "VALUE" FOR \FIELD1\ (100)]

Then I need to run a search for that exact value. The problem is the double
quotes and slashes when I try to construct the facet query ...
String fq = "Field1:" + "\"" + value + "\"";

The quotes play havoc with the concatenation, as do backslashes. I was
wondering if there's a way to build the search without having to manually
construct it in code. The only thing I can come up with is to transform the
field data at index time by replacing double quotes and backslashes. I don't
strip special chars because I'm using the facet values for display. This
problem may be specific to SolrJ. Thanks!




--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209p4162907.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Exact match on string field with special characters

Posted by tedsolr <ts...@sciquest.com>.
Shoot I just noticed the error in my original post which would certainly
cause confusion.

Instead of
query.addFacetField(fq); 

I meant to write
query.setParam("fq", fg);

Sorry.



--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209p4162908.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Exact match on string field with special characters

Posted by Ahmet Arslan <io...@yahoo.com.INVALID>.
Hi,

raw query parser or term query parser would be handy. 

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

Ahmet



On Thursday, October 2, 2014 12:32 AM, tedsolr <ts...@sciquest.com> wrote:
I am trying to do SQL like aggregation (GROUP BY) with solr faceting. So I
use string fields for faceting - to try to get an exact match. However, it
seems like to run a facet query I have to surround the value with double
quotes. That poses issues when the field value is

green "bath" towels

-or-

red \cars

Those two special characters must be transformed somehow on indexing so I
can create the query:
(java)
...
String fg = fieldName + ":\"" + fieldValue + "\"";
query.addFacetField(fq);
...

Is there a way to request an exact match search without having to resort to
the quotes? I could possibly convert spaces to underscores at index time,
but I'd like to avoid munging that data because I'm using the string field
for display too! That saves time/searches when aggregating against 10 - 15
fields which takes a whole lot of facet searches to begin with.

Using Solr 4.9 




--
View this message in context: http://lucene.472066.n3.nabble.com/Exact-match-on-string-field-with-special-characters-tp4162209.html
Sent from the Solr - User mailing list archive at Nabble.com.