码迷,mamicode.com
首页 > Web开发 > 详细

Lucene和Solr学习总结(3)增删改查

时间:2018-11-23 18:35:53      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:opd   file   通过   ring   index   termquery   增删改   位置   创建   

使用IndexSearcher,IndexWriter对象对索引进行增删改查

直接贴代码了,方便日后查看,回顾

public class LuceneManager {

 

private IndexWriter getIndexWriter() throws IOException {

Directory directory = FSDirectory.open(new File("D://lucene//index"));//指定索引库存放位置Directory对象

Analyzer analyzer = new IKAnalyzer();

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);//指定分析器

IndexWriter indexWriter = new IndexWriter(directory, config);//创建IndexWriter

return indexWriter;

}

 

public IndexSearcher getIndexSearcher() throws Exception{

Directory directory = FSDirectory.open(new File("D://lucene//index"));

IndexReader r = DirectoryReader.open(directory);

return new IndexSearcher(r);

}

 

private void printResult(Query query,IndexSearcher indexSearcher) throws IOException {

TopDocs search = indexSearcher.search(query, 10);

for (ScoreDoc scoreDao : search.scoreDocs) {

int doc = scoreDao.doc;

Document document = indexSearcher.doc(doc);

 System.out.println(document.get("fielName"));

           //文件内容

         //  System.out.println(document.get("file_content"));

           //文件大小

           System.out.println(document.get("fileSize"));

           //文件路径

           System.out.println(document.get("filePath"));

           System.out.println("=============");

}

 

}

 

@Test

public void testDeleteAll() throws Exception {

IndexWriter indexWriter = getIndexWriter();

indexWriter.deleteAll();//删除所有

indexWriter.close();

}

//按条件删除

@Test

public void testDelete() throws Exception{

IndexWriter indexWriter = getIndexWriter();

Query query = new TermQuery(new Term("fielName","apache"));

indexWriter.deleteDocuments(query);

indexWriter.close();

}

 

//修改,删掉一个在添加一个

@Test

public void testUpdate() throws Exception{

IndexWriter indexWriter = getIndexWriter();

Document doc = new Document();

doc.add(new TextField("fielName", "测试更新", Store.YES));

//先删,再添加

indexWriter.updateDocument(new Term("fielName","lucene"), doc,new IKAnalyzer());

indexWriter.close();

}

 

/*

 * 使用Query的子类进行查询

 */

//查所有

@Test

public void findAll() throws Exception {

IndexSearcher indexSearcher = getIndexSearcher();

Query query = new MatchAllDocsQuery();

System.out.println(query);

printResult(query, indexSearcher);

//idnexSearch是通过构造创建,里面包含indexReader,可以这样关流

indexSearcher.getIndexReader().close();

}

 

 

//根据数值范围查

@Test

public void findByNumeriRangeQuery() throws Exception{

IndexSearcher indexSearcher = getIndexSearcher();

 

Query query = NumericRangeQuery.newLongRange("fileSize", 0L, 200L, true, false);//是否包含最大和最小值

System.out.println(query);

printResult(query, indexSearcher);

 

//idnexSearch是通过构造创建,里面包含indexReader,可以这样关流

indexSearcher.getIndexReader().close();

}

 

//组合查询 BooleanQuery

@Test

public void testBooleanQuery() throws Exception {

IndexSearcher indexSearcher = getIndexSearcher();

Query query1 = new TermQuery(new Term("fielName","apache"));

Query query2 = new TermQuery(new Term("fielName","lucene"));

 

/*

 * 域值 +fielName:apache fielName:lucene

 * MUST中域值前面带一个+,MUST_NOT是-号,should是空格

 * must相当于and   shuoud相当于or  must_not相当于!(非)

 */

BooleanQuery bq = new BooleanQuery();

bq.add(query1, Occur.MUST_NOT);//必须,相当于and条件

bq.add(query2,Occur.MUST);//应该,

System.out.println(bq);

printResult(bq, indexSearcher);

indexSearcher.getIndexReader().close();

}

 

/*

 * 使用queryparser查询

 * 通过QueryParser也可以创建Query,

 * QueryParser提供一个Parse方法,

 * 此方法可以直接根据查询语法来查询。

 * Query对象执行的查询语法可通过System.out.println(query);

 */

@Test

public void testQueryParse() throws Exception{

IndexSearcher searcher = getIndexSearcher();

//第一个参数默认搜索的域

String f = "fielName";

QueryParser queryParser = new QueryParser(f, new IKAnalyzer());

Query query = queryParser.parse("apache and java");//AND是关键字,会使用语法查询

searcher.search(query, 10);

printResult(query, searcher);

searcher.getIndexReader().close();

 

 

}

 

//指定多个默认域查询 MultiFieldQueryParser

@Test

public void testMultiField() throws Exception {

IndexSearcher indexSearcher = getIndexSearcher();

String[] f = {"fielName","fielContent"};

MultiFieldQueryParser fieldQueryParser = new MultiFieldQueryParser(f, new IKAnalyzer());

Query query = fieldQueryParser.parse("lucene AND lucene");

indexSearcher.search(query, 10);

printResult(query, indexSearcher);

indexSearcher.getIndexReader().close();

}

 

 

}

public class LuceneManager {
private IndexWriter getIndexWriter() throws IOException {Directory directory = FSDirectory.open(new File("D://lucene//index"));//指定索引库存放位置Directory对象Analyzer analyzer = new IKAnalyzer();IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);//指定分析器IndexWriter indexWriter = new IndexWriter(directory, config);//创建IndexWriterreturn indexWriter;}public IndexSearcher getIndexSearcher() throws Exception{Directory directory = FSDirectory.open(new File("D://lucene//index"));IndexReader r = DirectoryReader.open(directory);return new IndexSearcher(r);}private void printResult(Query query,IndexSearcher indexSearcher) throws IOException {TopDocs search = indexSearcher.search(query, 10);for (ScoreDoc scoreDao : search.scoreDocs) {int doc = scoreDao.doc;Document document = indexSearcher.doc(doc); System.out.println(document.get("fielName"));           //文件内容         //  System.out.println(document.get("file_content"));           //文件大小           System.out.println(document.get("fileSize"));           //文件路径           System.out.println(document.get("filePath"));           System.out.println("=============");}}@Testpublic void testDeleteAll() throws Exception {IndexWriter indexWriter = getIndexWriter();indexWriter.deleteAll();//删除所有indexWriter.close();}//按条件删除@Testpublic void testDelete() throws Exception{IndexWriter indexWriter = getIndexWriter();Query query = new TermQuery(new Term("fielName","apache"));indexWriter.deleteDocuments(query);indexWriter.close();}//修改,删掉一个在添加一个@Testpublic void testUpdate() throws Exception{IndexWriter indexWriter = getIndexWriter();Document doc = new Document();doc.add(new TextField("fielName", "测试更新", Store.YES));//先删,再添加indexWriter.updateDocument(new Term("fielName","lucene"), doc,new IKAnalyzer());indexWriter.close();}/* * 使用Query的子类进行查询 *///查所有@Testpublic void findAll() throws Exception {IndexSearcher indexSearcher = getIndexSearcher();Query query = new MatchAllDocsQuery();System.out.println(query);printResult(query, indexSearcher);//idnexSearch是通过构造创建,里面包含indexReader,可以这样关流indexSearcher.getIndexReader().close();}
//根据数值范围查@Testpublic void findByNumeriRangeQuery() throws Exception{IndexSearcher indexSearcher = getIndexSearcher();Query query = NumericRangeQuery.newLongRange("fileSize", 0L, 200L, true, false);//是否包含最大和最小值System.out.println(query);printResult(query, indexSearcher);//idnexSearch是通过构造创建,里面包含indexReader,可以这样关流indexSearcher.getIndexReader().close();}//组合查询 BooleanQuery@Testpublic void testBooleanQuery() throws Exception {IndexSearcher indexSearcher = getIndexSearcher();Query query1 = new TermQuery(new Term("fielName","apache"));Query query2 = new TermQuery(new Term("fielName","lucene"));/* * 域值 +fielName:apache fielName:lucene * MUST中域值前面带一个+,MUST_NOT是-号,should是空格 * must相当于and   shuoud相当于or  must_not相当于!(非) */BooleanQuery bq = new BooleanQuery();bq.add(query1, Occur.MUST_NOT);//必须,相当于and条件bq.add(query2,Occur.MUST);//应该,System.out.println(bq);printResult(bq, indexSearcher);indexSearcher.getIndexReader().close();}/* * 使用queryparser查询 * 通过QueryParser也可以创建Query, * QueryParser提供一个Parse方法, * 此方法可以直接根据查询语法来查询。 * Query对象执行的查询语法可通过System.out.println(query); */@Testpublic void testQueryParse() throws Exception{IndexSearcher searcher = getIndexSearcher();//第一个参数默认搜索的域String f = "fielName";QueryParser queryParser = new QueryParser(f, new IKAnalyzer());Query query = queryParser.parse("apache and java");//AND是关键字,会使用语法查询searcher.search(query, 10);printResult(query, searcher);searcher.getIndexReader().close();}//指定多个默认域查询 MultiFieldQueryParser@Testpublic void testMultiField() throws Exception {IndexSearcher indexSearcher = getIndexSearcher();String[] f = {"fielName","fielContent"};MultiFieldQueryParser fieldQueryParser = new MultiFieldQueryParser(f, new IKAnalyzer());Query query = fieldQueryParser.parse("lucene AND lucene");indexSearcher.search(query, 10);printResult(query, indexSearcher);indexSearcher.getIndexReader().close();}}

Lucene和Solr学习总结(3)增删改查

标签:opd   file   通过   ring   index   termquery   增删改   位置   创建   

原文地址:https://www.cnblogs.com/a-small-lyf/p/10008872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!