代码之家  ›  专栏  ›  技术社区  ›  Umair Ayub

如何在solr中搜索包含[和/或]的字段?

  •  1
  • Umair Ayub  · 技术社区  · 7 年前

    我有解决方案。

    我想搜索所有 [ ] 在里面。

    我试过了

    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:*[*
    

    但返回空结果。

    1 回复  |  直到 7 年前
        1
  •  1
  •   MatsLindh    7 年前

    寻找 [ ,只要确保用 \ 创建查询时。给一个集合 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}]
    }}
    
    推荐文章