# Sequential keyword searching?

**URL:** <https://discuss.elastic.co/t/sequential-keyword-searching/48479>\
**Category:** Elasticsearch\
**Created:** [April 26, 2016, 9:12pm UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479 "2016-04-26T21:12:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgeorge](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tgeorge](https://discuss.elastic.co/u/tgeorge)\
**Post date:** [April 26, 2016, 9:12pm UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/1 "2016-04-26T21:12:12Z")

</div>

Is it possible with ElasticSearch to search for a keyword or phrase AFTER a different keyword has been found?

for example, document with field "text" has value of "this is a test document".

I need to be able to find it based on "this_document" or "this_test".

---

<div class="post-metadata">

**Author:** ![softwaredoug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/softwaredoug/32/22681_2.png) [@softwaredoug](https://discuss.elastic.co/u/softwaredoug)\
**Post date:** [April 26, 2016, 11:14pm UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/2 "2016-04-26T23:14:47Z")

</div>

Do span queries fit the bill for your use case?

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html)

---

<div class="post-metadata">

**Author:** ![tgeorge](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tgeorge](https://discuss.elastic.co/u/tgeorge)\
**Post date:** [April 27, 2016, 1:30am UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/3 "2016-04-27T01:30:04Z")

</div>

span\_near query got me really really close, but the problem I was running into was that phrases were not working?

the following was as close as I was getting

```
                            foreach($arrKeywords[$i] as $strKeyword)
				{
					$arrClauses[] = ['span_term' => ['transcript' => '"'.$strKeyword.'"']];
				}

				$objQuery->query->bool->should[]->span_near = [
				    'clauses' => $arrClauses,
				    'slop' => 100000000,
				    'in_order' => true
				];
```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 27, 2016, 4:01am UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/4 "2016-04-27T04:01:29Z")

</div>

I know this won't solve your solution, but that slop value is a bit hectic.

---

<div class="post-metadata">

**Author:** ![tgeorge](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tgeorge](https://discuss.elastic.co/u/tgeorge)\
**Post date:** [April 27, 2016, 12:44pm UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/5 "2016-04-27T12:44:12Z")

</div>

I agree Walkom, I am trying to accomplish a task without the right tool it feels.

I want to find a keyword in a block of text and then search for another keyword that only happens after the previous keyword was found. This was the closest solution I could come up with but phrases are not working, only terms.

---

<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:55pm UTC](https://discuss.elastic.co/t/sequential-keyword-searching/48479/6 "2017-07-05T22:55:59Z")

</div>


