SOLR Plugin Development

Source: Internet
Author: User
Tags solr

Scenario Description:

When working with input text, you need to convert a short connection such as Http:// 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;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://". Equals (token))  {                 return  " SOLR ";            } else if  (" http :// ". Equals (token))  {                 return  "";             }        } 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;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.