A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 项老师 黑马粉丝团   /  2018-5-31 11:16  /  1284 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

【合肥中心】Lucene5.3.1 使用的简单实例
Lucene是一个基于Java的开源全文信息检索工具包。
MyTest.Java
[AppleScript] 纯文本查看 复制代码
public class MyTest {

    /// 存放需要分析的文件的地址
    private static String FILE_PATH = "/Users/zoujs/Desktop/test4lucene";

    // 存放索引的位置
    private static String INDEX_PATH = "/Users/zoujs/Desktop/index4lucene";

    @Test
    public void createIndex() {
        IndexUtils.createIndex(INDEX_PATH, FileUtils.loadFiles(FILE_PATH));
    }

    @Test
    public void searchIndex() {
        SearcherUtils.search(INDEX_PATH, "title", "china");
    }
}  
FileBean.java
[AppleScript] 纯文本查看 复制代码
public class FileBean implements Serializable{

    private String title;
    private String content;
    private int 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;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "FileBean{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", id=" + id +
                '}';
    }
}
IndexUtils.java
[AppleScript] 纯文本查看 复制代码
public class IndexUtils {
  
    public static void createIndex(String indexPath, List<FileBean> files) {
      
        Directory directory = null;
        try {
            if (StringUtils.isBlank(indexPath))
                directory = new RAMDirectory();
            else
                directory = FSDirectory.open(Paths.get(indexPath));

            // 获取分析器
            Analyzer analyzer = new StandardAnalyzer();  //标准分析器

            // IndexWriter的配置
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE); //无索引文件则创建,有则在后面继续添加

            // 创建IndexWriter
            IndexWriter writer = new IndexWriter(directory, indexWriterConfig);

            // 遍历文件Bean创建索引
            for (FileBean bean : files) {
                // 创建Document
                Document doc = new Document();

                // 为Document创建三个字段 id title content
                doc.add(new IntField("id", bean.getId(), IntField.TYPE_STORED));
                doc.add(new Field("title", bean.getTitle(), TextField.TYPE_STORED));
                doc.add(new TextField("content", bean.getContent(), Field.Store.YES));

                // 将Document写入IndexWriter
                writer.addDocument(doc);
            }

            // 提交
            writer.commit();
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
SearchUtils.java
[AppleScript] 纯文本查看 复制代码
public class SearcherUtils {

    public static void search(String indexPath, String field, String text) {
        try {

            // 索引文件目录
            Directory directory = FSDirectory.open(Paths.get(indexPath));

            // 获取IndexReader
            DirectoryReader reader = DirectoryReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);

            Term term = new Term(field, text);
            Query query = new TermQuery(term);

            TopDocs topDocs = searcher.search(query, 10);
            ScoreDoc[] hits = topDocs.scoreDocs;

            if (null != hits)
                for (int i = 0; i < hits.length; i++) {
                    Document doc = searcher.doc(hits[i].doc);
                    System.out.println(doc.get("title"));
                }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


9 个回复

倒序浏览
回复 使用道具 举报
666
回复 使用道具 举报
回复 使用道具 举报
666
回复 使用道具 举报
回复 使用道具 举报
项老师 来自手机 黑马粉丝团 2018-5-31 15:29:28
7#
6666666
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马