Sunday 19 March 2017

Elasticsearch query with aggregation across two types (I think)

I am trying to figure a way to do this query in Elasticsearch and am having a very hard language. I don't think I know exactly what to search for or research to find the answer and this seems way out of my league in terms of a basic Elasticsearch query.

Anyways, I have two types (really I have about 8 but this pertains only to two of them) in my Elasticsearch instance. Lets call them Invitees and Answers. Now the invitees model has an array of invitees which is basically "who has been invited to answer questions about this particular request". A special thing to note is that the invitees are invited by their role and expertise, and not by a user name, however when a user answers, their user name is tracked as there can be multiple people with the same role/expertise that answer questions about a request.

The Invitees model looks like this requestId: String, invitees:[ { role: String, expertise: String
} ]

The Answers model looks like this. I will spare the unimportant fields here, but the answers model is created each time a person answers a question for a request. Anyone can answer questions about a request, however in the particular query I will get to will only be able the invited roles/expertise.

requestId: String (should match the invitees requestId), role: String, expertise: String, username: String, answers: Array of questions which were answered.

The query I am trying to write deals with querying both the answers and invitees type and finding all instances where the invited roles/expertises ALL answered questions for a given request. Basically a query which would essentially be able to find the roles/expertises invited, ensure that there is at least one Answer type for each of them for the request and return that request ID (which is attached to both the invitee and answer models). Since anyone can answer questions about any request, its not as simple as counting the number of answer types and matching that to the length of the array within the invitee type. In terms of the questions specifically, they do not matter. If a user answers a question an answer type will be created for them in the Elasticsearch instance. If there are no answers in the instance then that means that no one has answered any questions. The Request type is also available in the Elasticsearch instance, however the request ID is attached to both the invitees and answers model so I don't think it needs to be involved in the query.... but I am not 100% sure at this point.

I will take any solution that doesn't result in grabbing all documents out of Elasticsearch and doing some intense iteration on the results in some other language. I am really at a loss here.

I am sure that this is not the best way to have this system set up, however I was kind of dropped into this with the requirement of delivering a solution. I have had to do several other types of queries for this specific system and have eventually found a way to do them.... but this one... this one I just cannot seem to gain any ground on. Also, if it matters I am working in a MEAN stack environment.



via Chris D.

No comments:

Post a Comment