1

I am trying to execute a search query with search_type of count with the elasticsearch.angular.js build from the npm module.

I can execute the query like this:

POST /index1/type1/_search?search_type=count

 {
   "aggs": {
   "reviews": {
     "nested": {
       "path": "reviews"
      }
    }
  }
}

but when I try to translate the query to the .js api, I get an error. My code looks like this:

  var requestObject = {
      index:'index1',
      type:'type1',
      searchType: 'count',
      body: {
        query:{
          aggs: {
            reviews: {
              nested: {
                path: "reviews"
              }
            }
          }
        }
    };
    esClient.search(requestObject)

The trace looks like this:

  console.js:1 DEBUG: 2015-08-04T15:28:59Z
      starting request { method: 'POST',
        path: '/index1/type1/_search',
        body: { aggs: { reviews: [Object] } },
        query: { search_type: 'count' } }

That looks OK to an elasticsearch newbie, but the request completes with an error: ReferenceError: count is not defined.

What am I missing here please?

4
  • It's probably not related (never know though), but the aggs part needs to be a direct child of the body element, i.e. not nested inside the query element. Can you make that change and try again to see if that makes any difference? Commented Aug 4, 2015 at 16:32
  • I'm pretty sure that that's a JS error and not an ES error. Can you post how you're handing the response of esClient.search()? Commented Aug 4, 2015 at 16:58
  • @robertkliep I'm not at my computer now, but I am handling the response with the promise syntax. Commented Aug 4, 2015 at 18:53
  • @robertklep: just checked my response-handling code and sure enough I was console.logging something called 'count'. I'll blame it on the end of a long day, but thanks for your help! Commented Aug 4, 2015 at 19:22

1 Answer 1

1

It turned out that my problem was a stupid error on my part (thanks @robertklep for pointing it out). The code above actually works correctly. As I was not able to find an example of using using searchType from the api, I am leaving this here in the hope it will be useful to somebody else.

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

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.