弹性搜索:像查询不在字符串中包含&(和号)?

时间:2016-03-17 07:28:47

标签: elasticsearch

我的查询无效。我认为搜索的值包含& (&符号)符号。我在谈论以下问题。

GET "path"_search
{

  "from": 0,
  "size": 10000,
  "query": {
    "query_string": {
      "query": "(Department_3037.Department_3037_analyzed:*P&C*)"
    }    
  }
}

为什么这个查询不起作用以及如何克服这个问题,或者我需要查询包含&的字符串喜欢(p& c),l& t等... 让我知道如何解决这个问题。 谢谢。

1 个答案:

答案 0 :(得分:2)

由于您要搜索的字段为analyzed。如果该字段包含文字(Department_3037.Department_3037_analyzed:*P&C*),则会将其标记为:Department_3037Department_3037_analyzedPC

你可以使用:

curl -XGET "http://localhost:9200/_analyze?tokenizer=standard" -d "(Department_3037.Department_3037_analyzed:*P&C*)"

您将获得如下令牌:

{"tokens":[{"token":"Department_3037","start_offset":1,"end_offset":16,"type":"<ALPHANUM>","position":1},

{"token":"Department_3037_analyzed","start_offset":17,"end_offset":41,"type":"<ALPHANUM>","position":2},

{"token":"P","start_offset":43,"end_offset":44,"type":"<ALPHANUM>","position":3},

{"token":"C","start_offset":45,"end_offset":46,"type":"<ALPHANUM>","position":4}]}.

如果要检索文档,则必须转义特殊字符:

{
"query": {
   "query_string": {
      "default_field": "name",
      "query": "\\(Department_3037\\.Department_3037_analyzed\\:\\*P\\&C\\*\\)"
    }
   }
 }

希望它有所帮助。