Elasticsearch _msearch不处理聚合

时间:2017-08-10 09:55:48

标签: elasticsearch

我需要在一个请求中为多个字段获取不同的值。

当我尝试为一个字段获取不同的值时,一切都很完美:

GET shakespeare/_search
{
  "size": 0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "speaker",
        "size": 1000
      }
    }
  }
}

当我尝试使用_msearch调用多个查询时,一切都有效:

GET shakespeare/_msearch
{}
{"query" : {"match_all" : {}}, "size":2, "from":10}
{}
{"query" : {"match_all" : {}}, "size":2}

但是一旦我尝试合并它,它就会失败:

GET shakespeare/_msearch
{}
{
  "size": 0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "speaker",
        "size": 1000
      }
    }
  }
}
{}
{"query" : {"match_all" : {}}, "size":2}

我得到的错误如下:

{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Exception when parsing search request"
      }
    ],
    "type": "parse_exception",
    "reason": "Exception when parsing search request",
    "caused_by": {
      "type": "json_parse_exception",
      "reason": "Unexpected end-of-input within/between Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@1a0c4712; line: 1, column: 23]"
    }
  },
  "status": 400
}

我做错了吗?我应该以某种方式在查询中包装aggs吗?

还有其他办法吗?

任何帮助表示赞赏!谢谢:))

1 个答案:

答案 0 :(得分:1)

在一行中查询如下:

\n

即使普通查询(没有聚合)如果包含header\n body\n header\n body\n 也会失败。这是因为API格式:

\n

它将GET shakespeare/_msearch {} { "query" : {"match_all" : {}}, "size":2, "from":10} {} {"query" : {"match_all" : {}}, "size":2} 视为分隔符。以下示例也将失败。

{{1}}