Search code examples
regexsolr

Using Regex in SOLR Query


I have a data set of street names and numbers which I need to search.

eg. 12 HILL STREET
    12A HILL STREET
    12B HILL STREET
    123 HILL STREET
    12 HILARY STREET

If I search as follows q=(street_name:12\ HILL*), I get

12 HILL STREET

I want to obtain the following results:

12 HILL STREET
12A HILL STREET
12B HILL STREET

Is there a way to query in SOLR to return the results as the above example shows?

I have tried querying as:

q=(street_name:/12[A-Z]\ HILL*/)

but don't get anything back.


Solution

  • You can use

    q=(street_name:/12[A-Z]* HILL.*/)
    

    Here, the pattern means

    • 12 - string starts with 12
    • [A-Z]* - zero or more ASCII uppercase letters
    • - a space
    • HILL - HILL char sequence
    • .* - any zero or more chars other than line break chars as many as possible (so, the rest of the line).