方法类

package com.wxf.Test;
import com.wxf.pojo.Goods;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import java.io.IOException;
import java.nio.file.Paths; /**
* @Auther: wxf
* @Date: 2018/6/29 15:40
*/
public class IndexCRUD {
private Directory dir;
{
try {
dir = FSDirectory.open(Paths.get( System.getProperty("user.dir")+"\\src\\main\\resources\\index"));
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 获取IndexWriter实例
* @return
* @throws Exception
*/
public IndexWriter getWriter()throws Exception{
//中文分词器
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
IndexWriterConfig iwc=new IndexWriterConfig(standardAnalyzer);
IndexWriter writer=new IndexWriter(dir, iwc);
return writer;
} public void setUp() throws Exception {
Goods goods=new Goods("123","红色强化门",360);
Goods goods2=new Goods("223","黑色强化门",370);
Goods goods3=new Goods("333","白色强化门",380);
String skuid[]={"123","223","333"};
String name[]={"红色强化门","黑色强化门","白色强化门"};
Object obj[]={goods,goods2,goods3};
IndexWriter writer=getWriter();
for(int i=0;i<skuid.length;i++){
Document doc=new Document();
doc.add(new StringField("skuid", skuid[i], Field.Store.YES));
doc.add(new TextField("name",name[i],Field.Store.YES));
doc.add(new TextField("obj", obj[i].toString(), Field.Store.YES));
writer.addDocument(doc); // 添加文档
}
writer.close();
} /**
* 测试写了几个文档
* @throws Exception
*/
public void testIndexWriter()throws Exception{
IndexWriter writer=getWriter();
System.out.println("写入了"+writer.numDocs()+"个文档");
writer.close();
} /**
* 测试读取文档
* @throws Exception
*/
public void testIndexReader()throws Exception{
IndexReader reader=DirectoryReader.open(dir);
System.out.println("最大文档数:"+reader.maxDoc());
System.out.println("实际文档数:"+reader.numDocs());
reader.close();
} /**
* 查询
* @return
*/
public void select(String str1,String str2) throws IOException, ParseException {
//得到读取索引文件的路径
Directory dir = FSDirectory.open(Paths.get(System.getProperty("user.dir")+"\\src\\main\\resources\\index"));
IndexReader ireader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(ireader);
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
/**
* 第一个参数是要查询的字段;
* 第二个参数是分析器Analyzer
* */
QueryParser parser = new QueryParser(str1, standardAnalyzer);
//根据传进来的str2查找
Query query = parser.parse(str2);
//计算索引开始时间
long start = System.currentTimeMillis();
/**
* 第一个参数是通过传过来的参数来查找得到的query;
* 第二个参数是要出查询的行数
* */
TopDocs rs = searcher.search(query, 10);
long end = System.currentTimeMillis();
System.out.println("匹配"+str2+",总共花费了"+(end-start)+"毫秒,共查到"+rs.totalHits+"条记录。");
for (int i = 0; i < rs.scoreDocs.length; i++) {
Document doc = searcher.doc(rs.scoreDocs[i].doc);
System.out.println("skuid:" + doc.getField("skuid").stringValue());
System.out.println("name:" + doc.getField("name").stringValue());
System.out.println("obj:" + doc.getField("obj").stringValue());
}
}
}

测试类

package com.wxf.Test;
/**
* @Auther: wxf
* @Date: 2018/6/29 15:46
*/
public class Test {
public static void main(String[] args) throws Exception {
IndexCRUD indexCRUD=new IndexCRUD();
// indexCRUD.setUp();
indexCRUD.testIndexWriter();
indexCRUD.testIndexReader();
indexCRUD.select("name", "黑");
}
}

  

indexCRUD.setUp() 这个方法 调一次就可以了 
结果如下

这次换个范围大的查询参数

public class Test {
public static void main(String[] args) throws Exception {
IndexCRUD indexCRUD=new IndexCRUD();
// indexCRUD.setUp();
indexCRUD.testIndexWriter();
indexCRUD.testIndexReader();
indexCRUD.select("name", "强化");
}
}

  结果如下:

这里采用一元分词 可以随意匹配 

lucene简单搜索demo的更多相关文章

  1. lucene简单使用demo

    测试结构目录: 1.索引库.分词器 Configuration.java package com.test.www.web.lucene; import java.io.File; import or ...

  2. lucene 简单搜索步骤

    1.创建IndexReader实例: Directory dir = FSDirectory.open(new File(indexDir)); IndexReader reader = Direct ...

  3. [MyBean说明书]-如何进行最简单的DEMO

    MyBean是轻量级的.绿色的框架,不需要安装任何的组件和携带任何的其他文件,前 期步骤已经相当精简了,仔细阅读完下面简单的五个步骤,就可以编写基于MyBean的插件: 1.加入Delphi的搜索路径 ...

  4. ElasticSearch 5学习(4)——简单搜索笔记

    空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...

  5. C#可扩展编程之MEF学习笔记(一):MEF简介及简单的Demo

    在文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架).单从名字我们不难发现:MEF是专门致力于解决扩展性问题的框架 ...

  6. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  7. Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)

    然后是项目下的文件:完整的项目请看  上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...

  8. MEF简介及简单的Demo

    MEF简介及简单的Demo 文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架).单从名字我们不难发现:MEF是专门 ...

  9. 分布式搜索ElasticSearch构建集群与简单搜索实例应用

    分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...

随机推荐

  1. js原生的节点操作API

    // yi获取元素节点 //一 :过id的方式( 通过id查找元素,大小写敏感,如果有多个id只找到第一个) document.getElementById('div1'); // 通过类名查找元素, ...

  2. [JAVA]JAVA章4 Thread Dump如何分析

    一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工具.每一个Java虚拟机都有及时生成所有线程在某一点状态的thread- ...

  3. zeromq学习记录(八)负载均衡 附ZMQ_ROUTER的流程分析

    /************************************************************** 技术博客 http://www.cnblogs.com/itdef/   ...

  4. tcp、ip、http

    tcp是传输层协议,ip是网络层协议,http是应用层协议,简单说就是tcp是传输数据,而http是封装数据. rpc与http的区别是项目大的话,接口间调用变多的话,采用rpc的话,不用像http那 ...

  5. java生成pdf文件 --- Table

    Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...

  6. MySQL中校验规则(collation)的选取对实际数据筛选的影响

    在mysql中存在着各种utf8编码格式,如下表:1)utf8_bin2)utf8_general_ci utf8_bin将字符串中的每一个字符用二进制数据存储,区分大小写.utf8_genera_c ...

  7. Paper | 块分割信息 + 压缩视频质量增强

    目录 1. 亮点 2. 网络 3. Mask 及其融合 4. 结论 论文:Enhancing HEVC Compressed Videos with a Partition-Masked Convol ...

  8. handoop安装部署集群

    hdfs 1.0版本 一个namenode (secordary namenode 2.0冷备份) 和多个datanode构成分布式文件系统 mapreduce 一个 jobtracker 协调 ta ...

  9. IDEA运行android项目一直是同一个apk

    1.IDEA运行android项目时不像eclipse和android studio那样直接运行,IDEA需要设置Artifacts,这样每次运行的时候它才能重新编译,以下附上步骤! 这样就可以了.

  10. 第 1 篇 Scrum 冲刺博客

    各个成员在 Alpha 阶段认领的任务 姓名 Alpha 阶段认领的任务 徐婉萍 创建服务器.域名,环境搭建查询界面及页面的设计,查询方法的编写 谭燕 支出.收入添加界面及设计,收入.支出的方法编写, ...