首先引入包
代码:
建立索引
我现在数据库提取一些数据在里面
//创建的文件路径,没有文件夹会给你自动生成一个
private final static String PATH = "E:\\lucene";
//分词器支持中英文记住自己创建时候的版本不然可能查不到数据
private StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
//IndexWriter(Directory d, Analyzer a, boolean create, MaxFieldLength mfl),第三个参数指定,true表示建立新的索引库或覆盖现有的索引库(删除后重建);false表示使用已有的索引库,如果不存在,就报错。
IndexWriter writer = new IndexWriter(dir,analyzer , true,MaxFieldLength.UNLIMITED);
//循环查询出数据
while(oracleRS.next()){
//创建DOC由于我里面
Document doc1 = new Document();
//Filed这里面有几个参数,第一个设置的字段,第二个是设置该字段的值,第三,第四参见后面
doc1.add(new Field("kid",String.valueOf(oracleRS.getInt("kid")),Field.Store.YES,Index.ANALYZED));
doc1.add(new Field("title",oracleRS.getString("title"),Field.Store.YES,Index.ANALYZED));
doc1.add(new Field("content",oracleRS.getString("content"),Field.Store.YES,Index.ANALYZED));
//写入文件
writer.addDocument(doc1);
}
//合并索引库文件
writer.optimize();
//释放资源
writer.close();
枚举类型 枚举常量 说明
Store NO 不存储属性的值
YES 存储属性的值
Index NO 不建立索引
ANALYZED 分词后建立索引
NOT_ANALYZED 不分词,把整个内容作为一个词建立索引
索引建立好后会在我的D盘上生成一个叫lucene文件夹里面就是我们生成好的索引
查询
public String getIndex(String query)
{
String path = "D:\\lucene" ;
try {
FSDirectory dir = SimpleFSDirectory.open(new File(path));
//读取文件
IndexReader reader = IndexReader.open(dir);
//实例查询对象
IndexSearcher searcher = new IndexSearcher(reader);
//这个是查询哪个字段里面的值 可以是多个或是一个,
String[] fileds = {"title","content"};
//设置分词器版本,查询哪个字段里面的值
QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30, fileds, new StandardAnalyzer(Version.LUCENE_30));
//生成一个Query对象
Query querys = parser.parse(query);
Directory directory = FSDirectory.open(new File(path));
//这里为什么又要生成一个呢,因为我要查一个我查一共多少条数据后面主要分页查询
IndexSearcher indexSearcher = new IndexSearcher(directory);
TopDocs topDocs = indexSearcher.search(querys, 100);
//总条数
int count = topDocs.totalHits;
//共查询多少条数据我是10条
TopScoreDocCollector results = TopScoreDocCollector.create(10, false);
searcher.search(querys, results);
//分页从0条到10条
TopDocs tds = results.topDocs(0, 10);
ScoreDoc[] sd = tds.scoreDocs;
//这个是将查询出来的关键词前后加字符,我的页面的EM标签设置的字体颜色是红色所以在关键词到了页面就变成了红色
Formatter formatter = new SimpleHTMLFormatter("<em>", "</em>");
Scorer scorer = new QueryScorer(querys);
Highlighter highlighter = new Highlighter(formatter, scorer);
//查询多少字
highlighter.setTextFragmenter(new SimpleFragmenter(10000));
//里面就是查询出来的值,循环遍历里面的值
for (int i = 0; i < sd.length; i++) {
ScoreDoc scoreDoc = sd[i];
int docID = scoreDoc.doc;
Document document = indexSearcher.doc(docID);
String txt=highlighter.getBestFragment(new StandardAnalyzer(Version.LUCENE_30), "title", document.get("title"));
String txt2=highlighter.getBestFragment(new StandardAnalyzer(Version.LUCENE_30), "content", document.get("content"));
String txt3=document.get("url");
//获取该字段的值
System.out.println("kid:"+document.get("kid"));
System.out.println("title:"+txt);
System.out.println("content:"+document.get("content"));
System.out.println("url:"+txt3);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
后面的就比较简单了
然后是添加索引
/**
* 大致后上面一样IndexWriter writer = new IndexWriter(dir,analyzer , false,MaxFieldLength.UNLIMITED);
* 把它调成false就好了
* */
public void ss() throws Exception{
Directory dir = new SimpleFSDirectory(new File(PATH));
IndexWriter writer = new IndexWriter(dir,analyzer , false,MaxFieldLength.UNLIMITED);
Document doc1 = new Document();
doc1.add(new Field("kid","4444",Field.Store.YES,Index.ANALYZED));
doc1.add(new Field("title","阿森松岛",Field.Store.YES,Index.ANALYZED));
doc1.add(new Field("content","枫的手",Field.Store.YES,Index.ANALYZED));
doc1.add(new Field("url","阿萨德份额",Field.Store.YES,Index.ANALYZED));
writer.addDocument(doc1);
writer.optimize();
writer.close();
}
删除
public void deleteLucene(String id){
FSDirectory path;
try {
path = SimpleFSDirectory.open(new File("D:\\lucene"));
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter writer = new IndexWriter(path,analyzer,false,MaxFieldLength.UNLIMITED);
writer.setUseCompoundFile(true);
//注意这里最好是填写唯一的不然到时会满足该情况时,数据都删了
writer.deleteDocuments(new Term("kid",id));
writer.optimize();
writer.commit();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
修改
public void updateLucene(String key,String up){
System.out.println("修改");
FSDirectory path;
try {
path = SimpleFSDirectory.open(new File("D:\\lucene"));
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
IndexWriter writer = new IndexWriter(path,analyzer,false,MaxFieldLength.UNLIMITED);
writer.setUseCompoundFile(true);
Document document = new Document();
/**
* 这里特别要说下一定要每个值都设置不然没设置的那个字段会是NULL
* */
document.add(new Field("kid", key, Field.Store.YES,Index.ANALYZED));
document.add(new Field("title", "sssss", Field.Store.YES,Index.ANALYZED));
document.add(new Field("content", "sssss", Field.Store.YES,Index.ANALYZED));
document.add(new Field("url", up, Field.Store.YES,Index.ANALYZED));
//UPDATE方法
writer.updateDocument(new Term("kid", key), document);
//提交
writer.commit();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}