• 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();
    }
    }

    --

  • 相关阅读:
    AI boxfilter
    AI AdaBoost算法
    AI Haar特征
    15.VUE学习之-表单中使用key唯一令牌解决表单值混乱问题
    14.VUE学习之-v-if v-else-if语法在网站注册中的实际应用讲解
    13.VUE学习之控制行内样式
    12.2 VUE学习之-if判断,实践加减input里的值
    12.1.VUE学习之-循环li,if判断示例讲解class中应用表达式
    10.VUE学习之使用lodash库减少watch对后台请求的压力
    09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串
  • 原文地址:https://www.cnblogs.com/jwlfpzj/p/6768746.html
Copyright © 2020-2023  润新知