# Script condition for not matching records

**URL:** <https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293>\
**Category:** Elasticsearch\
**Created:** [February 25, 2022, 3:01pm UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293 "2022-02-25T15:01:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vradder](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vradder](https://discuss.elastic.co/u/vradder)\
**Post date:** [February 25, 2022, 3:01pm UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/1 "2022-02-25T15:01:29Z")

</div>

Hi,

I am selecting records based on range query.

```auto
/_update_by_query?conflicts=proceed
{
	"query":{"range":{"dateTime":{"gte":"xxx","lte":"xxx"}}},
	"script":{"source":"ctx._source.selected = true"}
}

```

how can i unselect rest of the records(means records which doesn't belong to above range)? because there might be previously selected records which may not be in new range.

Thanks

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 26, 2022, 2:46am UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/2 "2022-02-26T02:46:23Z")

</div>

Hi,  
Use Boolean query to negate the range query.

---

<div class="post-metadata">

**Author:** ![vradder](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vradder](https://discuss.elastic.co/u/vradder)\
**Post date:** [February 28, 2022, 3:21am UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/3 "2022-02-28T03:21:27Z")

</div>

Hi,  
Thanks for the response, yes I could do that, but how i can do that in the same request?  
it is possible to have 2 query and 2 respective actions in the same request?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 28, 2022, 4:13am UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/4 "2022-02-28T04:13:49Z")

</div>

Maybe no. is there any problem if you use two requests for the queries? Why you want to.do it in a single request?

---

<div class="post-metadata">

**Author:** ![vradder](https://avatars.discourse-cdn.com/v4/letter/v/898d66/32.png) [@vradder](https://discuss.elastic.co/u/vradder)\
**Post date:** [February 28, 2022, 5:19am UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/5 "2022-02-28T05:19:00Z")

</div>

No problem with making 2 request, thought single request could be faster.  
anyway I am using your "Boolean query to negate the range query" suggestion to make parallel requests, one to unselect and another to select. thanks for answer.

---

<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 28, 2022, 5:19am UTC](https://discuss.elastic.co/t/script-condition-for-not-matching-records/298293/6 "2022-03-28T05:19:04Z")

</div>

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