pathhierarchy-tokenizer.asciidoc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. [[analysis-pathhierarchy-tokenizer]]
  2. === Path hierarchy tokenizer
  3. ++++
  4. <titleabbrev>Path hierarchy</titleabbrev>
  5. ++++
  6. The `path_hierarchy` tokenizer takes a hierarchical value like a filesystem
  7. path, splits on the path separator, and emits a term for each component in the
  8. tree.
  9. [float]
  10. === Example output
  11. [source,console]
  12. ---------------------------
  13. POST _analyze
  14. {
  15. "tokenizer": "path_hierarchy",
  16. "text": "/one/two/three"
  17. }
  18. ---------------------------
  19. /////////////////////
  20. [source,console-result]
  21. ----------------------------
  22. {
  23. "tokens": [
  24. {
  25. "token": "/one",
  26. "start_offset": 0,
  27. "end_offset": 4,
  28. "type": "word",
  29. "position": 0
  30. },
  31. {
  32. "token": "/one/two",
  33. "start_offset": 0,
  34. "end_offset": 8,
  35. "type": "word",
  36. "position": 0
  37. },
  38. {
  39. "token": "/one/two/three",
  40. "start_offset": 0,
  41. "end_offset": 14,
  42. "type": "word",
  43. "position": 0
  44. }
  45. ]
  46. }
  47. ----------------------------
  48. /////////////////////
  49. The above text would produce the following terms:
  50. [source,text]
  51. ---------------------------
  52. [ /one, /one/two, /one/two/three ]
  53. ---------------------------
  54. [float]
  55. === Configuration
  56. The `path_hierarchy` tokenizer accepts the following parameters:
  57. [horizontal]
  58. `delimiter`::
  59. The character to use as the path separator. Defaults to `/`.
  60. `replacement`::
  61. An optional replacement character to use for the delimiter.
  62. Defaults to the `delimiter`.
  63. `buffer_size`::
  64. The number of characters read into the term buffer in a single pass.
  65. Defaults to `1024`. The term buffer will grow by this size until all the
  66. text has been consumed. It is advisable not to change this setting.
  67. `reverse`::
  68. If set to `true`, emits the tokens in reverse order. Defaults to `false`.
  69. `skip`::
  70. The number of initial tokens to skip. Defaults to `0`.
  71. [float]
  72. === Example configuration
  73. In this example, we configure the `path_hierarchy` tokenizer to split on `-`
  74. characters, and to replace them with `/`. The first two tokens are skipped:
  75. [source,console]
  76. ----------------------------
  77. PUT my_index
  78. {
  79. "settings": {
  80. "analysis": {
  81. "analyzer": {
  82. "my_analyzer": {
  83. "tokenizer": "my_tokenizer"
  84. }
  85. },
  86. "tokenizer": {
  87. "my_tokenizer": {
  88. "type": "path_hierarchy",
  89. "delimiter": "-",
  90. "replacement": "/",
  91. "skip": 2
  92. }
  93. }
  94. }
  95. }
  96. }
  97. POST my_index/_analyze
  98. {
  99. "analyzer": "my_analyzer",
  100. "text": "one-two-three-four-five"
  101. }
  102. ----------------------------
  103. /////////////////////
  104. [source,console-result]
  105. ----------------------------
  106. {
  107. "tokens": [
  108. {
  109. "token": "/three",
  110. "start_offset": 7,
  111. "end_offset": 13,
  112. "type": "word",
  113. "position": 0
  114. },
  115. {
  116. "token": "/three/four",
  117. "start_offset": 7,
  118. "end_offset": 18,
  119. "type": "word",
  120. "position": 0
  121. },
  122. {
  123. "token": "/three/four/five",
  124. "start_offset": 7,
  125. "end_offset": 23,
  126. "type": "word",
  127. "position": 0
  128. }
  129. ]
  130. }
  131. ----------------------------
  132. /////////////////////
  133. The above example produces the following terms:
  134. [source,text]
  135. ---------------------------
  136. [ /three, /three/four, /three/four/five ]
  137. ---------------------------
  138. If we were to set `reverse` to `true`, it would produce the following:
  139. [source,text]
  140. ---------------------------
  141. [ one/two/three/, two/three/, three/ ]
  142. ---------------------------
  143. [discrete]
  144. [[analysis-pathhierarchy-tokenizer-detailed-examples]]
  145. === Detailed examples
  146. A common use-case for the `path_hierarchy` tokenizer is filtering results by
  147. file paths. If indexing a file path along with the data, the use of the
  148. `path_hierarchy` tokenizer to analyze the path allows filtering the results
  149. by different parts of the file path string.
  150. This example configures an index to have two custom analyzers and applies
  151. those analyzers to multifields of the `file_path` text field that will
  152. store filenames. One of the two analyzers uses reverse tokenization.
  153. Some sample documents are then indexed to represent some file paths
  154. for photos inside photo folders of two different users.
  155. [source,console]
  156. --------------------------------------------------
  157. PUT file-path-test
  158. {
  159. "settings": {
  160. "analysis": {
  161. "analyzer": {
  162. "custom_path_tree": {
  163. "tokenizer": "custom_hierarchy"
  164. },
  165. "custom_path_tree_reversed": {
  166. "tokenizer": "custom_hierarchy_reversed"
  167. }
  168. },
  169. "tokenizer": {
  170. "custom_hierarchy": {
  171. "type": "path_hierarchy",
  172. "delimiter": "/"
  173. },
  174. "custom_hierarchy_reversed": {
  175. "type": "path_hierarchy",
  176. "delimiter": "/",
  177. "reverse": "true"
  178. }
  179. }
  180. }
  181. },
  182. "mappings": {
  183. "properties": {
  184. "file_path": {
  185. "type": "text",
  186. "fields": {
  187. "tree": {
  188. "type": "text",
  189. "analyzer": "custom_path_tree"
  190. },
  191. "tree_reversed": {
  192. "type": "text",
  193. "analyzer": "custom_path_tree_reversed"
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. POST file-path-test/_doc/1
  201. {
  202. "file_path": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  203. }
  204. POST file-path-test/_doc/2
  205. {
  206. "file_path": "/User/alice/photos/2017/05/16/my_photo2.jpg"
  207. }
  208. POST file-path-test/_doc/3
  209. {
  210. "file_path": "/User/alice/photos/2017/05/16/my_photo3.jpg"
  211. }
  212. POST file-path-test/_doc/4
  213. {
  214. "file_path": "/User/alice/photos/2017/05/15/my_photo1.jpg"
  215. }
  216. POST file-path-test/_doc/5
  217. {
  218. "file_path": "/User/bob/photos/2017/05/16/my_photo1.jpg"
  219. }
  220. --------------------------------------------------
  221. A search for a particular file path string against the text field matches all
  222. the example documents, with Bob's documents ranking highest due to `bob` also
  223. being one of the terms created by the standard analyzer boosting relevance for
  224. Bob's documents.
  225. [source,console]
  226. --------------------------------------------------
  227. GET file-path-test/_search
  228. {
  229. "query": {
  230. "match": {
  231. "file_path": "/User/bob/photos/2017/05"
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. // TEST[continued]
  237. It's simple to match or filter documents with file paths that exist within a
  238. particular directory using the `file_path.tree` field.
  239. [source,console]
  240. --------------------------------------------------
  241. GET file-path-test/_search
  242. {
  243. "query": {
  244. "term": {
  245. "file_path.tree": "/User/alice/photos/2017/05/16"
  246. }
  247. }
  248. }
  249. --------------------------------------------------
  250. // TEST[continued]
  251. With the reverse parameter for this tokenizer, it's also possible to match
  252. from the other end of the file path, such as individual file names or a deep
  253. level subdirectory. The following example shows a search for all files named
  254. `my_photo1.jpg` within any directory via the `file_path.tree_reversed` field
  255. configured to use the reverse parameter in the mapping.
  256. [source,console]
  257. --------------------------------------------------
  258. GET file-path-test/_search
  259. {
  260. "query": {
  261. "term": {
  262. "file_path.tree_reversed": {
  263. "value": "my_photo1.jpg"
  264. }
  265. }
  266. }
  267. }
  268. --------------------------------------------------
  269. // TEST[continued]
  270. Viewing the tokens generated with both forward and reverse is instructive
  271. in showing the tokens created for the same file path value.
  272. [source,console]
  273. --------------------------------------------------
  274. POST file-path-test/_analyze
  275. {
  276. "analyzer": "custom_path_tree",
  277. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  278. }
  279. POST file-path-test/_analyze
  280. {
  281. "analyzer": "custom_path_tree_reversed",
  282. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  283. }
  284. --------------------------------------------------
  285. // TEST[continued]
  286. It's also useful to be able to filter with file paths when combined with other
  287. types of searches, such as this example looking for any files paths with `16`
  288. that also must be in Alice's photo directory.
  289. [source,console]
  290. --------------------------------------------------
  291. GET file-path-test/_search
  292. {
  293. "query": {
  294. "bool" : {
  295. "must" : {
  296. "match" : { "file_path" : "16" }
  297. },
  298. "filter": {
  299. "term" : { "file_path.tree" : "/User/alice" }
  300. }
  301. }
  302. }
  303. }
  304. --------------------------------------------------
  305. // TEST[continued]