How to use regex for queries in Solr 4
I am desperate, so I ask for help. I am trying to query results from a Solr 4 engine using a regular expression.
Suppose I want to request a document:
<str name="text">description: best company; name: roca mola</str>
And I want to execute a query using this regex:
description:(.*)?company(.*)?;
I read in some forums that using regex in Solr 4 was as easy as adding a forward slash, for example:
localhost:8080/solr/q=text:/description\:(.*)?company(.*)?;/
but it doesn't work. And this one doesn't work either:
localhost:8080/solr/q=text:/description(.*)?company(.*)?;/
I don't need a simple query:
localhost:8080/solr/q=text:*company*
Since this would not match the docs, for example:
<str name="text">description: my home; name: mother company"</str>
If I don't understand, please let me know.
Greetings from Chile: D
NOTE . I have used margins text_general
in my diagram. As @arun pointed out, fields string
can handle the type of regex I'm using.
source to share
Instead of looking for a regex in a field type, text
try it in a type field string
, since your regex spans more than one word. (If your regex must match a single word, you can use a field text
.)
Also perform percentage encoding of special characters to ensure they are not causing inconsistencies.
q=strfield:/description%3A(.*?)company(.*?)%3B.*/
Update: Just tried this on a string field. The above regex works. It works even without coding percentages. Ie
q=strfield:/description:.*?company.*?;.*/
source to share