lucene7以上最低要求jdk1.8

lucene下载地址:

http://archive.apache.org/dist/lucene/java/

<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.0.0</version>
</dependency> <dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>6.0.0</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>6.0.0</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-memory -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-memory</artifactId>
<version>6.0.0</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-smartcn</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
<!--排除掉里面旧的lucene包,因为我们要重写里面的分析器和分词器 -->
<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
package com.ytkj.lucene;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File;
import java.io.IOException; /**
* lucene入门程序
*/
public class LuceneFrist {
/**
* 创建索引库
* @throws Exception
*/
public static void createIndex()throws Exception{
//1.创建一个Director对象,指定索引库保存的位置,保存在磁盘中
Directory directory=FSDirectory.open(new File("E:\\lucene\\lucenetemp").toPath());
//2.创建IndexWriter对象
//创建使用的分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(analyzer));
//3.读取磁盘上的文件,对应每个文件创建一个文档对象
File dir=new File("E:\\lucene\\luceneresource");
File[] files = dir.listFiles();
for (File file : files) {
//文件名称
String name = file.getName();
//文件路径
String path = file.getPath();
//文件内容
String content = FileUtils.readFileToString(file, "utf-8");
//文件大小
long size = FileUtils.sizeOf(file);
//创建域 参数:域的名称 文件名称 是否存储
Field fieldName=new TextField("name",name,Field.Store.YES);
Field fieldPath=new TextField("path",path,Field.Store.YES);
Field fieldContent=new TextField("content",content,Field.Store.YES);
Field fieldSize=new TextField("size",size+"",Field.Store.YES);
//4.创建文档对象
Document document=new Document();
//5.向文档对象中添加域
document.add(fieldName);
document.add(fieldPath);
document.add(fieldContent);
document.add(fieldSize);
//6.把文档对象写入索引库
indexWriter.addDocument(document);
}
//7.关闭indexwriter对象
indexWriter.close();
} /**
* 查询索引库
* @throws Exception
*/
public static void searchIndex() throws Exception {
//1.创建一个Director对象,指定索引库保存的位置
Directory directory=FSDirectory.open(new File("E:\\lucene\\lucenetemp").toPath());
//2.创建indexReader对象
IndexReader indexReader= DirectoryReader.open(directory);
//3.创建indexsearcher对象,构造方法中的参数indexReader对象
IndexSearcher indexSearcher=new IndexSearcher(indexReader);
//4.创建一个query对象
Query query=new TermQuery(new Term("content","spring"));
//5.执行查询,得到一个TopDocs对象 参数:查询对象 查询结果返回的最大记录数
TopDocs topDocs = indexSearcher.search(query, 10);
//6.取查询结果的总记录数
int totalHits = topDocs.totalHits;
System.out.println("查询结果的总记录数:"+totalHits);
//7.获取文档列表
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
//取文档id
int docId = scoreDoc.doc;
//8.根据id获取文档对象
Document document = indexSearcher.doc(docId);
System.out.println(document.get("name"));
System.out.println(document.get("path"));
System.out.println(document.get("content"));
System.out.println(document.get("size"));
}
//关闭创建indexReader对象
indexReader.close(); } /**
* 查看分词器的分词效果
* @throws Exception
*/
public static void testTikenStream() throws Exception {
//创建使用的标准分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
//使用分词器对象的tokenStream方法获取tokenStream对象
TokenStream tokenStream=analyzer.tokenStream("","org.springframework.jdbc.datasource.DataSourceTransactionManager");
//向tokenstream对象中设置一个引用,相当于一个指针
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
//调用tokenstream的rest方法
tokenStream.reset();
//循环遍历tokenStream对象
while (tokenStream.incrementToken()){
System.out.println(charTermAttribute.toString());
}
//关闭
tokenStream.close(); } /**
* 中文分词器测试
*/
public static void testIKAnalyzer() throws Exception {
String etext = "Analysis is one of the main causes of slow indexing. Simply put, the more you analyze the slower analyze the indexing (in most cases).";
String chineseText = "张三说的确实在理。";
/**
* ikanalyzer 中文分词器 因为Analyzer的createComponents方法API改变了 需要我们自己实现
* 分析器IKAnalyzer4Lucene7和分词器IKTokenizer4Lucene7
*/
// IKAnalyzer 细粒度切分
try (Analyzer ik = new IKAnalyzer();) {
TokenStream ts = ik.tokenStream("content", etext);
System.out.println("IKAnalyzer中文分词器 细粒度切分,英文分词效果:");
doToken(ts);
ts = ik.tokenStream("content", chineseText);
System.out.println("IKAnalyzer中文分词器 细粒度切分,中文分词效果:");
doToken(ts);
} // IKAnalyzer 智能切分
try (Analyzer ik = new IKAnalyzer(true);) {
TokenStream ts = ik.tokenStream("content", etext);
System.out.println("IKAnalyzer中文分词器 智能切分,英文分词效果:");
doToken(ts);
ts = ik.tokenStream("content", chineseText);
System.out.println("IKAnalyzer中文分词器 智能切分,中文分词效果:");
doToken(ts);
}
}
private static void doToken(TokenStream ts) throws IOException {
ts.reset();
CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class);
while (ts.incrementToken()) {
System.out.print(cta.toString() + "|");
}
System.out.println();
ts.end();
ts.close();
} public static void main(String[] args) throws Exception {
//createIndex();
//searchIndex();
//testTikenStream();
testIKAnalyzer();
}
}

lucene简单使用的更多相关文章

  1. Lucene 简单API使用

    本demo 简单模拟实现一个图书搜索功能. 模拟向数据库添加数据的时候,添加书籍索引. 提供搜索接口,支持按照书名,作者,内容进行搜索. 按默认规则排序返回搜索结果. Jar依赖: <prope ...

  2. Lucene 简单手记http://www.cnblogs.com/hoojo/archive/2012/09/05/2671678.html

    什么是全文检索与全文检索系统? 全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查 ...

  3. Lucene简单介绍

    [2016.6.11]以前写的笔记,拿出来放到博客里面~ 相关软件: Solr, IK Analyzer, Luke, Nutch;Tomcat; 1.是什么: Lucene是apache软件基金会j ...

  4. lucene简单搜索demo

    方法类 package com.wxf.Test; import com.wxf.pojo.Goods; import org.apache.lucene.analysis.standard.Stan ...

  5. Lucene简单总结

    Lucene API Document Document:文档对象,是一条原始数据 文档编号 文档内容 1 谷歌地图之父跳槽FaceBook 2 谷歌地图之父加盟FaceBook 3 谷歌地图创始人拉 ...

  6. lucene简单使用demo

    测试结构目录: 1.索引库.分词器 Configuration.java package com.test.www.web.lucene; import java.io.File; import or ...

  7. Lucene简单了解和使用

    一,Lucene简介 1 . Lucene  是什么? Lucene 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎, ...

  8. lucene 简单搜索步骤

    1.创建IndexReader实例: Directory dir = FSDirectory.open(new File(indexDir)); IndexReader reader = Direct ...

  9. Lucene入门的基本知识(四)

    刚才在写创建索引和搜索类的时候发现非常多类的概念还不是非常清楚,这里我总结了一下. 1 lucene简单介绍  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不 ...

随机推荐

  1. js对象传到后台ACTION使用

    需的包 <script type="text/javascript" src="s"></script>gson-1.2.3.jar谷歌 ...

  2. 关系型数据库MySQL(二)_索引

    优点 大大加快数据的查询速度 创建唯一性索引,保证数据库表中每一行数据的唯一性 在使用分组和排序子句进行数据检索时,可以显著减少查询中分组和排序的时间 缺点 索引需要占物理空间 当对表中的数据进行增删 ...

  3. 并发数/QPS/PV/ 服务器响应时间公示

    QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...

  4. do_mmap解读

    1: unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, 2: unsigned long len, unsigned ...

  5. asp.net dev xtraReporting(一)静态页面

    新建一个空web应用程序 添加一个default.aspx 添加新建项 建一个空report 写点数据 defalut.aspx中拖拽一个aspxDocumentView 预览可以实现展示了

  6. 页面跳转到Area区域连接

    @Html.ActionLink("主页", "Index", new { controller = "Test", Action = &q ...

  7. SQL数据库—<7>事务、异常和游标

    事务 一.什么是事务能够保证数据的一致性的代码控制,要么执行提交,要么滚回事务的初始状态 二.事务的四大特性:ACIDA:原子性-------事务不可拆开,要么执行要么回滚无中间状态C:一致性---- ...

  8. PostgreSQL的学习-序列

    序列对象(也叫序列生成器)都是用CREATE SEQUENCE创建的特殊的单行表.一个序列对象通常用于为行或者表生成唯一的标识符.下面序列函数,为我们从序列对象中获取最新的序列值提供了简单和并发读取安 ...

  9. java一个数组的内存图

  10. 【Java程序】约瑟夫环

    今天看视频教程无意间看到了一个数3减1的问题,百度之发现叫约瑟夫环问题,于是写了程序,问题大致描述如下: 一群带有编号的孩子手拉手围成一个圈报数,开始的孩子数1,他右边数2,再右边数3,数到n的孩子o ...