参考文档: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();
}
}

--

Lucene 6.5.0 入门Demo(2)的更多相关文章

  1. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...

  2. vue入门 0 小demo (挂载点、模板、实例)

    vue入门 0 小demo  (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...

  3. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  4. spring web flow 2.0入门(转)

    Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...

  5. 【SSH系列】初识spring+入门demo

    学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...

  6. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  7. lua入门demo(HelloWorld+redis读取)

    1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...

  8. netty入门demo(一)

    目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...

  9. canal入门Demo

    关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...

随机推荐

  1. vue cli的安装与使用

    一.简介 vue作为前端开发的热门工具,受到前端开发人员的喜爱.为了简化项目的构建,采用vue cli来简化开发. vue cli是一个全局安装的npm包,提供了终端使用的命令.vue create可 ...

  2. chrom浏览器-F2使用方法一

    由于F12是前端开发人员的利器,所以我自己也在不断摸索中,查看一些博客和资料后,自己总结了一下来帮助自己理解和记忆,也希望能帮到有需要的小伙伴,嘿嘿! 首先介绍Chrome开发者工具中,调试时使用最多 ...

  3. CAD命令标志

    CAD命令标志 主标识:(常用的)ACRX_CMD_MODAL 在别的命令执行的时候该命令不会在其中执行.ACRX_CMD_TRANSPARENT 命令可以再其它命令中执行,但在该标志下ads_sss ...

  4. zust_第二周——瞎扯系列

    首先来原题列表: A:Gridland http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1037 B:HangOver htt ...

  5. request :fail url not in domain list

    1.可点击开发者工具右上角 详情-域名信息,看看是否配置了域名: 2. 手机预览小程序的时候,打开调试

  6. C# 文件操作的工具类

    using System.IO; namespace 文件操作类 { public class FileHelper { /// <summary> /// 判断文件是否存在 /// &l ...

  7. linux配置nodeJs环境教程

    来自阿里云:https://help.aliyun.com/document_detail/50775.html

  8. Idea 搭建Maven--web项目(MVC)

    小编最近正在学习使用MVC框架,在搭建Maven的项目过程中,遇到了很多问题,上网搜了很多材料才找到答案,为了小编以后查起来方便,也为了向广大小伙伴分享,写了这部片博文,敬我昨天一天的学习结果! 步骤 ...

  9. 对于js运动中产生的问题

    1.不同的对象调用同一个定时器情况,则需要将定时器的名称定为该对象的一个属性来进行运用. 例: <!DOCTYPE html> <html lang="en"&g ...

  10. Python9-文件操作-day8

    # 1.文件路径:d:\xxx.txt# 绝对路径:从根目录开始# 想对路径:当前文件下# 2.编码方式:utf-8/gbk...# 3.操作方式:只读.只写.追加.读写.写读..... f=open ...