2.lucene入门案例(lucene6.0)

项目源码路径:https://github.com/tangxing1993/lucene/tree/master

项目使用Maven构建,采用lucene6.0,lucene前后版本有差异,pom文件如下:

    <dependencies>
    <!-- lucene的核心jar包 -->
    <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-core</artifactId>
    <version>6.0.0</version>
    </dependency>
    <!-- lucene的查询解析包 -->
    <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-queryparser</artifactId>
    <version>6.0.0</version>
    </dependency>
    <!-- lucene的各种分词器包 -->
    <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-analyzers-common</artifactId>
    <version>6.0.0</version>
   </dependency>
   <!-- lucene的查询包 -->
   <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-queries</artifactId>
    <version>6.0.0</version>
   </dependency>
   <!-- lucene的显示高亮包  -->
   <dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-highlighter</artifactId>
    <version>6.0.0</version>
   </dependency>
    </dependencies>
  <!-- 项目使用jdk1.8 -->  
  <build>
    <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
        <source>1.8</source>
        <target>1.8</target>
        </configuration>
        </plugin>
    </plugins>
  </build>

创建入门程序HelloLucene.java

/**
* 索引的核心API
* IndexWriter  索引的写对象
* IndexWriterConfig 索引写对象的配置对象(可以设置写操作是create还是append)
* Analyzer     通用的分词器接口
* Filed        域对象
* Document     文档对象
* IndexReader  索引的读对象
* IndexSearcher 搜索索引对象
*/
public class HelloLucene {

    /**
     * 被索引文件的存储路径
     */
    private String index_store = "D:\\develope\\store\\hello";
    /**
     * 索引的存储目录
     */
    private Directory directory;
    /**
     * 创建模拟数据   实际被索引的数据可以为内存中的数据,文件,数据库数据等等
     */
    private String[] names = {"java","lucene"};
    private String[] contents = {"hello java !","hello lucene !"};

    /**
     * 创建索引
     */
    public void createIndex(){
        IndexWriter writer = null;
        Document document = null;
        try{
            Long start = System.currentTimeMillis()/1000;
            System.out.println("开始创建索引...");
            File file = new File(index_store);
            if(!file.exists())
                file.mkdirs();
            //创建目录对象
            directory = FSDirectory.open(file.toPath());
            //创建标准分词器
            Analyzer analyzer = new StandardAnalyzer();
            //创建写索引对象的配置对象
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //创建索引的写入对象
            writer = new IndexWriter(directory, config);
            //清除所有的索引内容
            writer.deleteAll();
            //向文档对象中创建域对象,然后由写索引对象创建索引
            for(int i=0;i<names.length;i++){
                document = new Document();
                document.add(new StringField("name", names[i], Store.YES));
                document.add(new TextField("content", contents[i], Store.YES));
                writer.addDocument(document);
            }
            Long end = System.currentTimeMillis()/1000;
         System.out.println("索引创建成功!耗时"+(end-start)+"s");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(null!=writer){
                try {
                    writer.close();  //关闭资源
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 搜索操作
     */
    public void doSearch(){
        IndexReader reader =null;
        try{
            directory = FSDirectory.open(new File(index_store).toPath());
            //创建索引文件读对象
            reader = DirectoryReader.open(directory);
            //创建索引搜索对象
            IndexSearcher searcher = new IndexSearcher(reader);
            //创建标准分词器
            Analyzer analyzer = new StandardAnalyzer();
            //创建查询解析器
            QueryParser parser = new QueryParser("content", analyzer);
            //搜索内容中包含java的文件
            Query query = parser.parse("java");
            //搜索内容
            TopDocs search = searcher.search(query, 10);   //10表示要现实的调试
            //获取搜索到的结果
            ScoreDoc[] scoreDocs = search.scoreDocs;
            for(ScoreDoc sd : scoreDocs){
                //获取搜索到的文档
                Document doc = searcher.doc(sd.doc);
                System.out.println("文档id:"+sd.doc+" | name:"+doc.get("name")+" | content:"+doc.get("content"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(null!=reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        //创建索引
        HelloLucene lucene = new HelloLucene();
        lucene.createIndex();
        lucene.doSearch();
    }
}

最终执行结果:

这里写图片描述

luke工具的使用:

索引创建完之后可以使用索引查看工具进行查看和维护
Luke(https://code.google.com/archive/p/luke/)这个好像只能下载到支持lucene4.0版本以下的,打开页面
这里写图片描述
本片用到的工具在http://www.oschina.net/news/72599/luke-6-0-0 里面下载。
工具的信息展示
查看文档内容信息

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值