在Elasticsearch中使用特殊字符搜索 [英] search with special characters in elasticsearch

查看:857
本文介绍了在Elasticsearch中使用特殊字符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Elasticsearch中使用以特殊字符结尾/开头的字符进行搜索。例如 123456!

I am not able to do a search in a Elasticsearch with character ending/starting with special character. like "123456!"

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "123456!"
    }
 }
}

我的映射是

{
  "mappings": {
    "passwords": { 
      "properties": {
        "date":    { "type": "date"}, 
        "password":    { "type": "string","index": "not_analyzed"}, 

      }
    }
  }
}

它给了我错误,我该如何在搜索查询(或映射)中进行操作,以便将特殊字符视为搜索字符串的一部分?

It is giving me error, what can I do in my search query (or in the mapping), so that special characters will be treated as part of search string ?

推荐答案

由于您的密码字段为 not_analyzed (很好!),请尝试通过周围的方式进行完全匹配 123456!用双引号引起来:

Since your password field is not_analyzed (good!), try to do an exact match by surrounding 123456! with double quotes:

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "\"123456!\""
    }
 }
}

另一种方法是设置关键字您的 query_string 查询中的分析器(但请确保转义,因为它是保留字符(对于 NOT 运算符)

Another way of doing this is to set the keyword analyzer in your query_string query (but make sure to escape the ! because it's a reserved character (for the NOT operator)

{
 "query": {
    "query_string": {
        "default_field": "password",
        "query": "123456\!",
        "analyzer": "keyword"
    }
 }
}

这篇关于在Elasticsearch中使用特殊字符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆