Lucene和Solr学习总结增删改查

Posted a-small-lyf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucene和Solr学习总结增删改查相关的知识,希望对你有一定的参考价值。

使用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();}//组合查询 [email protected] 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();}//指定多个默认域查询 [email protected] 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学习总结增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Solr配置中文分词器IKAnalyzer及增删改查调用

Mybatis学习总结—使用接口实现数据的增删改查

Lucene和Solr学习总结

MyBatis 学习总结 02 对表执行增删改查(CRUD)操作 OLD

Elasticsearch总结笔记

5月11日 python学习总结 子查询pymysql模块增删改查防止sql注入问题