Tomasz Swider
Tomasz Swider

Reputation: 2382

Elasticsearch aggregation query in elastic.js

I have a hard time translating aggregation query for elastic search into elastic.js. I am reading the documentation but I just can not figure it out. And the examples that you can find online are mostly about deprecated facets feature, that is not very useful. The JSON for example aggregation is as follows:

{
  "aggs": {
    "foo": {
      "filter": {
        "bool": {
          "must": [
            {
              "query": {
                "query_string": {
                  "query": "*"
                }
              }
            },
            {
              "terms": {
                "shape": [
                  "wc"
                ]
              }
            }
          ]
        }
      },
      "aggs": {
        "field": {
          "terms": {
            "field": "shape",
            "size": 10,
            "exclude": {
              "pattern": []
            }
          }
        }
      }
    }
  },
  "size": 0
}

Upvotes: 1

Views: 1576

Answers (2)

ChintanShah25
ChintanShah25

Reputation: 12672

This is how you would nest terms aggregation into filter aggregation with elasticjs

ejs.Request()
    .size(0)
    .agg(ejs.FilterAggregation("foo").filter(ejs.BoolFilter()
            .must(ejs.TermsFilter('shape', 'wc'))
            .must(ejs.QueryFilter(ejs.QueryStringQuery().query("*"))))
              .agg(ejs.TermsAggregation("field").field("shape").size(10).exclude("my_pattern"))
        )

BTW you are filtering on shape and then doing aggregations on it. I am not sure what exactly you are trying.

I found their documentation pretty good, Also they have a great tool to check if your query is valid and right. This would help you a lot

Hope this helps!!

Upvotes: 2

Or Weinberger
Or Weinberger

Reputation: 7482

It seems like you have misplaced your query under the aggs element. Try this:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query": {
            "query_string": {
              "query": "*"
            }
          }
        },
        {
          "terms": {
            "shape": [
              "wc"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "foo": {
      "terms": {
        "field": "shape",
        "size": 10,
        "exclude": {
          "pattern": []
        }
      }
    }
  }
}

Upvotes: 0

Related Questions