# How to apply x pack ML job on filtered data and group by field

**URL:** <https://discuss.elastic.co/t/how-to-apply-x-pack-ml-job-on-filtered-data-and-group-by-field/119574>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning\
**Created:** [February 13, 2018, 4:41am UTC](https://discuss.elastic.co/t/how-to-apply-x-pack-ml-job-on-filtered-data-and-group-by-field/119574 "2018-02-13T04:41:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarvendra.singh](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sarvendra.singh](https://discuss.elastic.co/u/sarvendra.singh)\
**Post date:** [February 13, 2018, 4:41am UTC](https://discuss.elastic.co/t/how-to-apply-x-pack-ml-job-on-filtered-data-and-group-by-field/119574/1 "2018-02-13T04:41:34Z")

</div>

Hi Team,  
how to apply filter in x pack ML job..  
for example we don't apply to mean(number\_of\_records) on all records only for some values(for example location="US")..also please let us know how we can apply group by some fild in xpack ML job....like calculate mean(no\_of records) group by location

Thanks in advance.

Thanks  
Sarvendra

---

<div class="post-metadata">

**Author:** ![richcollier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/richcollier/32/115035_2.png) [@richcollier](https://discuss.elastic.co/u/richcollier)\
**Post date:** [February 13, 2018, 1:45pm UTC](https://discuss.elastic.co/t/how-to-apply-x-pack-ml-job-on-filtered-data-and-group-by-field/119574/2 "2018-02-13T13:45:34Z")

</div>

Hi,

You have two choices.

1. Build the filtered search in Kibana, save it as a "[Saved search](https://www.elastic.co/guide/en/kibana/6.x/save-open-search.html)". Then when creating an ML job, you can use that Saved search as the basis for the job.
2. When creating an ML job either using the Advanced Job UI or via the API, you can modify the query used in the datafeed to employ elasticsearch DSL - just like the other example we showed you on the scripted fields thread.

---

<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:** [March 13, 2018, 1:45pm UTC](https://discuss.elastic.co/t/how-to-apply-x-pack-ml-job-on-filtered-data-and-group-by-field/119574/3 "2018-03-13T13:45:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
