example-text-analysis-plugin.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. [[example-text-analysis-plugin]]
  2. ==== Example text analysis plugin
  3. This example shows how to create a simple "Hello world" text analysis plugin
  4. using the stable plugin API. The plugin provides a custom Lucene token filter
  5. that strips all tokens except for "hello" and "world".
  6. Elastic provides a Grade plugin, `elasticsearch.stable-esplugin`, that makes it
  7. easier to develop and package stable plugins. The steps in this guide assume you
  8. use this plugin. However, you don't need Gradle to create plugins.
  9. . Create a new directory for your project.
  10. . In this example, the source code is organized under the `main` and
  11. `test` directories. In your project's home directory, create `src/` `src/main/`,
  12. and `src/test/` directories.
  13. . Create the following `build.gradle` build script in your project's home
  14. directory:
  15. +
  16. [source,gradle]
  17. ----
  18. ext.pluginApiVersion = '8.7.0'
  19. ext.luceneVersion = '9.5.0'
  20. buildscript {
  21. ext.pluginApiVersion = '8.7.0'
  22. repositories {
  23. mavenCentral()
  24. }
  25. dependencies {
  26. classpath "org.elasticsearch.gradle:build-tools:${pluginApiVersion}"
  27. }
  28. }
  29. apply plugin: 'elasticsearch.stable-esplugin'
  30. apply plugin: 'elasticsearch.yaml-rest-test'
  31. esplugin {
  32. name 'my-plugin'
  33. description 'My analysis plugin'
  34. }
  35. group 'org.example'
  36. version '1.0-SNAPSHOT'
  37. repositories {
  38. mavenLocal()
  39. mavenCentral()
  40. }
  41. dependencies {
  42. //TODO transitive dependency off and plugin-api dependency?
  43. compileOnly "org.elasticsearch.plugin:elasticsearch-plugin-api:${pluginApiVersion}"
  44. compileOnly "org.elasticsearch.plugin:elasticsearch-plugin-analysis-api:${pluginApiVersion}"
  45. compileOnly "org.apache.lucene:lucene-analysis-common:${luceneVersion}"
  46. //TODO for testing this also have to be declared
  47. testImplementation "org.elasticsearch.plugin:elasticsearch-plugin-api:${pluginApiVersion}"
  48. testImplementation "org.elasticsearch.plugin:elasticsearch-plugin-analysis-api:${pluginApiVersion}"
  49. testImplementation "org.apache.lucene:lucene-analysis-common:${luceneVersion}"
  50. testImplementation ('junit:junit:4.13.2'){
  51. exclude group: 'org.hamcrest'
  52. }
  53. testImplementation 'org.mockito:mockito-core:4.4.0'
  54. testImplementation 'org.hamcrest:hamcrest:2.2'
  55. }
  56. ----
  57. . In `src/main/java/org/example/`, create `HelloWorldTokenFilter.java`. This
  58. file provides the code for a token filter that strips all tokens except for
  59. "hello" and "world":
  60. +
  61. [source,java]
  62. ----
  63. package org.example;
  64. import org.apache.lucene.analysis.FilteringTokenFilter;
  65. import org.apache.lucene.analysis.TokenStream;
  66. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  67. import java.util.Arrays;
  68. public class HelloWorldTokenFilter extends FilteringTokenFilter {
  69. private final CharTermAttribute term = addAttribute(CharTermAttribute.class);
  70. public HelloWorldTokenFilter(TokenStream input) {
  71. super(input);
  72. }
  73. @Override
  74. public boolean accept() {
  75. if (term.length() != 5) return false;
  76. return Arrays.equals(term.buffer(), 0, 4, "hello".toCharArray(), 0, 4)
  77. || Arrays.equals(term.buffer(), 0, 4, "world".toCharArray(), 0, 4);
  78. }
  79. }
  80. ----
  81. . This filter can be provided to Elasticsearch using the following
  82. `HelloWorldTokenFilterFactory.java` factory class. The `@NamedComponent`
  83. annotation is used to give the filter the `hello_world` name. This is the name
  84. you can use to refer to the filter, once the plugin has been deployed.
  85. +
  86. [source,java]
  87. ----
  88. package org.example;
  89. import org.apache.lucene.analysis.TokenStream;
  90. import org.elasticsearch.plugin.analysis.TokenFilterFactory;
  91. import org.elasticsearch.plugin.NamedComponent;
  92. @NamedComponent(value = "hello_world")
  93. public class HelloWorldTokenFilterFactory implements TokenFilterFactory {
  94. @Override
  95. public TokenStream create(TokenStream tokenStream) {
  96. return new HelloWorldTokenFilter(tokenStream);
  97. }
  98. }
  99. ----
  100. . Unit tests may go under the `src/test` directory. You will have to add
  101. dependencies for your preferred testing framework.
  102. . Run:
  103. +
  104. [source,sh]
  105. ----
  106. gradle bundlePlugin
  107. ----
  108. This builds the JAR file, generates the metadata files, and bundles them into a
  109. plugin ZIP file. The resulting ZIP file will be written to the
  110. `build/distributions` directory.
  111. . <<plugin-management,Install the plugin>>.
  112. . You can use the `_analyze` API to verify that the `hello_world` token filter
  113. works as expected:
  114. +
  115. [source,console]
  116. ----
  117. GET /_analyze
  118. {
  119. "text": "hello to everyone except the world",
  120. "tokenizer": "standard",
  121. "filter": ["hello_world"]
  122. }
  123. ----
  124. // TEST[skip:would require this plugin to be installed]
  125. [discrete]
  126. === YAML REST tests
  127. If you are using the `elasticsearch.stable-esplugin` plugin for Gradle, you can
  128. use {es}'s YAML Rest Test framework. This framework allows you to load your
  129. plugin in a running test cluster and issue real REST API queries against it. The
  130. full syntax for this framework is beyond the scope of this tutorial, but there
  131. are many examples in the Elasticsearch repository. Refer to the
  132. {es-repo}tree/main/plugins/examples/stable-analysis[example analysis plugin] in
  133. the {es} Github repository for an example.
  134. . Create a `yamlRestTest` directory in the `src` directory.
  135. . Under the `yamlRestTest` directory, create a `java` folder for Java sources
  136. and a `resources` folder.
  137. . In `src/yamlRestTest/java/org/example/`, create
  138. `HelloWorldPluginClientYamlTestSuiteIT.java`. This class implements
  139. `ESClientYamlSuiteTestCase`.
  140. +
  141. [source,java]
  142. ----
  143. import com.carrotsearch.randomizedtesting.annotations.Name;
  144. import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
  145. import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
  146. import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
  147. public class HelloWorldPluginClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
  148. public HelloWorldPluginClientYamlTestSuiteIT(
  149. @Name("yaml") ClientYamlTestCandidate testCandidate
  150. ) {
  151. super(testCandidate);
  152. }
  153. @ParametersFactory
  154. public static Iterable<Object[]> parameters() throws Exception {
  155. return ESClientYamlSuiteTestCase.createParameters();
  156. }
  157. }
  158. ----
  159. . In `src/yamlRestTest/resources/rest-api-spec/test/plugin`, create the
  160. `10_token_filter.yml` YAML file:
  161. +
  162. [source,yaml]
  163. ----
  164. ## Sample rest test
  165. ---
  166. "Hello world plugin test - removes all tokens except hello and world":
  167. - do:
  168. indices.analyze:
  169. body:
  170. text: hello to everyone except the world
  171. tokenizer: standard
  172. filter:
  173. - type: "hello_world"
  174. - length: { tokens: 2 }
  175. - match: { tokens.0.token: "hello" }
  176. - match: { tokens.1.token: "world" }
  177. ----
  178. . Run the test with:
  179. +
  180. [source,sh]
  181. ----
  182. gradle yamlRestTest
  183. ----