pathhierarchy-tokenizer-examples.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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,console]
  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. // TESTSETUP
  79. A search for a particular file path string against the text field matches all
  80. the example documents, with Bob's documents ranking highest due to `bob` also
  81. being one of the terms created by the standard analyzer boosting relevance for
  82. Bob's documents.
  83. [source,console]
  84. --------------------------------------------------
  85. GET file-path-test/_search
  86. {
  87. "query": {
  88. "match": {
  89. "file_path": "/User/bob/photos/2017/05"
  90. }
  91. }
  92. }
  93. --------------------------------------------------
  94. It's simple to match or filter documents with file paths that exist within a
  95. particular directory using the `file_path.tree` field.
  96. [source,console]
  97. --------------------------------------------------
  98. GET file-path-test/_search
  99. {
  100. "query": {
  101. "term": {
  102. "file_path.tree": "/User/alice/photos/2017/05/16"
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. With the reverse parameter for this tokenizer, it's also possible to match
  108. from the other end of the file path, such as individual file names or a deep
  109. level subdirectory. The following example shows a search for all files named
  110. `my_photo1.jpg` within any directory via the `file_path.tree_reversed` field
  111. configured to use the reverse parameter in the mapping.
  112. [source,console]
  113. --------------------------------------------------
  114. GET file-path-test/_search
  115. {
  116. "query": {
  117. "term": {
  118. "file_path.tree_reversed": {
  119. "value": "my_photo1.jpg"
  120. }
  121. }
  122. }
  123. }
  124. --------------------------------------------------
  125. Viewing the tokens generated with both forward and reverse is instructive
  126. in showing the tokens created for the same file path value.
  127. [source,console]
  128. --------------------------------------------------
  129. POST file-path-test/_analyze
  130. {
  131. "analyzer": "custom_path_tree",
  132. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  133. }
  134. POST file-path-test/_analyze
  135. {
  136. "analyzer": "custom_path_tree_reversed",
  137. "text": "/User/alice/photos/2017/05/16/my_photo1.jpg"
  138. }
  139. --------------------------------------------------
  140. It's also useful to be able to filter with file paths when combined with other
  141. types of searches, such as this example looking for any files paths with `16`
  142. that also must be in Alice's photo directory.
  143. [source,console]
  144. --------------------------------------------------
  145. GET file-path-test/_search
  146. {
  147. "query": {
  148. "bool" : {
  149. "must" : {
  150. "match" : { "file_path" : "16" }
  151. },
  152. "filter": {
  153. "term" : { "file_path.tree" : "/User/alice" }
  154. }
  155. }
  156. }
  157. }
  158. --------------------------------------------------