pathhierarchy-tokenizer-examples.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. [[analysis-pathhierarchy-tokenizer-examples]]
  2. === Path Hierarchy Tokenizer Examples
  3. A common use-case for the `path_hierarchy` tokenizer is filtering results by
  4. file paths. If indexing a file path along with the data, the use of the
  5. `path_hierarchy` tokenizer to analyze the path allows filtering the results
  6. by different parts of the file path string.
  7. This example configures an index to have two custom analyzers and applies
  8. those analyzers to multifields of the `file_path` text field that will
  9. store filenames. One of the two analyzers uses reverse tokenization.
  10. Some sample documents are then indexed to represent some file paths
  11. for photos inside photo folders of two different users.
  12. [source,js]
  13. --------------------------------------------------
  14. PUT file-path-test
  15. {
  16. "settings": {
  17. "analysis": {
  18. "analyzer": {
  19. "custom_path_tree": {
  20. "tokenizer": "custom_hierarchy"
  21. },
  22. "custom_path_tree_reversed": {
  23. "tokenizer": "custom_hierarchy_reversed"
  24. }
  25. },
  26. "tokenizer": {
  27. "custom_hierarchy": {
  28. "type": "path_hierarchy",
  29. "delimiter": "/"
  30. },
  31. "custom_hierarchy_reversed": {
  32. "type": "path_hierarchy",
  33. "delimiter": "/",
  34. "reverse": "true"
  35. }
  36. }
  37. }
  38. },
  39. "mappings": {
  40. "properties": {
  41. "file_path": {
  42. "type": "text",
  43. "fields": {
  44. "tree": {
  45. "type": "text",
  46. "analyzer": "custom_path_tree"
  47. },
  48. "tree_reversed": {
  49. "type": "text",
  50. "analyzer": "custom_path_tree_reversed"
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. POST file-path-test/_doc/1
  58. {
  59. "file_path": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  60. }
  61. POST file-path-test/_doc/2
  62. {
  63. "file_path": "/User/alice/photos/2017/05/16/my_photo2.jpg"
  64. }
  65. POST file-path-test/_doc/3
  66. {
  67. "file_path": "/User/alice/photos/2017/05/16/my_photo3.jpg"
  68. }
  69. POST file-path-test/_doc/4
  70. {
  71. "file_path": "/User/alice/photos/2017/05/15/my_photo1.jpg"
  72. }
  73. POST file-path-test/_doc/5
  74. {
  75. "file_path": "/User/bob/photos/2017/05/16/my_photo1.jpg"
  76. }
  77. --------------------------------------------------
  78. // CONSOLE
  79. // TESTSETUP
  80. A search for a particular file path string against the text field matches all
  81. the example documents, with Bob's documents ranking highest due to `bob` also
  82. being one of the terms created by the standard analyzer boosting relevance for
  83. Bob's documents.
  84. [source,js]
  85. --------------------------------------------------
  86. GET file-path-test/_search
  87. {
  88. "query": {
  89. "match": {
  90. "file_path": "/User/bob/photos/2017/05"
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. // CONSOLE
  96. It's simple to match or filter documents with file paths that exist within a
  97. particular directory using the `file_path.tree` field.
  98. [source,js]
  99. --------------------------------------------------
  100. GET file-path-test/_search
  101. {
  102. "query": {
  103. "term": {
  104. "file_path.tree": "/User/alice/photos/2017/05/16"
  105. }
  106. }
  107. }
  108. --------------------------------------------------
  109. // CONSOLE
  110. With the reverse parameter for this tokenizer, it's also possible to match
  111. from the other end of the file path, such as individual file names or a deep
  112. level subdirectory. The following example shows a search for all files named
  113. `my_photo1.jpg` within any directory via the `file_path.tree_reversed` field
  114. configured to use the reverse parameter in the mapping.
  115. [source,js]
  116. --------------------------------------------------
  117. GET file-path-test/_search
  118. {
  119. "query": {
  120. "term": {
  121. "file_path.tree_reversed": {
  122. "value": "my_photo1.jpg"
  123. }
  124. }
  125. }
  126. }
  127. --------------------------------------------------
  128. // CONSOLE
  129. Viewing the tokens generated with both forward and reverse is instructive
  130. in showing the tokens created for the same file path value.
  131. [source,js]
  132. --------------------------------------------------
  133. POST file-path-test/_analyze
  134. {
  135. "analyzer": "custom_path_tree",
  136. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  137. }
  138. POST file-path-test/_analyze
  139. {
  140. "analyzer": "custom_path_tree_reversed",
  141. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  142. }
  143. --------------------------------------------------
  144. // CONSOLE
  145. It's also useful to be able to filter with file paths when combined with other
  146. types of searches, such as this example looking for any files paths with `16`
  147. that also must be in Alice's photo directory.
  148. [source,js]
  149. --------------------------------------------------
  150. GET file-path-test/_search
  151. {
  152. "query": {
  153. "bool" : {
  154. "must" : {
  155. "match" : { "file_path" : "16" }
  156. },
  157. "filter": {
  158. "term" : { "file_path.tree" : "/User/alice" }
  159. }
  160. }
  161. }
  162. }
  163. --------------------------------------------------
  164. // CONSOLE