Path Hierarchy Tokenization - Exclude results below certain depth

Hi all,

I'm attempting to search for all items at the current path level of my search and nothing lower. I have created an index like so:

{
	"settings": {
		"analysis": {
			"analyzer": {
				"occupation_hierarchy_analyzer": {
					"tokenizer": "path_hierarchy"
				}
			}
		}
	},
	"mappings": {
		"category": {
			"properties": {
				"path": {
					"type": "text",
					"analyzer": "occupation_hierarchy_analyzer"
				}
			}
		}
	}
}

Then I'm adding a set of categories with increasing depth:

{"index": {"_index": "occupations", "_type": "category"}}
{"path": "/","name": "Business, finance and administration occupations"}
{"index": {"_index": "occupations", "_type": "category"}}
{"path": "/Business, finance and administration occupations","name": "Senior financial administrator"}
{"index": {"_index": "occupations", "_type": "category"}}
{"path": "/Business, finance and administration occupations/Senior financial administrator","name": "Senior finance operator"}
{"index": {"_index": "occupations", "_type": "category"}}
{"path": "/Business, finance and administration occupations/Senior financial administrator/Senior finance operator","name": "Senior bank manager"}

Now my query looks like so:

{
  "query": {
  	"bool": {
  	  	"filter": {
  			"term":{
  				"path": "/Business, finance and administration occupations"
  			}
  		}	
  	}
  }
}

This query returns all documents within that path and it's sub-paths. What I'd like is just the documents within the given path and none from a lower depth. I've searched all around, but I have yet to see a way to do this. I read documentation here: https://www.elastic.co/guide/en/elasticsearch/guide/master/denormalization-concurrency.html, which seemed to indicate the above query would only search for documents at that depth.

Any insight is greatly appreciated!

Maybe I'm missing something, but if you only want to find documents with the exact path, then why use the path_hierarchy tokenizer? If you create the index with path mapped as type keyword (without an analyzer) instead, you will only find documents that match the exact path.

Hi Abdon,

Thank you, I'm brand new to Elastic Search. I tried doing what you suggested and it works exactly the way I want now. Thanks so much for the reply!

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