1、索引库的维护
1.1、suoy索引库的删除
1.1.1抽取通用的方法
// 抽取公共的方法
private IndexWriter getIndexWriter() throws Exception {
// TODO Auto-generated method stub
Analyzer analyzer = new IKAnalyzer();
//指定索引和文档存储的目录
Directory directory = FSDirectory.open(new File("G://lucene"));
//创建写对象的初始化对象
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
//创建索引和文档写对象 并且返回
return new IndexWriter(directory, config);
}
1.1.2 删除所有 或者针对性的删除
@Test
public void testIndexDel() throws Exception{
//创建分词器,StandardAnalyzer标准分词器,标准分词器对英文分词效果很好,对中文是单字分词
IndexWriter indexWriter = getIndexWriter();
//删除所有
//indexWriter.deleteAll();
//根据名称进行删除
//Term词元,就是一个词, 第一个参数:域名, 第二个参数:要删除含有此关键词的数据
indexWriter.deleteDocuments(new Term("fileName", "apache"));
//提交
indexWriter.commit();
//关闭
indexWriter.close();
}
1.2 索引库的修改
/**
* 更新就是按照传入的Term进行搜索,如果找到结果那么删除,将更新的内容重新生成一个Document对象
* 如果没有搜索到结果,那么将更新的内容直接添加一个新的Document对象
* @throws Exception
*/
@Test
public void testIndexUpdate() throws Exception{
//创建分词器,StandardAnalyzer标准分词器,标准分词器对英文分词效果很好,对中文是单字分词
//创建索引和文档写对象
IndexWriter indexWriter = getIndexWriter();
//根据文件名称进行更新
Term term = new Term("fileName", "web");
//更新的对象
Document doc = new Document();
doc.add(new TextField("fileName", "xxxxxx", Store.YES));
doc.add(new TextField("fileContext", "think in java xxxxxxx", Store.NO));
doc.add(new LongField("fileSize", 100L, Store.YES));
//更新
indexWriter.updateDocument(term, doc);
//提交
indexWriter.commit();
//关闭
indexWriter.close();
}
2、索引库的查询
2.1 TermQuery查询TermQuery,通过项查询,TermQuery不使用分析器所以建议匹配不分词的Field域查询,比如订单号、分类ID号等。
// 使用TermQuery()方法进行查询TermQuery,通过项查询,TermQuery不使用分析器所以建议匹配不分词的Field域查询,比如订单号、分类ID号等。
@Test
public void testIndexTermQuery() throws Exception{
//创建分词器(创建索引和所有时所用的分词器必须一致)
Analyzer analyzer = new IKAnalyzer();
//创建词元:就是词,
Term term = new Term("fileName", "apache");
//使用TermQuery查询,根据term对象进行查询
TermQuery termQuery = new TermQuery(term);
//指定索引和文档的目录
Directory dir = FSDirectory.open(new File("G://lucene"));
//索引和文档的读取对象
IndexReader indexReader = IndexReader.open(dir);
//创建索引的搜索对象
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//搜索:第一个参数为查询语句对象, 第二个参数:指定显示多少条
TopDocs topdocs = indexSearcher.search(termQuery, 5);
//一共搜索到多少条记录
System.out.println("=====count=====" + topdocs.totalHits);
//从搜索结果对象中获取结果集
ScoreDoc[] scoreDocs = topdocs.scoreDocs;
for(ScoreDoc scoreDoc : scoreDocs){
//获取docID
int docID = scoreDoc.doc;
//通过文档ID从硬盘中读取出对应的文档
Document document = indexReader.document(docID);
//get域名可以取出值 打印
System.out.println("fileName:" + document.get("fileName"));
System.out.println("fileSize:" + document.get("fileSize"));
System.out.println("============================================================");
}
}
2.2 NumericRangeQuery 数值范围查询
@Test
public void testNumericRangeQuery() throws Exception{
//创建分词器(创建索引和所有时所用的分词器必须一致)
Analyzer analyzer = new IKAnalyzer();
//根据数字范围查询
//查询文件大小,大于100 小于1000的文章
//第一个参数:域名 第二个参数:最小值, 第三个参数:最大值, 第四个参数:是否包含最小值, 第五个参数:是否包含最大值
Query query = NumericRangeQuery.newLongRange("fileSize", 100L, 1000L, true, true);
//指定索引和文档的目录
Directory dir = FSDirectory.open(new File("G://lucene"));
//索引和文档的读取对象
IndexReader indexReader = IndexReader.open(dir);
//创建索引的搜索对象
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//搜索:第一个参数为查询语句对象, 第二个参数:指定显示多少条
TopDocs topdocs = indexSearcher.search(query, 5);
//一共搜索到多少条记录
System.out.println("=====count=====" + topdocs.totalHits);
//从搜索结果对象中获取结果集
ScoreDoc[] scoreDocs = topdocs.scoreDocs;
for(ScoreDoc scoreDoc : scoreDocs){
//获取docID
int docID = scoreDoc.doc;
//通过文档ID从硬盘中读取出对应的文档
Document document = indexReader.document(docID);
//get域名可以取出值 打印
System.out.println("fileName:" + document.get("fileName"));
System.out.println("fileSize:" + document.get("fileSize"));
System.out.println("============================================================");
}
}
2.3 BooleanQuery 可以组合查询
@Test
public void testBooleanQuery() throws Exception{
//创建分词器(创建索引和所有时所用的分词器必须一致)
Analyzer analyzer = new IKAnalyzer();
//布尔查询,就是可以根据多个条件组合进行查询
//文件名称包含apache的,并且文件大小大于等于100 小于等于1000字节的文章
BooleanQuery query = new BooleanQuery();
//根据数字范围查询
//查询文件大小,大于100 小于1000的文章
//第一个参数:域名 第二个参数:最小值, 第三个参数:最大值, 第四个参数:是否包含最小值, 第五个参数:是否包含最大值
Query numericQuery = NumericRangeQuery.newLongRange("fileSize", 100L, 1000L, true, true);
//创建词元:就是词,
Term term = new Term("fileName", "apache");
//使用TermQuery查询,根据term对象进行查询
TermQuery termQuery = new TermQuery(term);
//Occur是逻辑条件
//must相当于and关键字,是并且的意思
//should,相当于or关键字或者的意思
//must_not相当于not关键字, 非的意思
//注意:单独使用must_not 或者 独自使用must_not没有任何意义
query.add(termQuery, Occur.MUST);
query.add(numericQuery, Occur.MUST);
//指定索引和文档的目录
Directory dir = FSDirectory.open(new File("G://lucene"));
//索引和文档的读取对象
IndexReader indexReader = IndexReader.open(dir);
//创建索引的搜索对象
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//搜索:第一个参数为查询语句对象, 第二个参数:指定显示多少条
TopDocs topdocs = indexSearcher.search(query, 5);
//一共搜索到多少条记录
System.out.println("=====count=====" + topdocs.totalHits);
//从搜索结果对象中获取结果集
ScoreDoc[] scoreDocs = topdocs.scoreDocs;
for(ScoreDoc scoreDoc : scoreDocs){
//获取docID
int docID = scoreDoc.doc;
//通过文档ID从硬盘中读取出对应的文档
Document document = indexReader.document(docID);
//get域名可以取出值 打印
System.out.println("fileName:" + document.get("fileName"));
System.out.println("fileSize:" + document.get("fileSize"));
System.out.println("============================================================");
}
}
2.4 MultiFieldQueryParser 多个领域查询
@Test
public void testMultiFieldQueryParser() throws Exception{
//创建分词器(创建索引和所有时所用的分词器必须一致)
Analyzer analyzer = new IKAnalyzer();
String [] fields = {"fileName","fileContext"};
//从文件名称和文件内容中查询,只有含有apache的就查出来
MultiFieldQueryParser multiQuery = new MultiFieldQueryParser(fields, analyzer);
//输入需要搜索的关键字
Query query = multiQuery.parse("apache");
//指定索引和文档的目录
Directory dir = FSDirectory.open(new File("G://lucene"));
//索引和文档的读取对象
IndexReader indexReader = IndexReader.open(dir);
//创建索引的搜索对象
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//搜索:第一个参数为查询语句对象, 第二个参数:指定显示多少条
TopDocs topdocs = indexSearcher.search(query, 5);
//一共搜索到多少条记录
System.out.println("=====count=====" + topdocs.totalHits);
//从搜索结果对象中获取结果集
ScoreDoc[] scoreDocs = topdocs.scoreDocs;
for(ScoreDoc scoreDoc : scoreDocs){
//获取docID
int docID = scoreDoc.doc;
//通过文档ID从硬盘中读取出对应的文档
Document document = indexReader.document(docID);
//get域名可以取出值 打印
System.out.println("fileName:" + document.get("fileName"));
System.out.println("fileSize:" + document.get("fileSize"));
System.out.println("============================================================");
}
}