Lucene實現自訂分詞器(同義字查詢與高亮)

來源:互聯網
上載者:User

標籤:搜尋   全文檢索索引   lucene   

今天我們實現一個簡單的分詞器,僅僅做示範使用功能如下:

1、分詞按照空格、橫杠、點號進行拆分;

2、實現hi與hello的同義字查詢功能;

3、實現hi與hello同義字的高亮顯示;

MyAnalyzer實現代碼:

public class MyAnalyzer extends Analyzer {private int analyzerType;public MyAnalyzer(int type) {super();analyzerType = type;}@Overrideprotected TokenStreamComponents createComponents(String fieldName, Reader reader) {MyTokenizer tokenizer = new MyTokenizer(fieldName, reader, analyzerType);return new TokenStreamComponents(tokenizer);}}

MyTokenizer實現代碼:

public class MyTokenizer extends Tokenizer {public class WordUnit{WordUnit(String word, int start, int length){this.word = word;this.start = start;this.length = length;//System.out.println("\tWordUnit: " + word + "|" + start + "|" + length);}String word;int start;int length;}private int analyzerType;private int endPosition;private Iterator<WordUnit> it;private ArrayList<WordUnit> words;private final CharTermAttribute termAtt;private final OffsetAttribute offsetAtt;public MyTokenizer(String fieldName, Reader in, int type) {super(in);it = null;endPosition = 0;analyzerType = type;offsetAtt = addAttribute(OffsetAttribute.class);termAtt = addAttribute(CharTermAttribute.class);addAttribute(PayloadAttribute.class);}@Overridepublic boolean incrementToken() throws IOException {clearAttributes();char[] inputBuf = new char[1024];if(it == null) {int bufSize = input.read(inputBuf);if(bufSize <= 0) return false;int beginIndex = 0;int endIndex = 0;words = new ArrayList<WordUnit>();for(endIndex = 0; endIndex < bufSize; endIndex++) {if(inputBuf[endIndex] != '-' && inputBuf[endIndex] != ' ' && inputBuf[endIndex] != '.') continue;addWord(inputBuf, beginIndex, endIndex);beginIndex = endIndex + 1;}addWord(inputBuf, beginIndex, endIndex);//add the lastif(words.isEmpty()) return false;it = words.iterator();}if(it != null && it.hasNext()){WordUnit word = it.next();termAtt.append(word.word);termAtt.setLength(word.word.length());endPosition = word.start + word.length;offsetAtt.setOffset(word.start, endPosition);return true;}return false;}@Overridepublic void reset() throws IOException {super.reset();it = null;endPosition = 0;}@Overridepublic final void end() {int finalOffset = correctOffset(this.endPosition);offsetAtt.setOffset(finalOffset, finalOffset);}private void addWord(char[] inputBuf, int begin, int end){if(end <= begin) return;String word = new String(inputBuf, begin, end - begin);words.add(new WordUnit(word, begin, end - begin)); if(analyzerType == 0 && word.equals("hi")) words.add(new WordUnit("hello", begin, 2)); if(analyzerType == 0 && word.equals("hello")) words.add(new WordUnit("hi", begin, 5));}}

索引的時候分詞器類型:analyzerType=0;

搜尋的時候分詞器類型:analyzerType=1;

高亮的時候分詞器類型:analyzerType=0;

搜尋hello時的效果如下:

Score doc 0 hightlight to: look <em>hello</em> onScore doc 1 hightlight to: I am <em>hi</em> China Chinese
可以看到含有hi的文檔也被搜尋出來,同樣也會高亮。

Lucene實現自訂分詞器(同義字查詢與高亮)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.