Umair Ayub
Umair Ayub

Reputation: 21201

How to search a field containing [ and/or ] in it in SOLR?

I have SOLR setup.

I want to search all documents having [ or ] in it.

I have tried

nk_title:"\["

But it returns all documents in my DB.

Tried

nk_title:[*

But it gave

  "error": {
    "msg": "org.apache.solr.search.SyntaxError: Cannot parse 'nk_source:156 AND nk_title:[*': Encountered \"<EOF>\" at line 1, column 29.\nWas expecting one of:\n    \"TO\" ...\n    <RANGE_QUOTED> ...\n    <RANGE_GOOP> ...\n    ",
    "code": 400
  }

I also tried

nk_title:\[*

nk_title:*[*

But returns empty results.

Upvotes: 1

Views: 497

Answers (1)

MatsLindh
MatsLindh

Reputation: 52792

To search for [, just make sure to escape it with \ when creating the query. Given a collection with a title field defined as string with three documents:

{
    "id":"doc1",
    "title":"This is a title",
    "_version_":1602438086510247936
},
{
    "id":"doc2",
    "title":"[This is a title",
    "_version_":1602438093178142720
},
{
    "id":"doc3",
    "title":"This is [a title",
    "_version_":1602438101227012096
}

Querying for title:[* gives doc2 as a hit:

 {"numFound":1,"start":0,"docs":[{
    "id":"doc2",
    "title":"[This is a title",
    "_version_":1602438093178142720}]}

And wildcarding on both sides work as you expect (title:*\[*):

"response":{"numFound":2,"start":0,"docs":[
{
    "id":"doc2",
    "title":"[This is a title",
    "_version_":1602438093178142720},
  {
    "id":"doc3",
    "title":"This is [a title",
    "_version_":1602438101227012096}]
}}

Upvotes: 1

Related Questions