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
}
2
  • could you verify if this is the right query? Commented Dec 31, 2015 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. Commented Dec 31, 2015 at 20:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

So it is not 100 % what I need it pointed me in right direction.
So is it possible to express the original query in elastic.js ?
It is possible, I have edited my answer pls have a look, also query string is not necessary, it is selecting everything
Thanks for the tool thing it is very useful
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": []
        }
      }
    }
  }
}

1 Comment

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

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.