一、lucene的索引的创建
package com.stx.testlucene; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.IntField; import org.apache.lucene.document.StringField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; public class DelIndex { public int[] id={1,2,3,4,5,6}; public String[] name={"熊战","小黑","VS","猛犸","小小","人吗"}; public String[] type={"天灾","近卫"}; public Directory dir; public String indexPath; public IndexWriter indexWriter; //初始化变量就是索引的存储位置 public DelIndex() throws IOException{ indexPath="E:\\测试lucene\\index"; dir=FSDirectory.open(new File(indexPath)); indexWriter=getIndexWriter(); } //创建索引 @Test public void createIndex() throws IOException{ for(int i=0;i<id.length;i++){ Document doc=new Document(); doc.add(new StringField("name",name[i],Field.Store.YES)); doc.add(new IntField("id",id[i],Field.Store.YES)); if(i%2==0){ doc.add(new StringField("type",type[0],Field.Store.YES)); }else{ doc.add(new StringField("type",type[1],Field.Store.YES)); } indexWriter.addDocument(doc); } indexWriter.close(); } //得到indexWriter对象值 public IndexWriter getIndexWriter() throws IOException{ Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_42, true); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_42, analyzer); return new IndexWriter(dir, iwc); } }
二、lucene索引搜索
@Test public void createSearch() throws IOException{ indexWriter.close(); IndexReader indexReader=DirectoryReader.open(dir); IndexSearcher indexSearch=new IndexSearcher(indexReader); Term term=new Term("type","天灾"); TermQuery termQuery=new TermQuery(term); TopDocs hits=indexSearch.search(termQuery, 3); ScoreDoc[] docs=hits.scoreDocs; System.out.println("总的搜索条数"+hits.totalHits); for(ScoreDoc score:docs){ Document doc=indexSearch.doc(score.doc); System.out.println(doc.get("name")); System.out.println(doc.get("type")); } }
结果
总的搜索条数3 熊战 天灾 VS 天灾 小小 天灾
三、索引的删除操作
全部删除
indexWriter.deleteAll(); 结果肯定是什么也搜不到了
匹配删除
Term term=new Term("type","天灾"); TermQuery termQuery=new TermQuery(term); //把匹配的删除掉 indexWriter.deleteDocuments(term); indexWriter.close(); //结果搜索为0 -------------------------------------------------------------------------------------- 改为搜索 近卫 操作 有记录的
删除信息
IndexReader.maxDoc(); //总文当数 IndexReader.numDeletedDocs();//得到索引删除条数
相关推荐
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...
- **Contrib模块**:包含社区贡献的扩展功能,可能包括特殊分词器、搜索建议等,如`lucene-join-4.7.0.jar`、`lucene-suggest-4.7.0.jar`等。 使用这些JAR包时,开发者需要根据具体需求选择合适的模块,并确保它们...
赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
《Apache Lucene核心技术详解:从2.9.4到3.4.0的演变》 Apache Lucene,作为开源的全文检索库,是Java开发人员进行高效信息检索的重要工具。这个压缩包文件包含了Lucene从2.9.4版本到3.4.0版本的核心组件,让我们...
赠送jar包:lucene-memory-6.6.0.jar; 赠送原API文档:lucene-memory-6.6.0-javadoc.jar; 赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档...
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-suggest-7.7.0.jar; 赠送原API文档:lucene-suggest-7.7.0-javadoc.jar; 赠送源代码:lucene-suggest-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-7.7.0.pom; 包含翻译后的API...
赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-sandbox-7.2.1.jar; 赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API...
赠送jar包:lucene-spatial-6.6.0.jar; 赠送原API文档:lucene-spatial-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-6.6.0.pom; 包含翻译后的API...
赠送jar包:lucene-misc-6.6.0.jar; 赠送原API文档:lucene-misc-6.6.0-javadoc.jar; 赠送源代码:lucene-misc-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-misc-6.6.0.pom; 包含翻译后的API文档:lucene...
这是一个Lucene.net的多索引示例,以数据库的动态数据为数据源,每个表对应一个索引,使用了盘古分词方法,适用于中文的分词,并且实现了增删改查的所有功能。 同时,在查询索引时,适用了分页方法,可直接引用到...
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...