cjbottaro
(Christopher J. Bottaro)
July 25, 2016, 9:31pm
1
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.
nik9000
(Nik Everett)
July 25, 2016, 9:45pm
2
Is named filter good enough?
cjbottaro
(Christopher J. Bottaro)
July 25, 2016, 10:11pm
3
Maybe! But I'm using a really old version of Es (1.2.1) and I'm running into this issue:
See http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/search-request-named-queries-and-filters.html, it says:
Each filter and query can accept a _name in its top level definition.
However, not all queries/filters can accept...
:Query DSL
adoptme
bug
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"}] }
Thanks!
nik9000
(Nik Everett)
July 25, 2016, 10:24pm
4
or
and and
are pretty deprecated in favor of bool
's must
and should
clauses. Maybe try rewriting it as a bool
query?
cjbottaro
(Christopher J. Bottaro)
July 26, 2016, 8:16pm
5
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