# Best way to implement terms query for analyzed field

**URL:** <https://discuss.elastic.co/t/best-way-to-implement-terms-query-for-analyzed-field/161088>\
**Category:** Elasticsearch\
**Created:** [December 17, 2018, 8:00am UTC](https://discuss.elastic.co/t/best-way-to-implement-terms-query-for-analyzed-field/161088 "2018-12-17T08:00:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_es](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_es/32/53584_2.png) [@tom\_es](https://discuss.elastic.co/u/tom_es)\
**Post date:** [December 17, 2018, 8:00am UTC](https://discuss.elastic.co/t/best-way-to-implement-terms-query-for-analyzed-field/161088/1 "2018-12-17T08:00:31Z")

</div>

Hi,  
I have a document that contains a text field with a hierarchy path

> - doc {
> - path: "some/long/path/to/file.txt"  
> }

now, my requirements are that I will be able to query all the documents that their path field ends with \< **some path** \>.

for example, I want to supply a list of partial paths (to be more exact, suffixes of paths), and I want to get all the documents that their path ends with one of the suffixes.

If I should have search an exact matches, it looks like at terms query would fit, but from what I have read, it's only for not analyzed fields, and in my case, I don't see a way out or analyzing the path.

I have read about [path hierarchy](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html) analyzer , and it looks like it can help by using "reverse" property of the tokenizer.. but it's still not solve the "terms" query requirements  
(it's crucial for us to query all documents in one bulk, because there could be a lot, so a loop is not an option here..

and suggestion for this scenario?

Thanks

---

<div class="post-metadata">

**Author:** ![tom\_es](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_es/32/53584_2.png) [@tom\_es](https://discuss.elastic.co/u/tom_es)\
**Post date:** [December 18, 2018, 8:10am UTC](https://discuss.elastic.co/t/best-way-to-implement-terms-query-for-analyzed-field/161088/2 "2018-12-18T08:10:04Z")

</div>

update -  
it looks like terms query does work with analyzed field.  
used path\_hierarchy tokenizer + reverse mode and it seems to work.

---

<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:** [January 15, 2019, 8:10am UTC](https://discuss.elastic.co/t/best-way-to-implement-terms-query-for-analyzed-field/161088/3 "2019-01-15T08:10:04Z")

</div>

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