Lucene-索引库的维护

Field域的属性

  是否分析:是否对域的内容进行分词处理,前提是我们要对域的内容进行查询;

  是否索引:将Field分析后的词或整个Field值进行索引,只有索引方可搜索到;

    比如:商品名称,商品简介分析后进行索引;订单号和身份证号不同进行分析但也要索引,这些将来都要作为查询条件;

  是否存储:将Field值存储在文档中,存储在文档中的Field才可以从Documenr中获取;

    比如:商品名称,订单号,凡是将来要从Document中获取的Field都要存储。是否存储的标椎:是否要将内容展示给用户;

  Field类:

    StringField(fieldName,fieldValue,Stroe.YES/NO) 存储的数据类型为字符串,包含索引,是否存储根据Stroe定义,不会经过分析器;

    StroeField(fieldName,fieldValue) 支持多种数据类型,不分析,不建立索引,默认保存到索引库当中;

    LongPoint(name,value) 会进行分析,会创建索引,但是不会保存到索引当中;

    TextField(fieldName,fieldValue,Stroe.YES/NO) 会分析,会创建索引,是否保存取决于Stroe;

    

索引添加

package com.wn.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.StoredField; import org.apache.lucene.document.TextField; 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.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; public class AddDocument { /*添加索引*/
    public static void main(String[] args)throws IOException { Directory directory= FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()); IndexWriterConfig config=new IndexWriterConfig(new IKAnalyzer()); //创建一个indexwriter对象
        IndexWriter indexWriter=new IndexWriter(directory,config); //创建一个Document对象
        org.apache.lucene.document.Document document=new org.apache.lucene.document.Document(); //不同document可以有不同的域,同一个域document可以有相同的域
        document.add(new TextField("fieldName","hhh", Field.Store.YES)); document.add(new TextField("fieldContent","新添加文档的内容hhh",Field.Store.YES)); //LongPoint创建索引
        document.add(new LongPoint("fieldSize",123)); //storeField存储数据
        document.add(new StoredField("fieldSize",123)); //不需要创建索引的就使用storeFiled存储
        document.add(new StoredField("fieldPath","E:\\Lucene\\temp\\hhh.txt")); //添加文档的索引库
 indexWriter.addDocument(document); //关闭indexwriter
 indexWriter.close(); } }

  效果实现:

    

索引删除

  1.删除全部

package com.wn.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; public class DeleteDocument { /*删除全部*/
    public static void main(String[] args)throws IOException { IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer())); //删除索引
 indexWrite.deleteAll(); //关闭资源
 indexWrite.close(); } }

    效果实现

      

  2.根据域或关键词删除

    删除前

      

package com.wn.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; /*根据域和关键词删除*/
public class DeleteDanDocument { public static void main(String[] args)throws IOException { IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer())); //定义一个删除条件,定义一个查询对象
        Query query=new TermQuery(new Term("fieldName","text01.txt")); //删除索引
 indexWrite.deleteDocuments(query); //关闭资源
 indexWrite.close(); } }

    删除后

      

索引修改

  修改前的索引

    

package com.wn.Document; import org.apache.commons.io.FileUtils; import org.apache.lucene.document.*; import org.apache.lucene.index.*; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException;
public class UpdateDocument { /*修改索引库*/
    public static void main(String[] args)throws IOException { IndexWriter indexWrite=new IndexWriter(FSDirectory.open(new File("E:\\Lucene\\temp\\index").toPath()),new IndexWriterConfig(new IKAnalyzer())); //创建文档
        Document document=new Document(); document.add(new TextField("fieldName","hhh", Field.Store.YES)); document.add(new StoredField("fieldPath","c://hhh.txt")); document.add(new LongPoint("fieldSize",456)); document.add(new StoredField("fieldSize",456)); document.add(new TextField("fieldContent","修改fieldName为全文检索的文档,进行文档替换,先删除掉fieldName为全文检索的两个文档,再添加一个fileName为new的新文档", Field.Store.YES)); //修改,参数一:条件 参数二:修改的文档值
        indexWrite.updateDocument(new Term("fieldName","全文"),document); //关闭资源
 indexWrite.close(); } }

   修改后的索引