Should I use runtime fields to design StackOverflow’s search schema?

I'm using StackOverflow as a motivating example because it's similar enough to my situation. Here's a screenshot of StackOverflow's search for context:

enter image description here

The "Question" document schema includes basics like title/description/author. Additionally, there's a "vote count" and (in my case) whether or not the current user has upvoted a question.

Vote count could be implemented as a simple integer on the "Question" document. However, the setting of vote count should be idempotent due to the impossibility of exactly-once delivery. Therefore vote count will be implemented as a list of UserIds who have upvoted that question. I was initially trying to figure out if you could do aggregations on a field of a document, but now I think runtime fields are the best way forwards, especially because you can index the resulting count (and therefore easily sort by popularity).

To return if the current user has upvoted a question, I plan on using a runtime field (again). I'll pass a script_fields parameter containing the current user's id as part of the request. The runtime field will be a Painless script that takes the provided UserId parameter and searches the Question document's ListOfUserIdsWhoUpvoted for that UserId.

Does this runtime field approach sound good, or is there a better way? Would this be impossible prior to 7.12, when runtime fields were introduced?

Read more here:

Content Attribution

This content was originally published by DharmaTurtle at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: