首页 > 解决方案 > 如何在 SOLR 中搜索包含 [ 和/或 ] 的字段?

问题描述

我有 SOLR 设置。

我想搜索所有包含[]其中的文档。

我努力了

nk_title:"\["

但它会返回我数据库中的所有文档。

试过了

nk_title:[*

但它给了

  "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
  }

我也试过

nk_title:\[*

nk_title:*[*

但返回空结果。

标签: apachesolr

解决方案


要搜索[,只需确保\在创建查询时将其转义即可。给定一个集合,其title字段定义为string三个文档:

{
    "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
}

查询title:[*doc2定的命中:

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

双方的通配符都可以按预期工作(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}]
}}

推荐阅读