1

I have a query that works fine when I run it using _search, but it fails when using _count. Can someone tell me why? I'd rather not have to run the complete query just to get a count.

This is the query.

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

This is the failure:

{
    "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]]; "
            }
        ]
    }
}

Given this the mapping for this nested field:

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

and these data (for this nested field only):

"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 Answers 2

2

As per the documentation count can accept only query hence you would need to rewrite it as

{
   "query": {
      "filtered": {
         "filter": {
            "nested": {
               "filter": {
                  "bool": {
                     "must": [
                        {
                           "term": {
                              "user_id": 5
                           }
                        }
                     ]
                  }
               },
               "path": "participants"
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

While that does answer the question, it doesn't help me with how to get what I want, so, I'll upvote it, but here's the solution to my question:

/_search?search_type=count

simple as that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.