Lucene 6.0 配置(二)整合mmseg4j分词器

    为了实现更强大的分词功能.我加入了mmseg4j高级分词器,但也是因为网上目前的配置版本过老的缘故..需要重配

    github地址:https://github.com/chenlb/mmseg4j-from-googlecode

    官方博客地址:http://blog.chenlb.com/category/mmseg4j

1.准备条件

pom.xml

        <dependency>  
            <groupId>com.chenlb.mmseg4j</groupId>  
            <artifactId>mmseg4j-solr</artifactId>  
            <version>2.3.0</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.apache.solr</groupId>  
                    <artifactId>solr-core</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  

2、使用Lucene实战

主要代码还是跟上一篇一样的.就是把配置索引对象时所创建的分词器改为mmseg4j
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

import com.chenlb.mmseg4j.analysis.ComplexAnalyzer;

public class LuceneTest {
	static Analyzer analyzer = null;
	static Directory directory = null;
	static String text = "CSDN.NET - 全球最大中文IT社区,为IT专业技术人员提供最全面的信息传播和服务平台";
	static String text1 = "京华时报1月23日报道 昨天,受一股来自中西伯利亚的强冷空气影响,本市出现大风降温天气,白天最高气温只有零下7摄氏度,同时伴有6到7级的偏北风。";

	public static void main(String[] args) throws Exception {
		analyzer = new ComplexAnalyzer();
		directory = new RAMDirectory();
		IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
		iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
		IndexWriter iwriter = new IndexWriter(directory, iwConfig);
		List<String> list = new ArrayList<String>();
		list.add(text);
		list.add(text1);
		for (String item : list) {
			Document doc = new Document();
			doc.add(new TextField("text", item, Field.Store.YES));
			iwriter.addDocument(doc);
		}
		iwriter.close();

		DirectoryReader ireader = DirectoryReader.open(directory);
		IndexSearcher searcher = new IndexSearcher(ireader);
		Query q = new TermQuery(new Term("text", "西伯利亚"));
		System.out.println(q);
		TopDocs tds = searcher.search(q, 10);
		System.out.println("======size:" + tds.totalHits + "========");
		for (ScoreDoc sd : tds.scoreDocs) {
			System.out.println(sd.score);
			System.out.println(searcher.doc(sd.doc).get("text"));
		}
	}
}


运行后便能得到我们所要看到的数据.但在这里要注意的是.查询的值,应该是一个完整的单词,任何意义上的缩写,都不能查询出所要的信息,另外我觉得这个比较鱼的是,在上面这个例子中.当我分别搜寻“全球”,“最大”两个词都能搜的到,但是搜“全球最大”便搜寻不到.这样不能够很精确的搜索.另外这个分词工具可以提供自定义词典,具体实现,请看github或是官网.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值