zoukankan      html  css  js  c++  java
  • Lucene 6.5.0 入门Demo(2)

    参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description

    对于path路径不是很清楚,参考了:http://blog.csdn.net/zhangweiwtmdbf/article/details/7099988

    package com.cf.first;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.*;
    import org.apache.lucene.index.*;
    import org.apache.lucene.queryparser.classic.ParseException;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.junit.Test;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Path;
    
    /**
     * 参考文件:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description
     * <p>
     * Created by Administrator on 2017/4/26.
     */
    public class TestLucence {
        // 创建索引
        @Test
        public void createIndex() throws IOException {
            //分词器  (对文本进行分词...)
            Analyzer analyzer = new StandardAnalyzer();
            File file = new File("temp/");
            Path path = file.toPath();
            Directory directory = FSDirectory.open(path);
    
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    //构建用于操作索引的类
            IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
            Document document = new Document();
            IndexableField filed = new TextField("id", Integer.toString(1), Field.Store.YES);
            IndexableField title = new StringField("title", "Lucene_百度百科", Field.Store.YES);
            IndexableField content = new TextField("content", "Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的...", Field.Store.YES);
    
            document.add(filed);
            document.add(title);
            document.add(content);
            indexWriter.addDocument(document);
            indexWriter.close();
        }
    
        @Test
        public void searchIndex() throws IOException, ParseException {
    
            Analyzer analyzer = new StandardAnalyzer();
    
            File file = new File("temp/");
            Path path = file.toPath();
            //索引存放的位置....
            Directory directory = FSDirectory.open(path);
            IndexReader indexReader = DirectoryReader.open(directory);
            //通过indexSearcher 去检索索引目录...
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            //这个是一个搜索条件..,通过定义条件来进行查找
            QueryParser parser = new QueryParser("content", analyzer);
            Query query = parser.parse("apache");
            //搜索先搜索索引目录..
            //找到符合query 条件的前面N条记录...
            TopDocs topDocs = indexSearcher.search(query, 100);
            System.out.println("总记录数是====:" + topDocs.totalHits);
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            //返回一个击中
            for (ScoreDoc scoreDoc : scoreDocs) {
                int docId = scoreDoc.doc;
                Document document = indexSearcher.doc(docId);
                System.out.println(document.get("id"));
                System.out.println(document.get("title"));
                System.out.println(document.get("content"));
            }
    
        }
       //删除索引
    @Test
    public void deleteIndex() throws IOException {
    // 创建标准分词器
    Analyzer analyzer = new StandardAnalyzer();

    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    File file = new File("temp/");

    Path path = file.toPath();
    Directory directory = FSDirectory.open(path);
    // 创建indexWriter
    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    // 删除全部
    indexWriter.deleteAll();
    indexWriter.close();
    }
    }

    --

  • 相关阅读:
    Python的学习之旅———UDP
    Python的学习之旅———socket ,socketserver
    Python的学习之旅———time 模块
    python的学习之旅---面向对象
    Python的学习之旅———re 模块正则表达式
    有事没事找高宇哥聊天,李泽军爸妈聊天,管那么多人干嘛,活好自己
    还有教师观没有记
    Navicat中怎么查看数据库密码
    Oracle 删除用户时出现异常ora-01922: CASCADE must bu specified to drop 用户名
    kali使用sqlmap注入dvma
  • 原文地址:https://www.cnblogs.com/jwlfpzj/p/6768746.html
Copyright © 2011-2022 走看看