【问题标题】:Elasticsearch itself ignores the special characters in query string. How can i avoid that?Elasticsearch 本身会忽略查询字符串中的特殊字符。我怎样才能避免这种情况?
【发布时间】:2018-07-13 08:32:24
【问题描述】:

我必须用特殊字符搜索一些特定的 query_string,但它会给我所有的结果。

当我分析它时:

GET /exact/_analyze
{
  "field": "subject",
  "text": "abdominal-scan"
}

输出如下:

{
  "tokens": [
    {
      "token": "abdominal",
      "start_offset": 0,
      "end_offset": 9,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "scan",
      "start_offset": 10,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

这意味着自动跳过连字符 (-) 并将这些单词视为 2 个单词。

如果我将字段的索引更改为 not_analyzed,则我无法在该字段中搜索单个单词,整个句子必须在 query_string 中传递。

是否有任何其他选择,以便我可以得到精确的搜索(不忽略特殊字符)?

【问题讨论】:

    标签: elasticsearch special-characters exact-match


    【解决方案1】:

    您应该查看definitive guide's section about analysis,因为这对于了解索引的行为非常重要。

    默认情况下,您的字段使用standard analyzer 进行分析,它在连字符上分割单词。

    whitespace analyzer 是一个非常容易理解的分析器,它将输入拆分为空白字符上的标记。

    你可以试试这个例子:

    POST /solution
    {
      "mappings":{
        "doc": {
          "properties":{
            "subject": {
              "type": "string",
              "analyzer": "whitespace"
            }
          }
        }
      }
    }
    
    GET /solution/_analyze
    {
      "field": "subject",
      "text": "This is about abdominal-scan"
    }
    

    哪个输出:

    {
      "tokens": [
        {
          "token": "This",
          "start_offset": 0,
          "end_offset": 4,
          "type": "word",
          "position": 0
        },
        {
          "token": "is",
          "start_offset": 5,
          "end_offset": 7,
          "type": "word",
          "position": 1
        },
        {
          "token": "about",
          "start_offset": 8,
          "end_offset": 13,
          "type": "word",
          "position": 2
        },
        {
          "token": "abdominal-scan",
          "start_offset": 14,
          "end_offset": 28,
          "type": "word",
          "position": 3
        }
      ]
    }
    

    您可以看到在这种情况下您的连字符被保留了。

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      相关资源
      最近更新 更多