ElasticSearch嵌套_count查询获取"请求不支持[filter]"

时间:2015-04-30 19:34:51

标签: elasticsearch

我使用_search运行时查询效果很好,但使用_count时失败了。有人可以告诉我为什么吗?我不是必须运行完整的查询才能获得计数。

这是查询。

{
  "filter": {
    "nested": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "user_id": 5
              }
            }
          ]
        }
      }, 
      "path": "participants"
    }
  }
}

这是失败:

{
    "count": 0,
    "_shards": {
        "total": 5,
        "successful": 0,
        "failed": 5,
        "failures": [
            {
                "index": "messages_20150428_000025",
                "shard": 0,
                "reason": "BroadcastShardOperationFailedException[[messages_20150428_000025][0] ]; nested: QueryParsingException[[messages_20150428_000025] request does not support [filter]]; "
            },
            {
                "index": "messages_20150428_000025",
                "shard": 1,
                "reason": "BroadcastShardOperationFailedException[[messages_20150428_000025][1] ]; nested: QueryParsingException[[messages_20150428_000025] request does not support [filter]]; "
            },
            {
                "index": "messages_20150428_000025",
                "shard": 2,
                "reason": "BroadcastShardOperationFailedException[[messages_20150428_000025][2] ]; nested: QueryParsingException[[messages_20150428_000025] request does not support [filter]]; "
            },
            {
                "index": "messages_20150428_000025",
                "shard": 3,
                "reason": "BroadcastShardOperationFailedException[[messages_20150428_000025][3] ]; nested: QueryParsingException[[messages_20150428_000025] request does not support [filter]]; "
            },
            {
                "index": "messages_20150428_000025",
                "shard": 4,
                "reason": "BroadcastShardOperationFailedException[[messages_20150428_000025][4] ]; nested: QueryParsingException[[messages_20150428_000025] request does not support [filter]]; "
            }
        ]
    }
}

鉴于此嵌套字段的映射:

"participants": {
    "type": "nested",
    "properties": {
        "archived": {
            "type": "boolean"
        },
        "has_unread": {
            "type": "boolean"
        },
        "name": {
            "type": "string"
        },
        "pk": {
            "type": "long"
        },
        "user_id": {
            "type": "long"
        }
    }
},

和这些数据(仅适用于此嵌套字段):

"participants": [
    {
        "archived": false,
        "user_id": 5,
        "name": "Person A",
        "has_unread": false,
        "pk": 1
    },
    {
        "archived": false,
        "user_id": 7,
        "name": "Person B",
        "has_unread": false,
        "pk": 2
    }
],

2 个答案:

答案 0 :(得分:2)

根据documentation 计数 只接受查询,因此您需要将其重写为

{
   "query": {
      "filtered": {
         "filter": {
            "nested": {
               "filter": {
                  "bool": {
                     "must": [
                        {
                           "term": {
                              "user_id": 5
                           }
                        }
                     ]
                  }
               },
               "path": "participants"
            }
         }
      }
   }
}

答案 1 :(得分:1)

虽然这确实回答了这个问题,但它并没有帮助我如何得到我想要的东西,所以,我会赞成它,但这是我的问题的解决方案:

/_search?search_type=count
这很简单。