arelastic icon indicating copy to clipboard operation
arelastic copied to clipboard

trouble with syntax

Open schowdhury opened this issue 6 years ago • 1 comments

I'm having trouble figuring out the query for something like the following (more examples / documentation would be helpful). Can you guide me in the right direction as to how

{
  "query": {
    "terms": {
      "type": ["some type"]
    }
  },
  "size": 0,
  "aggs": {
    "documents": {
      "terms": {
        "field": "group_id"
      }
    }
  }
}

I started with the query:

Arelastic::Builders::Queries.terms(
      'type', ['some type'])

and then the aggregation

Arelastic::Searches::Aggregations.new(
    Arelastic::Aggregations::Terms.new('documents', 'field' => 'group_id'))

# {"aggs"=>{"documents"=>{"terms"=>{"field"=>"group_id"}}}}

How do I piece them together to get the above query?

schowdhury avatar Sep 23 '19 17:09 schowdhury

This should produce the query you are after:

Arelastic::Nodes::HashGroup.new([
  Arelastic::Searches::Query.new(
    Arelastic::Builders::Queries.terms('type', ['some type'])),
  Arelastic::Searches::Size.new(0),
  Arelastic::Searches::Aggregations.new(
    Arelastic::Aggregations::Terms.new('documents', 'field' => 'group_id'))
]).as_elastic

Some additional examples have been added to the README which hopefully makes this more clear.

j1wilmot avatar Oct 31 '19 22:10 j1wilmot