把实体对象Article保存到文件中进行查找


public class Article {
    private Long id;
    private String title;
    private String content;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}


lucene已document 形式存储如图

lucene基本操作及原理_lucene

整体文件可看成一个document 每个实体对象可看成一个List<Feild> 实体对象里的属性名对应file里的key 实例对象实行值对象file val。实体对象里面的属性构成List<Feild>


把对象存到文件中

lucene基本操作及原理_lucene_02



在文件中读取

lucene基本操作及原理_lucene_03

@Test
    public void testCreateIndex() throws Exception{
        /**
         * 1、创建一个article对象,并且把信息存放进去
         * 2、调用indexWriter的API把数据存放在索引库中
         * 3、关闭indexWriter
         */
        //创建一个article对象,并且把信息存放进去
        Article article = new  Article();
        article.setId(1L);
        article.setTitle("lucene可以做搜索引擎");
        article.setContent("baidu,google都是很好的搜索引擎");
        //调用indexWriter的API把数据存放在索引库中
           /**
            * 创建一个IndexWriter
            *    参数三个
            *       1、索引库   指向索引库的位置
            *       2、分词器
            */
               //创建索引库
               Directory directory = FSDirectory.open(new File("./indexDir"));
               //创建分词器
               Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED);
              //把一个article对象转化成document
        Document document = new Document();
        Field idField = new Field("id",article.getId().toString(),Store.YES,Index.NOT_ANALYZED);
        Field titleField = new Field("title",article.getTitle(),Store.YES,Index.ANALYZED);
        Field contentField = new Field("content",article.getContent(),Store.YES,Index.ANALYZED);
        document.add(idField);
        document.add(titleField);
        document.add(contentField);
        indexWriter.addDocument(document);
        //关闭indexWriter
        indexWriter.close();
    }
      
    @Test
    public void testSearchIndex() throws Exception{
        /**
         * 1、创建一个 IndexSearch对象
         * 2、调用search方法进行检索
         * 3、输出内容
         */
        //创建一个 IndexSearch对象
        Directory directory = FSDirectory.open(new File("./indexDir"));
        IndexSearcher indexSearcher = new IndexSearcher(directory);
        //调用search方法进行检索
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        QueryParser queryParser = new QueryParser(Version.LUCENE_30,"content",analyzer);
        Query query = queryParser.parse("lucene");//关键词
        TopDocs topDocs = indexSearcher.search(query, 2);
        int count = topDocs.totalHits;//根据关键词查询出来的总的记录数
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<Article> articleList = new ArrayList<Article>();
        for(ScoreDoc scoreDoc:scoreDocs){
            float score = scoreDoc.score;//关键词得分
            int index = scoreDoc.doc;//索引的下标
            Document document = indexSearcher.doc(index);
            //把document转化成article
            Article article = new  Article();
            article.setId(Long.parseLong(document.get("id")));//document.getField("id").stringValue()
            article.setTitle(document.get("title"));
            article.setContent(document.get("content"));
            articleList.add(article);
        }
          
        for(Article article:articleList){
            System.out.println(article.getId());
            System.out.println(article.getTitle());
            System.out.println(article.getContent());
        }
    }