Scenario Description:
When working with input text, you need to convert a short connection such as Http://bit.ly/3ynriE to a real connection LUCENE.APACHE.ORG/SOLR
1, realize Tokenfilter
package com.url.plugin;import org.apache.lucene.analysis.tokenfilter;import org.apache.lucene.analysis.tokenstream;import org.apache.lucene.analysis.tokenattributes.chartermattribute;import java.io.ioexception;import java.util.regex.pattern;public class resolveurltokenfilter extends tokenfilter { private final chartermattribute chartermattribute=addattribute ( Chartermattribute.class); private final pattern Patterntomatchshortenedurls; public resolveurltokenfilter (TokenStream input, pattern patterntomatchshortenedurls) { super ( Input); this.patterntomatchshortenedurls = patterntomatchshortenedurls; } @Override Public boolean incremenTtoken () throws IOException { if (! Input.incrementtoken ()) return False; //chartermattribute will save read char char[] term=chartermattribute.buffer (); int len=term.length; //Structuring Strings string token=new string (Term,0,len); //match token for the scene we need to refactor if ( Patterntomatchshortenedurls.matcher (token). Matches ()) { chartermattribute.setempty (). Append (Resolveurltoken (token)); } return true; } private string Resolveurltoken (String token) { //TODO Handle token try { according to actual demand if ("Http://bit.ly/3ynriE". Equals (token)) { return "lucene.apache.org/ SOLR "; } else if (" http ://bit.ly/15tzw ". Equals (token)) { return "Manning.com"; } } catch (EXCEPTION EXC) { &nbsP; // rather than failing analysis if you can ' t resolve the url, // you should log the error and return the un-resolved value exc.printstacktrace (); } return token; }}
2, Implementing Tokenfilterfactory
package com.url.plugin;import org.apache.lucene.analysis.tokenfilter;import Org.apache.lucene.analysis.tokenstream;import org.apache.lucene.analysis.util.tokenfilterfactory;import java.util.map;import java.util.regex.pattern;public class resolveurltokenfilterfactory extends TokenFilterFactory { private Pattern Patterntomatchshortenedurls; public resolveurltokenfilterfactory (Map<String, string> args) { super (args); assurematchversion (); // Gets the regular expression information from the configuration file information read by SOLR string shortenedurls=require (args , "Shortenedurlpattern"); patterntomatchshortenedurls= Pattern.compile (shortenedurls); } @Override public tokenfilter create (Tokenstream tokenstream) { //creating Resolveurltokenfilter Instance Objects return new resolveurltokenfilter (Tokenstream,patterntomatchshortenedurls); }}
3, make it into a jar package
4, add the following in the SOLR schema file
5, create the plugin folder under the SOLR root directory (location with the Dist,contrib file) and place the 3 generated jar into it
6, add in Solrconfg.xml
<lib dir= ". /.. /.. /plugins/"regex=". *\.jar "/>
7,java-jar Start.jar
SOLR Plugin Development