Ad hoc query time fields?

Is it possible to generate fields at query time based on filters?

I want query a set of documents and generate at query time fields on those documents that denote if a set of filters match for them.

Concrete example...

# Document
{
type: "User",
id: 1,
age: 35,
name: "Chris",
gender: "m"
}

# Filters
filter1 = { term: { gender: "f" } }
filter2 = { range: { age: { gt: 30 } } }
filter3 = { prefix: { user: "Ch" } }

# Query
...???...

# Results
{
type: "User",
id: 1,
age: 35,
name: "Chris",
gender: "m",
num_filters_matched: 2 # How to get this?
}

And I guess the real tricky part is that I want to be able to sort and filter by that ad hoc / dynamically generated field num_filters_matched.

Is this possible?

Thanks for the help.

Is named filter good enough?

Maybe! But I'm using a really old version of Es (1.2.1) and I'm running into this issue:

I can't figure out the right incantation to get it to work with "and" filters. This doesn't work:

{ or: [{and: [...], _name: "f1"}, {and: [...], _name: "f2"}] }

:confused:

Thanks!

or and and are pretty deprecated in favor of bool's must and should clauses. Maybe try rewriting it as a bool query?

Alright, I upgraded to latest Es and translated the query to bool query like you suggested and am making progress!

The the documents in the result set have a matched_queries field such as:

"matched_queries"=>["f1", "f2"]

Now the trick... how to make a dynamic / ad hoc field that I can sort and search on which is the length of that array?

In other words, I want a field named match_queries_count that I can sort and filter on. Perhaps using scripting somehow?

Or can I use the matched_queries field directly in filters (or sort by the cardinality of it)?

Thanks again,
-- C