# Ad hoc query time fields?

**URL:** <https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338>\
**Category:** Elasticsearch\
**Created:** [July 25, 2016, 9:31pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338 "2016-07-25T21:31:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjbottaro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjbottaro/32/11055_2.png) [@cjbottaro](https://discuss.elastic.co/u/cjbottaro)\
**Post date:** [July 25, 2016, 9:31pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/1 "2016-07-25T21:31:50Z")

</div>

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...

```auto
# 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.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 25, 2016, 9:45pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/2 "2016-07-25T21:45:59Z")

</div>

Is [named filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html) good enough?

---

<div class="post-metadata">

**Author:** ![cjbottaro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjbottaro/32/11055_2.png) [@cjbottaro](https://discuss.elastic.co/u/cjbottaro)\
**Post date:** [July 25, 2016, 10:11pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/3 "2016-07-25T22:11:47Z")

</div>

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

> <https://github.com/elastic/elasticsearch/issues/6496>

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

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

```

😕

Thanks!

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 25, 2016, 10:24pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/4 "2016-07-25T22:24:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cjbottaro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjbottaro/32/11055_2.png) [@cjbottaro](https://discuss.elastic.co/u/cjbottaro)\
**Post date:** [July 26, 2016, 8:16pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/5 "2016-07-26T20:16:23Z")

</div>

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:

```auto
"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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:32pm UTC](https://discuss.elastic.co/t/ad-hoc-query-time-fields/56338/6 "2017-07-05T22:32:42Z")

</div>


