2、索引的增删改查、加权、luke(lucene笔记)

一、创建索引

这里我们先给出相关类方法(工程lucene_index
IndexUtil.java

package cn.lucene.index;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class IndexUtil {
    private String[] ids = {"1", "2", "3", "4", "5", "6"};
    //下面是邮件
    private String[] emails = {"aa@qq.com", "bb@sina.edu", "cc@yahu.org", "ss@sina.com", "dd@gmail.com", "ee@163.com"};
    //下面是邮件内容
    private String[] content = {
            "welcom to visited the space", 
            "hello boy", 
            "come on baby", 
            "first blood", 
            "I like football", 
            "my girlfriend is so beatiful"
    };
    private int[] attaches = {2,5,6,5,8,4};//附件数量
    //发件人名字
    private String[] names = {"Tom", "Jack", "goudan", "alibaba", "jerry", "kitty"};
    
    private Directory directory = null;
    
    public IndexUtil() {
        try {
            directory = FSDirectory.open(new File("E:/myeclipse/Lucene/index"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建索引
    public void index(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            //此方法可将索引全部清空
            writer.deleteAll();
            Document document = null;
            for(int i = 0; i < ids.length; i++){
                document = new Document();
                //id需要存储,不需要加权、分词,email也需要存储,但不需要分词,有时候也需要加权
                //对于内容,我们不需要存储和加权,但需要分词。而名字需要存储,不需要分词和加权
                //这里我们先不对整型数据进行索引,后面再说
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", content[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                writer.addDocument(document);
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

说明:

  • 1.这里我们只是对idemail、内容、名字创建索引,对于整型数据的索引后面再讲。对于创建索引的过程我们可以联系数据库的创建过程。
  • 2.使用FSDirectory.open方法创建一个索引存放的目录
  • 3.创建一个Document对象,这个对象就像数据库中的字段一样
  • 4.将每个要创建索引的内容(比如id、内容等)都添加到Document对象中,就像我们往数据库中的某个表中的字段添加数据一样。
  • 5.当然我们要将相关的索引创建出来,当然需要借助IndexWriter 对象,此对象用于存储索引。注意,用完之后需要关闭此对象。
  • 6.这里我们还可以使用方法writer.deleteAll()将相关索引全部清空。

现在我们对此方法进行测试(TestIndex.java

@Test
public void testIndex(){
    IndexUtil util = new IndexUtil();
    util.index();
}

说明:

  • 1.创建完之后我们发现存储目录中多出许多段。我们对段先作一个简要介绍。
  • 2..fnm保存的是域选项的信息;.fdt.fdx中保存的是Store.YES中的数据;.frq保存的是哪些单词出现了多少次;.nrm存储评分信息(加权信息);.prx是偏移量;.tii.tis存储索引中的所有内容信息。
    1

二、查询索引

相关方法:

public void query(){
    try {
        IndexReader reader = IndexReader.open(directory);
        //maxDoc 和 numDocs()方法的区别:maxDoc()返回索引中删除和未被删除的文档总数,
        //后者返回索引中未被删除的文档总数,通过reader可以有效获取文档的数量
        System.out.println("numDocs: " + reader.numDocs());
        System.out.println("maxDocs: " + reader.maxDoc());
        //查看被删除的索引
        System.out.println("deleteDocs : " + reader.numDeletedDocs());
        //记得用完之后关闭
        reader.close();
        
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

说明:

  • 1.查询时使用的是IndexReader类,我们可以使用此类中的相关方法得到有关索引的信息。
  • 2.特别注意maxDocnumDocs()方法的区别:maxDoc()返回索引中删除和未被删除的文档总数,后者返回索引中未被删除的文档总数,因为我们删除一个索引之后并不是真正的删除,而是放在了回收站,当然这里的回收站只是通俗说法,在后面具体说明。通过reader可以有效获取文档的数量。

三、删除索引

相关方法:

public void delete(){
    IndexWriter writer = null;
    try {
        writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        
        //参数可以是一个选项,可以是一个query,也可以是一个term,term是一个精确查找的值
        //这里我们测试此方法之后再次执行搜索方法,发现文档数numDocs还有5个,比之前少了一个,但是maxDoc还是6个
        //在我们的索引目录中发现出现了一个delete的文件。这里的删除就像一个回收站一样,是可以恢复的
        writer.deleteDocuments(new Term("id", "1"));//这里表示删除索引为1的id
        
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(writer != null){
            try {
                writer.close();
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

说明:这里我们是通过Term对象来精确删除某个索引,当然还有其他很多删除方法,这里不细说。我们删除之后可以再次查询,可以发现回收站中多了一个文件。

测试:

@Test
public void testDelete(){
    IndexUtil util = new IndexUtil();
    util.delete();
}

四、恢复索引

相关方法:

    public void undelete(){
        
        try {
            //使用IndexReader恢复,第二个参数表示readOnly,默认为true,这里我们设置为false
            IndexReader reader = IndexReader.open(directory, false);
            reader.undeleteAll();//这里是恢复所有
            reader.close();
        } catch (StaleReaderException e) {
            e.printStackTrace();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

测试方法:

@Test
public void testUndelete(){
    IndexUtil util = new IndexUtil();
    util.undelete();
}

说明:上面的方法中我们已经提到过,这里的删除并不是真正的删除,而是放到了回收站,那这里的方法就是将回收站里的索引恢复。这里要注意的是需要将IndexReader.open方法的readOnly设置为false

五、清除回收站

相关方法:

    public void forcrDelete(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            
            writer.forceMergeDeletes();//强制清除回收站
            
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

测试:

@Test
public void testForceDelete(){
    IndexUtil util = new IndexUtil();
    util.forcrDelete();
}

说明:这里清空方法较为简单,只需要注意不要使用老版本中的方法。

六、手动合并

相关方法:

public void merge(){
    IndexWriter writer = null;
    try {
        writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        
        //这里我们将允许的段的数量最多只能有两个,那么多余的自然需要合并,
        //当然lucene其实默认有一个值,这里我们只是试验,强制合并的时候
        //会将回收站里的内容清空,当然这里我们设置的段的值为2,所以可能会有一个端中回收站的内容保留下来。
        //在3.5版本之后不建议使用,lucene会根据情况自行处理,如果要清空可以使用forceMergeDeletes方法
        writer.forceMerge(2);
        
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(writer != null){
            try {
                writer.close();
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

测试:

@Test
public void testForceMerge(){
    IndexUtil util = new IndexUtil();
    util.merge();
}

说明:这里我们只是作为演示,其实真正使用时不需要也不建议。在lucene中会自动根据段数量的最大值帮我们进行合并。

七、更新索引

相关方法:

public void update(){
    IndexWriter writer = null;
    try {
        writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

        /* lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
         * 先删除之后再添加,所以不是在之前的位置更新
         * 测试之后我们会发现回收站中有一个索引
         * */
        Document document = new Document();
        document.add(new Field("id", "11", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
        document.add(new Field("email", emails[0], Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.add(new Field("content", content[0], Field.Store.NO, Field.Index.ANALYZED));
        document.add(new Field("name", names[0], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
        writer.updateDocument(new Term("id", "1"), document);
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(writer != null){
            try {
                writer.close();
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

测试:

@Test
public void testUpdate(){
    IndexUtil util = new IndexUtil();
    util.update();
}

说明:这里的更新和一般意义上的更新不一样,这里的更新是先删除后添加,从测试之后回收站中产生一个文件就可以看出。

八、索引加权

这里我们拷贝上一个工程进行改进,这里是工程(lucene_index01
给出相关代码IndexUtil.java

package cn.lucene.index;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.StaleReaderException;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

public class IndexUtil {
    private String[] ids = {"1", "2", "3", "4", "5", "6"};
    //下面是邮件
    private String[] emails = {"aa@qq.com", "bb@sina.edu", "cc@yahu.org", "ss@sina.com", "dd@gmail.com", "ee@163.com"};
    //下面是邮件内容
    private String[] content = {
            "welcom to visited the space,I like football", 
            "hello boy, i like someone", 
            "come on baby", 
            "first blood", 
            "I like football,I like football", 
            "my girlfriend is so beatiful, every body like game"
    };
    private int[] attaches = {2,5,6,5,8,4};//附件数量
    //发件人名字
    private String[] names = {"Tom", "Jack", "goudan", "alibaba", "jerry", "kitty"};
    
    private Directory directory = null;
    private Map<String, Float> scores = new HashMap<String, Float>();//新建一个Map,用来存储权值
    
    public IndexUtil() {
        try {
            scores.put("qq.com", 2.0f);//如果是"qq.com"结尾的索引则让其权值为2.0,注意:默认是1.0
            scores.put("sina.edu", 1.5f);
            directory = FSDirectory.open(new File("E:/myeclipse/Lucene/index"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建索引
    public void index(){
        IndexWriter writer = null;
        try {
            writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            //此方法可将索引全部清空
            writer.deleteAll();
            Document document = null;
            for(int i = 0; i < ids.length; i++){
                document = new Document();
                //id需要存储,不需要加权、分词,email也需要存储,但不需要分词,有时候也需要加权
                //对于内容,我们不需要存储和加权,但需要分词。而名字需要存储,不需要分词和加权
                //这里我们先不对整型数据进行索引,后面再说
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", content[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                
                String et = emails[i].substring(emails[i].lastIndexOf("@") + 1);
                System.out.println(et);
                //加入权值
                //if(scores.containsKey(et)){
                //  document.setBoost(scores.get(et));
                //}else{
                //  document.setBoost(0.5f);
                //}
                writer.addDocument(document);
            }
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这里我们给结尾为qq.comsina.edu的邮件加入权值,注意新建一个Map来保存。但是这里我们先将加入索引的部分代码注释掉,不加入权值,然后写一个搜索方法:
IndexUtil.java

public void search(){
    IndexReader reader;
    try {
        reader = IndexReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        TermQuery query = new TermQuery(new Term("content", "like"));//搜索内容中含有like的
        TopDocs tds = searcher.search(query, 10);
        for(ScoreDoc sd : tds.scoreDocs){
            Document doc = searcher.doc(sd.doc);
            System.out.println("(" + sd.doc + ")" + doc.get("name") + "[" + doc.get("email") + "]-->" + doc.get("id"));
        }
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

说明:这里我们搜索内容中含有like关键字的索引,使用测试方法为:

@Test
public void testSearch(){
    IndexUtil util = new IndexUtil();
    util.search();
}

说明:我们先重新生成索引,然后再使用search方法进行搜索,结果为:

(4)jerry[dd@gmail.com]-->5
(1)Jack[bb@sina.edu]-->2
(0)Tom[aa@qq.com]-->1
(5)kitty[ee@163.com]-->6

现在我们将生成索引方法中的加入权值部分代码注释拿掉,重新生成索引,然后再次使用搜索方法查询,结果为:

(0)Tom[aa@qq.com]-->1
(1)Jack[bb@sina.edu]-->2
(4)jerry[dd@gmail.com]-->5
(5)kitty[ee@163.com]-->6

从排序中可以看到我们加入权值的内容排在前面。

九、luke工具

这是一个可以管理lucene索引信息的一个工具,我们可以在命令行中使用命令java –jar lukeall-3.5.0.jar打开。
打开之后,点击Browse选择我们创建的索引存放目录,这里是E:\myeclipse\Lucene\index

1

点击OK
2

可以看到相关的索引信息。这里我们只是做一个介绍,后面再具体说明。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,427评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,526评论 25 709
  • 爸爸刚刚烧好的!
    李葳Taiwan阅读 1,241评论 0 0
  • @所有人 《命运乾坤4期》作业 (2017年8月7日,周一,请于明天早上8:00前上传到本群) 亲爱的学员:周天好...
    加减红尘阅读 2,939评论 0 0
  • 这几天,你疯狂的沉迷饥荒这个游戏,不能自拔。日以继夜,你都坐在电脑前,面色癫狂,嘴角微张,双手机械地舞动着,即便两...
    淡漠羊阅读 3,669评论 0 0