1

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
}
ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
Tomasz Swider
  • 2,314
  • 18
  • 22
  • could you verify if this is the right query? – ChintanShah25 Dec 31 '15 at 18:30
  • I am not es expert so I am sending the query to it to see it it is valid and yes it is valid and working as expected, where as the query proposed by Or Weinberger does not work. As I understand it to have filter for your aggregation, it has to be 'in side' of the aggregation. Where as the 'query' filed is out side of it but it has bit different syntax than filter one. – Tomasz Swider Dec 31 '15 at 20:55

2 Answers2

2

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!!

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
0

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": []
        }
      }
    }
  }
}
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
  • So I have tried that ( elasticsearch 1.7 ) and it is returning "error": "SearchPhaseExecutionException[Failed to execute phase [query], it is working when I replace root level 'query' with 'filter'. Anyway the question was about elastic.js syntax – Tomasz Swider Dec 31 '15 at 18:57