B.Mr.W. B.Mr.W. - 11 days ago 6
Java Question

Solr Webapp Regular Expression Search

I am following the Solr quick start tutorial and got the web app up and running. I know that Solr has a regular expression search functionality and I am having a hard time get it working.

enter image description here

So here, I have a field called MFR and I want to use regular expression to match values like AVAGO. However, it is giving me errors like cannot parse it correctly, then I tried to escape certain characters and all the following didn't work:

MFR:AV\[A\]GO
MFR:AV/[A/]GO
MFR:AV/[A/]GO
MFR:"AV\[A\]GO"
MFR:"AV/[A/]GO"
MFR:"AV[A]GO"


Now, I started to ask myself is the q parameter the right place where I can put regular expressions or I am not even in the ballpark.

I also played with the TestRegexpQuery in Eclipse running the unit test and clearly, the query is a different type of query. How can I get regex working in the GUI?

Answer

Your question is how to use a RegExpQuery on query screen.

You already did use RegExpQuery in lucene.

As default solr using the standard query parser. The standard query parser does support RegexpQuery but a RegExp have to start and end with the slash character / like MRF:/AV[A-X]GO/ or MRF:/av[a-x]go/.

Be aware that a regular expression must match against the token in the inverted index and not against the original word.

E.g. for FieldType "text_general" a word AVAGO will become the token avago, so a regExpr with uppercase letters would not match.

You can test with the solr analysis screen the way from a word (in real text) to the corresponding token in the index.

See also: How to use regex for querying in Solr 4

Comments