How to limit number of documents having a given property value

Problem: I have a document type 'employee' with a property called 'department'. I would like to limit the number of employees in any given department to N.

Does ES provide means to enforce such a rule during document creation. If yes, what would the best way of achieving this.
Please note that using a RDBMS or some other form of storage is not an option.
This is a familiar problem, however, I am not aware of ways of achieving this in ElasticSearch. Pointers would be appreciated.

As I know there are no such constraint possible, but if you are creating array property of department, then before inserting try selecting that doc and check department count before inserting,

What do you mean when you say creating an array property of department? Are you suggesting having an array of department in employee or vice versa?

_id : 10
employee_id : 20
Employee_name : "test1"
department : ["dep1", "dep2","dep3","dep4","dep5"]

So before updating you can read this document and check this array count if its already 5. using inline script. (painless in ES version 5.0 and above)

I see your point. Taking a look at inline scripts in ES 5.0. That said in my case it would be the department document that would need to contain the employees for I need to limit the number of employees in a department versus limiting the number of departments an employee can belong to.

If you have used inline script in ES 5.0, how did you find its performance. Any resources you can point me to would be helpful.


This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.