Tuesday 30 May 2017

how to count the number of documents under nested fields in elasticsearch

I am new to elasticsearch, just started to try out utilize the elasticsearch to do fulltext search on nodejs. Currently I have a problem to find out the count the number of item of the nested field. For example suppose this is my data:

{
  user1: {
    movies: {
      {
        title: 'eat'
      },
      {
        title: 'drink'
      }
    },
    books: {
      {
        author: 'apple'
      }
    }
  },
  user2: {
    movies: {
      {
        title: 'jump'
      },
      {
        title: 'punch'
      },
      {
        title: 'sleep'
      },
      {
        title: 'eat'
      }
    },
    books: {
      {
        author: 'orange'
      }
    }
  }
}

how to do a simple count of how many movies does user1 or user2 have?(2 and 4 in this case).

If it turns out to be surprisingly difficult, I could still fallback to store that number somewhere else.



via Sean sean

No comments:

Post a Comment