分析带前导连字符和分析端点的文本



使用弹性搜索来分析一些文本(使用_analyze端点)。

我看到,当有前导连字符时,它不会返回json,而是返回一些其他格式。

尝试搜索有关此的文档,但一无所获。有人能告诉我原因吗?有没有办法强制输出json?以下示例。

谢谢。


简短的例子,一个文本是"this is",另一个是"--------this is"。

这很好:

% curl -XGET 'localhost:9200/_analyze?analyzer=standard' -d 'this is'
{"tokens":[{"token":"this","start_offset":0,"end_offset":4,"type":"<ALPHANUM>","position":1},{"token":"is","start_offset":5,"end_offset":7,"type":"<ALPHANUM>","position":2}]}

但带前导码--返回其他格式的

% curl -XGET 'localhost:9200/_analyze?analyzer=standard' -d '---------this is'
---
tokens:
- token: "this"
  start_offset: 9
  end_offset: 13
  type: "<ALPHANUM>"
  position: 1
- token: "is"
  start_offset: 14
  end_offset: 16
  type: "<ALPHANUM>"
  position: 2

当文档没有说明时,您应该转向最终的"文档"资源,即代码。

从将处理_analyze调用的主RestAnalyzeAction REST端点开始,我们可以看到,在第87行,它将尝试通过调用RestActions.guessBodyContentType来猜测请求主体的内容类型。该方法将转而调用XContentFactory.xContentType,在后者中,我们可以在第156行找到原因,即,如果正文以两个连字符开头,则请求被解释为YAML,响应将相应地格式化为YAML。

您可以通过在curl命令中添加-v(表示详细)开关来确认这一事实:

curl -v -XGET 'localhost:9200/_analyze?analyzer=standard' -d '---------this is'

您将得到的响应将显示响应的内容类型为application/yaml

* Connected to localhost (::1) port 9200 (#0)
> GET /_analyze?analyzer=standard HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:9200
> Accept: */*
> Content-Length: 16
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 16 out of 16 bytes
< HTTP/1.1 200 OK
< Content-Type: application/yaml                   <---- HERE
< Content-Length: 183
< 
---
tokens:
- token: "this"
  start_offset: 9
  end_offset: 13
  type: "<ALPHANUM>"
  position: 1
- token: "is"
  start_offset: 14
  end_offset: 16
  type: "<ALPHANUM>"
  position: 2

当我尝试相同的有意义的插件时,我得到了以下结果

GET /_analyze?analyzer=standard&text=-this is
{
   "tokens": [
      {
         "token": "this",
         "start_offset": 1,
         "end_offset": 5,
         "type": "<ALPHANUM>",
         "position": 1
      },
      {
         "token": "is",
         "start_offset": 6,
         "end_offset": 8,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}
GET /_analyze?analyzer=standard&text=---------this is
{
   "tokens": [
      {
         "token": "this",
         "start_offset": 9,
         "end_offset": 13,
         "type": "<ALPHANUM>",
         "position": 1
      },
      {
         "token": "is",
         "start_offset": 14,
         "end_offset": 16,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}

最新更新