【问题标题】:elastic Search not working for having special character '^(caret symbol)'弹性搜索不适用于具有特殊字符'^(插入符号)'
【发布时间】:2016-10-15 07:11:47
【问题描述】:

问题是任何具有提升运算符“^(插入符号)”的字符序列都不会返回任何搜索结果。

但是根据下面的弹性搜索文档

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters

    • && || ! ( ) { } [ ] ^ " ~ * ? : \ 字符可以用 \ 符号转义。

需要在弹性搜索中使用 n-gram 分析器进行包含搜索。

下面是示例用例的映射结构和

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "nGram_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding"
            ],
            "type": "custom",
            "tokenizer": "ngram_tokenizer"
          },
          "whitespace_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding"
            ],
            "type": "custom",
            "tokenizer": "whitespace"
          }
        },
        "tokenizer": {
          "ngram_tokenizer": {
            "token_chars": [
              "letter",
              "digit",
              "punctuation",
              "symbol"
            ],
            "min_gram": "2",
            "type": "nGram",
            "max_gram": "20"
          }
        }
      }
    }
  },
  "mappings": {
    "employee": {
      "properties": {
        "employeeName": {
          "type": "string",
          "analyzer": "nGram_analyzer",
          "search_analyzer": "whitespace_analyzer"
        }
      }
    }
  }
}

具有如下所示的员工姓名,其中包含特殊字符 xyz%^&*

还有用于包含搜索的示例查询,如下所示

GET
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "employeeName": {
              "query": "xyz%^",
              "type": "boolean",
              "operator": "or"
            }
          }
        }
      ]
    }
  }
}

即使我们尝试以 "query": "xyz%\^" 的形式转义,它的错误也会消失。所以无法搜索任何包含“^(插入符号)”的字符

非常感谢任何帮助。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    ngram tokenizer 中存在与issue 相关的错误。

    基本上^ 不被Symbol |Letter |Punctuation 视为ngram-tokenizer。 结果,它标记了^ 上的输入。

    例子:(url编码xyz%^):

    GET <index_name>/_analyze?tokenizer=ngram_tokenizer&text=xyz%25%5E

    上面analyze api的结果显示没有^,如下回复所示:

    {
       "tokens": [
          {
             "token": "xy",
             "start_offset": 0,
             "end_offset": 2,
             "type": "word",
             "position": 0
          },
          {
             "token": "xyz",
             "start_offset": 0,
             "end_offset": 3,
             "type": "word",
             "position": 1
          },
          {
             "token": "xyz%",
             "start_offset": 0,
             "end_offset": 4,
             "type": "word",
             "position": 2
          },
          {
             "token": "yz",
             "start_offset": 1,
             "end_offset": 3,
             "type": "word",
             "position": 3
          },
          {
             "token": "yz%",
             "start_offset": 1,
             "end_offset": 4,
             "type": "word",
             "position": 4
          },
          {
             "token": "z%",
             "start_offset": 2,
             "end_offset": 4,
             "type": "word",
             "position": 5
          }
       ]
    }
    

    由于 '^' 未编入索引,因此没有匹配项

    【讨论】:

    • 感谢@keety 的回复。
    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多