【问题标题】:Elastic Search - Search string having spaces in it弹性搜索 - 搜索包含空格的字符串
【发布时间】:2015-04-28 15:06:11
【问题描述】:

我正在寻找 ElasticSearch 查询,它将提供精确匹配的字符串,其中包含空格。

例如 - 我想搜索“XYZ 公司解决方案”之类的词。 我尝试了查询字符串查询,但无论搜索结果如何,它都会给我所有记录。我还阅读了帖子,发现我们必须为该字段添加一些映射。我在现场尝试了“Not_Analyzed”分析仪,但它仍然不起作用。

如果有人有完整的示例或步骤,可以分享给我吗?

提前致谢。

谢谢, 萨米尔

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    由于您没有发布代码,因此很难判断出了什么问题,但映射中的 "index": "not_analyzed" 是处理此问题的正确方法。

    这是一个简单的工作示例。首先我创建一个使用"index": "not_analyzed"的映射:

    PUT /test_index
    {
        "mappings": {
            "doc": {
                "properties": {
                    "name":{
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
    

    然后添加几个文档进行测试

    POST /test_index/doc/_bulk
    {"index":{"_id":1}}
    {"name":"XYZ Company Solutions"}
    {"index":{"_id":2}}
    {"name":"Another Company"}
    

    现在我可以通过简单的term query 得到我想要的文档:

    POST /test_index/doc/_search
    {
        "query": {
            "term": {
               "name": {
                  "value": "XYZ Company Solutions"
               }
            }
        }
    }
    ...
    {
       "took": 1,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1,
          "hits": [
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                   "name": "XYZ Company Solutions"
                }
             }
          ]
       }
    }
    

    term filter 甚至 match query 在这种情况下也可以使用。

    这是我用来测试它的代码:

    http://sense.qbox.io/gist/90fcc7f7a88d58f098c50d5aaf0315fdf06e9e9a

    【讨论】:

    • 我只是补充一点,从 5.0 版开始,不再支持“字符串”类型,并引入了两个新的类型。 “文本”默认设置为已分析,“关键字”默认设置为 not_analyzed。因此,如果您使用的是较新版本,只需将类型更改为“关键字”即可。
    【解决方案2】:
    PUT /index_1
    {   
      "settings": {
        "analysis": {
          "normalizer": {
            "lowercase_normalizer": { "type": "custom", "char_filter": [],           "filter": ["lowercase"]}
          }
        }
      },
      "mappings": {
         "doc_type": {
                "properties": {
                    "name":{"type": "keyword", "normalizer": "lowercase_normalizer"}
                }
         }
      }
    }
    

    我使用上面的设置和映射来定义索引。 然后将一些值推送到数据库中

    POST index_1/doc_type/1
    {
      "name" : "a b c"
    }
    
    POST index_1/doc_type/1
    {
      "name" : "a c"
    }
    
    POST index_1/doc_type/1
    {
      "name" : "a b"
    }
    

    现在,如果我们在上述索引的名称字段中搜索单个字母,我们将一无所获

    GET index_1/doc_type/_search
    {
      "query" : 
        {"match": {"name": "A"}}
    }
    
    GET index_1/doc_type/_search
    {
      "query" : 
        {"match": {"name": "b"}}
    }
    

    但是如果我们搜索

    GET index_1/doc_type/_search
    {
      "query" : 
        {"match": {"name": "A B C"}}
    }
    

    我们会得到匹配的

    这有助于搜索完整的关键字,同时避免区分大小写

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2023-03-09
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多