一、给索引加权
package com.wsy;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
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.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexUtil {
private String[] ids = {"1", "2", "3", "4", "5", "6"};
private String[] emails = {"a@a.com", "b@b.com", "c@c.com", "d@d.com", "e@e.com", "f@f.com"};
private String[] contents = {"content 1", "content 2", "content 3", "content 4", "content 5", "content 6"};
private int[] attachFiles = {1, 2, 3, 4, 5, 6};
private String[] names = {"qianyi", "zhaoer", "zhangsan", "lisi", "wangwu", "liuliu"};
private Directory directory = null;
private Map<String, Float> scores = new HashMap<>();
public IndexUtil() {
try {
// 设置加权,默认权重是1.0,数值越大权重越高
scores.put("a.com",1.5f);
scores.put("b.com",1.6f);
scores.put("c.com",1.7f);
scores.put("d.com",1.8f);
scores.put("e.com",1.9f);
scores.put("f.com",2.0f);
directory = FSDirectory.open(new File("E:\\Lucene\\IndexLibrary"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void index() {
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document document = null;
for (int i = 0; i < ids.length; i++) {
document = new Document();
document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("content", contents[i], Field.Store.NO, Field.Index.ANALYZED));
document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
String emailType = emails[i].substring(emails[i].lastIndexOf("@")+1);
if(scores.containsKey(emailType)){
document.setBoost(scores.get(emailType));
} else {
document.setBoost(0.1f);
}
indexWriter.addDocument(document);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (indexWriter != null) {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void search() {
try {
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 搜索content域包含“content”的
TermQuery termQuery = new TermQuery(new Term("content", "content"));
TopDocs topDocs = indexSearcher.search(termQuery, 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println(document.get("name") + " " + document.getBoost() + " " + scoreDoc.score);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (indexReader != null) {
try {
indexReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
先将setBoost()语句注释掉,调用一遍index()和search(),记住输出,再将setBoost()还原再去index()和search(),就会发现和刚刚的顺序是不一样的,这时setBoost()就起作用了。可是比较奇怪的是,在加上setBoost()之后,为什么searcher()方法中的输出显示的boost还是1.0呢?是因为index()中的document和search()中的document不是同一个,不行你可以试一下,把document.hashCode()打印出来看一看就知道了。
二、Luke的使用
Luke的版本要和Lucene的版本对应,否则索引是打不开的。
下载之后,放到一个文件夹中,在文件夹中创建一个startup.bat文件,文件内容如下,将luke-3.5.0.jar放到当前文件夹下的target中,以后启动Luke的时候,直接运行这个bat文件就可以了。
start javaw -jar .\target\luke-3.5.0.jar
先看个图,以后慢慢接触到了就会用了。