首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ElasticSerach查询中转义反斜杠

如何在ElasticSerach查询中转义反斜杠
EN

Stack Overflow用户
提问于 2017-10-26 17:24:22
回答 1查看 2.5K关注 0票数 0

我在ElasticSearch中有以下字段:

代码语言:javascript
运行
复制
 "type": "doc\doc1"

我的目标是选择一个特殊的类型,我尝试过:

代码语言:javascript
运行
复制
GET /my_index/my_type/_search
{
"query":{  
  "bool":{  
     "must":{  
        "term":{  
           "type": "doc\\doc1"
        }
     }
  }
}
}

但是它不起作用,我试过了:

代码语言:javascript
运行
复制
"type": "doc\\\\doc1"
"type": "\"doc\\\\doc1"\"
"type": "\"doc\\doc1"\"

但是查询不返回任何结果。

我尝试过:

代码语言:javascript
运行
复制
GET /my_index/my_type/_search
{
"query" : { 
  "query_string" : {
    "query" : "doc\\doc1",
    "analyzer": "keyword"      
  } 
}
} 

但这是相同的输出。

任何帮助都将不胜感激

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-10-26 17:51:58

您需要在字段值中转义反斜杠。

然后,您可以使用关键字分析器with term query搜索确切的值,或者您可以使用match query with analyzed:

代码语言:javascript
运行
复制
PUT test
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "type1": {
         "properties": {
            "field1": {
               "type": "text",
               "fields": {
                  "raw": {
                     "type": "keyword"
                  }
               }
            }
         }
      }
   }
}


PUT test/type1/1
{
    "field1" : "doc\\doc1"
}


POST test/_search?pretty
{
   "query": {
      "bool": {
         "must": {
            "term": {
               "field1.raw": "doc\\doc1"
            }
         }
      }
   }
}


POST test/_search?pretty
{
   "query": {
      "bool": {
         "must": {
            "match": {
               "field1": "doc\\doc1"
            }
         }
      }
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46950526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档