# Using field from query results as new search term

**URL:** https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816
**Category:** Elasticsearch
**Created:** [October 25, 2016, 2:01am UTC](https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816 "2016-10-25T02:01:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Geezer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@Geezer](https://discuss.elastic.co/u/Geezer)
#### Post date: [October 25, 2016, 2:01am UTC](https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816/1 "2016-10-25T02:01:41Z")

</div>

Hello

I have logs of Windows events which show users who add themselves temporarily to an admin group. I need a query to find users who have added themselves to an admin group but not removed themselves afterwards.

So, we first search on one event ID, and take the username from the results. We then search the same index for events containing those users and the second event ID. We subtract these results from the first set, so it leaves those users from the first results set who added themseves to a group but did not then remove themselves.

Is this possible?

Thank you

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [October 25, 2016, 9:33am UTC](https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816/2 "2016-10-25T09:33:48Z")

</div>

You could use aggregations to group a summary of related events for each user on-the-fly e.g. this simplified example would count the number of logins vs logouts for each user:

```
DELETE test
PUT test
{
	"settings": {
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings": {
		"doc": {
			"properties": {
				"user": {
					"type": "keyword"
				},
				"event": {
					"type": "keyword"
				}
			}
		}
	}
}
POST test/doc
{
	"user":"mark",
	"event":"login"
}
POST test/doc
{
	"user":"mark",
	"event":"logout"
}
POST test/doc
{
	"user":"dave",
	"event":"login"
}
GET test/doc/_search
{
	"size":0,
	"aggs":
	{
		"users":{
			"terms":{
				"field":"user"
			},
			"aggs":{
				"logins":{
					"terms":{
						"field":"event"                        
					}
				}             
			}
		}
	}
}

```

You would then need to post-process the results in your client to spot the behaviours you are interested in. However, this may require a lot of memory if you have a lot of unique users. This sort of behavioural analysis is often best tackled by maintaining an entity centric index alongside your event centric index. See here for discussion and example design/scripts [https://www.youtube.com/watch?v=yBf7oeJKH2Y](https://www.youtube.com/watch?v=yBf7oeJKH2Y)

---

<div class="post-metadata">

### Author: ![Geezer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@Geezer](https://discuss.elastic.co/u/Geezer)
#### Post date: [October 26, 2016, 2:47pm UTC](https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816/3 "2016-10-26T14:47:19Z")

</div>

Thank you, I will take a look at the videos you suggest

---

<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:09pm UTC](https://discuss.elastic.co/t/using-field-from-query-results-as-new-search-term/63816/4 "2017-07-05T22:09:25Z")

</div>


