弹性搜索ngram特殊字符



我有一个弹性搜索节点,具有以下默认配置

index :
  analysis :
    analyzer :
      default_index :
        type : custom
        tokenizer : whitespace
        filter :
        - lowercase
        - asciifolding
        - stop
        - my_ngram
        char_filter : html_strip
      default_search:
        type : custom
        tokenizer :  whitespace
        filter:
        - lowercase
        - asciifolding
        - stop
        char_filter :  html_strip
    filter:
      my_ngram:
        type: nGram
        max_gram: 50

然后我创建一个索引"测试"

curl -XPUT localhost:9200/test -d '{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  }
}'

我发布了

curl -XPOST localhost:9200/test/sub -d '{"n1" : "so?:me"}'

作为搜索

curl -XPOST 'localhost:9200/test/sub/_search?pretty&q=?'

上面显示的条目我得到了正确的结果,但当我进行时

curl -XPOST localhost:9200/test/sub/_search -d '{
  "query": {
    "query_string": {
      "query": "?"
    }
  }
}'

我得到一个异常,如下

{
  "error": "SearchPhaseExecutionException[Failed to execute phase [query_fetch], total failure;
            shardFailures {[1fLLfu79Qou8RbdrI6y8qw][test][0]: 
            SearchParseException[[test][0]: from[-1],size[-1]: 
            Parse Failure [Failed to parse source [
              {
                "query": {
                  "query_string": {
                    "query": "\?"
                  }
                }
              }
            ]]];
            nested: QueryParsingException[[test] Failed to parse]; 
            nested: JsonParseException[Unrecognized character escape '?' (code 63)n at [Source: [B@1601cda; line: 1, column: 45]]; }]",
  "status": 500
}

我不确定我在这里错过了什么?

我发现了更多的细节,这更令人困惑。

如果我发布

curl -XPOST localhost:9200/test/sub/_search -d '{
  "query": {
    "query_string": {
      "query": "\?"
    }
  }
}'

我得到了正确的结果,看起来JSON转义字符必须自己转义。但后来我发布了

curl -XPOST localhost:9200/test/sub -d '{"n1" : "oi\me"}'

现在如果我发布

curl -XPOST localhost:9200/test/sub/_search?pretty -d '{
  "query": {
    "query_string": {
      "query": "\\"
    }
  }
}'

我得到了结果,假设我之前发现的只是答案中的第一个"\",它显示了理想的

curl -XPOST localhost:9200/test/sub/_search?pretty -d '{
  "query": {
    "query_string": {
      "query": "\\\\"
    }
  }
}'

应该行得通,但不行。非常困惑。

我认为这是因为在oi\me中,第一个后斜杠用于转义第二个后斜杠,而不是存储为文字字符。这解释了\\工作的原因,因为在HTTP请求中,其中两个斜杠转义其他两个,然后在查询中,其余的第一个转义第二个。

一般来说,当您将查询作为JSON传递时,必须进行更多转义。也就是说,

curl -XPOST 'localhost:9200/test/sub/_search?pretty&q=?'

与相同

curl -XPOST 'localhost:9200/test/sub/_search?pretty' -d '{"query" : {"query_string" : {"query" : "\?"}}}'

是的,你对转义的表示是正确的,我昨天很晚才开始工作,但仍然无法正确地进行"\"搜索,对于json搜索帖子,我们需要一个额外的"\",因此对于第一个选择,我将发布

curl -XPOST 'localhost:9200/test/sub/_search?pretty' -d '{"query" : {"query_string" : {"query" : "\"}}}'

但这并没有帮助,必须使用"\\",而且不能使用更多或更少,所以试图弄清楚匹配单个"\"one_answers"\"的查询是什么,尽管在python中使用弹性utils,如果我只是用"\"转义每个特殊字符,那么通过包括"\"在内的所有特殊字符的代码,它效果很好,但curl没有

最新更新