Skip to content

Commit 0efac96

Browse files
authored
Merge pull request #1678 from vespa-engine/arnej/add-token-filter-factory-example
add working example of building your own lucene-linguistics component
2 parents d085b2e + f9552ae commit 0efac96

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Vespa Lucene Linguistics: Adding custom Lucene components
2+
3+
## TL;DR
4+
5+
Shows how you can use an extended version of the lucene-linguistics
6+
component which includes your own custom components, discoverable
7+
using the classpath as expected by Lucene SPI mechanism.
8+
9+
## Details
10+
11+
The included pom.xml builds your own component called
12+
"my-replacement-bundle" which includes a dummy implementation of
13+
TokenFilterFactory. The class name of the factory must be in
14+
META-INF/services/org.apache.lucene.analysis.TokenFilterFactory
15+
from src/main/resources/ directory.
16+
17+
After running "mvn install", you can copy the finished
18+
target/my-replacement-bundle-1.0.1-deploy.jar to the
19+
"components" directory in your application package.
20+
Put the snippet from "services.xml" into your services.xml
21+
in your application as well to activate it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!-- Copyright Vespa.ai. All rights reserved. -->
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>ai.vespa.test</groupId>
7+
<artifactId>my-replacement-bundle</artifactId>
8+
<version>1.0.1</version>
9+
<packaging>container-plugin</packaging>
10+
11+
<properties>
12+
<!-- Find latest version at search.maven.org/search?q=g:com.yahoo.vespa -->
13+
<vespa.version>8.499.20</vespa.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.yahoo.vespa</groupId>
19+
<artifactId>container</artifactId>
20+
<version>${vespa.version}</version>
21+
<scope>provided</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.yahoo.vespa</groupId>
25+
<artifactId>lucene-linguistics</artifactId>
26+
<version>${vespa.version}</version>
27+
<scope>compile</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>com.yahoo.vespa</groupId>
35+
<artifactId>bundle-plugin</artifactId>
36+
<version>${vespa.version}</version>
37+
<extensions>true</extensions>
38+
<configuration>
39+
<failOnWarnings>true</failOnWarnings>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- use the below instead of component with bundle="lucene-linguistics" -->
2+
3+
<component id="com.yahoo.language.lucene.LuceneLinguistics" bundle="my-replacement-bundle">
4+
<config name="com.yahoo.language.lucene.lucene-analysis" >
5+
<configDir>analysis-config</configDir>
6+
<analysis>
7+
<item key="en">
8+
<tokenizer>
9+
<name>whitespace</name>
10+
</tokenizer>
11+
<tokenFilters>
12+
<item>
13+
<name>myFilterFactory</name>
14+
</item>
15+
</tokenFilters>
16+
</item>
17+
</analysis>
18+
</config>
19+
</component>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ai.vespa.test;
2+
3+
import org.apache.lucene.analysis.TokenStream;
4+
import org.apache.lucene.analysis.TokenFilterFactory;
5+
6+
import java.util.Map;
7+
8+
public class MyFilterFactory extends TokenFilterFactory {
9+
10+
public static final String NAME = "myFilterFactory";
11+
12+
// not actually used, but must be present:
13+
public MyFilterFactory() {
14+
throw defaultCtorException();
15+
}
16+
17+
public MyFilterFactory(Map<String, String> config) {
18+
super(config);
19+
// probably plug in some configuration code here
20+
System.err.println("Constructed: " + this.getClass());
21+
}
22+
23+
public TokenStream create(TokenStream input) {
24+
System.err.println("create called for: " + this.getClass());
25+
// actually create your TokenFilter and return that here:
26+
return input;
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ai.vespa.test.MyFilterFactory

0 commit comments

Comments
 (0)