# How can I get immediate subdirectories using the path hierarchy tokenizer?

**URL:** <https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099>\
**Category:** Elasticsearch\
**Created:** [May 26, 2021, 5:00pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099 "2021-05-26T17:00:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![F\_eq\_kx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/f_eq_kx/32/62217_2.png) [@F\_eq\_kx](https://discuss.elastic.co/u/F_eq_kx)\
**Post date:** [May 26, 2021, 5:00pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/1 "2021-05-26T17:00:35Z")

</div>

I'm using the [path hierarchy tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html) and, using [the examples](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html#analysis-pathhierarchy-tokenizer-detailed-examples), I would like to give `/User/alice/photos/2017/05` to Elasticsearch and have it list the immediate subdirectories (e.g. `15` and `16`).

How can I do this?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 27, 2021, 6:40am UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/2 "2021-05-27T06:40:49Z")

</div>

I do not think you can get it directly through a query as only paths from the start and not individual path components are indexed, but you should be able to extract it from the returned documents.

---

<div class="post-metadata">

**Author:** ![F\_eq\_kx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/f_eq_kx/32/62217_2.png) [@F\_eq\_kx](https://discuss.elastic.co/u/F_eq_kx)\
**Post date:** [May 27, 2021, 1:10pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/3 "2021-05-27T13:10:40Z")

</div>

The problem is we have billions of documents, and are looking for a way to efficiently recreate directory traversal using ES data.

Would you say we just need to store a `parent_dir` field on each doc or something?

---

<div class="post-metadata">

**Author:** ![F\_eq\_kx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/f_eq_kx/32/62217_2.png) [@F\_eq\_kx](https://discuss.elastic.co/u/F_eq_kx)\
**Post date:** [May 27, 2021, 1:20pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/4 "2021-05-27T13:20:57Z")

</div>

I don't think I'd need each path component. Some more context:

- We're only indexing files, not dirs.
- To get the parent dir, I could ask it for the second last path in the array, since the last path would be the full path including the filename, right?
- I'd just need to as ES to do a kind of terms agg on the second last item in the paths array, and that would group files by their parent dirs, wouldn't it?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 27, 2021, 1:25pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/5 "2021-05-27T13:25:26Z")

</div>

Storing a document for each file with parent directory as a field would likely work well and be efficient. Sounds like a good idea. I do not think you can select terms when using the path tokeniser.

---

<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:** [June 24, 2021, 1:26pm UTC](https://discuss.elastic.co/t/how-can-i-get-immediate-subdirectories-using-the-path-hierarchy-tokenizer/274099/6 "2021-06-24T13:26:04Z")

</div>

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