1.最基本的创建索引:

@Test
public void testIndex(){
try {
Directory directory = FSDirectory.open(new File(LUCENE_DIRECTORY));
IndexWriter indexWriter = new IndexWriter(directory,new IndexWriterConfig(Version.LATEST,new StandardAnalyzer()));
Document document = new Document();
TextField titleFiled = new TextField("name","jiaoyiping", Field.Store.YES);
document.add(titleFiled);
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

 

2.使用FieldType创建索引

 

//使用FieldType创建Field(4.X之后才有)
@Test
public void testCreateIndexUseFieldType(){
try {
Directory directory = FSDirectory.open(new File(LUCENE_DIRECTORY));
IndexWriter indexWriter = new IndexWriter(directory,new IndexWriterConfig(Version.LATEST,new StandardAnalyzer()));
Document document = new Document();
FieldType titleType = new FieldType();
titleType.setIndexed(true);//索引选项
titleType.setStored(true); //存储选项
Field field = new Field("title","下班",titleType);
TextField titleFiled = new TextField("name","jiaoyiping", Field.Store.YES);
document.add(titleFiled);
document.add(field);
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

搜索示例:

/**
* 搜索示例
*/
@Test
public void testQuery(){
try {
IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(LUCENE_DIRECTORY)));
IndexSearcher searcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser("title",new StandardAnalyzer());
Query query = queryParser.parse("下班");
ScoreDoc[] docs = searcher.search(query,20).scoreDocs; //命中的数组
for(ScoreDoc sd:docs){
int docNumber = sd.doc;
System.out.println("文档号: "+docNumber);
Document doc = searcher.doc(docNumber);//根据文档号来查询文档
System.out.println(doc.get("name"));
} } catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

 

代码片段,lucene基本操作(基于lucene4.10.2)的更多相关文章

  1. 全文检索(二)-基于lucene4.10的增删改查

    今天 用lucene完毕了 一个简单的web应用.提取了早期编写的一个測试类. 首先简单介绍下lucene几个经常使用包; lucene 包的组成结构:对于外部应用来说索引模块(index)和检索模块 ...

  2. lucene4.10.2实例(增删改查)

    最新jar和src免费下载:http://download.csdn.net/detail/u011518709/8248403 lucene 包的组成结构:对于外部应用来说索引模块(index)和检 ...

  3. 10个 jQuery 代码片段,可以帮你快速开发。

    转载自:http://mp.weixin.qq.com/s/mMstI10vqwu8PvUwlLborw 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而 ...

  4. 在网站制作中随时可用的10个 HTML5 代码片段

    HTML 很容易写,但创建网页时,您经常需要重复做同样的任务,如创建表单.在这篇文章中,我收集了10个超有用的 HTML 代码片段,有 HTML5 启动模板.空白图片.打电话和发短信.自动完成等等,帮 ...

  5. 经验分享:10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...

  6. 高效Web开发的10个jQuery代码片段(10 JQUERY SNIPPETS FOR EFFICIENT WEB DEVELOPMENT)

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  7. 10 个实用的 jQuery 表单操作代码片段

    jQuery 绝对是一个伟大的开源JavaScript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的 JavaScript,在今天这篇代码片段分享文章中, ...

  8. 10个可以直接拿来用的JQuery代码片段

    jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了10段非常实用的jQue ...

  9. 10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库. 今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 1.平滑滚动到 ...

随机推荐

  1. Oracle分页(limit方式的运用)

    select * from a_matrix_navigation_map where rowid not in(select rowid from a_matrix_navigation_map w ...

  2. Nginx下轻松开启Drupal简洁链接

    大家都知道Drupal在apache环境下使用简洁链接是件很轻松的事,因为官方已经把写好的.htaccess文件附在源代码里,一般在配置里直接就可以打开了.但在Nginx下却没有那么简单,但不用担心, ...

  3. StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(一)准备工作

    原文地址:http://blog.starrtc.com/?p=48 啥也不说,先来个视频看看效果 视频播放器     00:00   00:54     概述为了体现StarRTC的实时音视频传输能 ...

  4. android EditText设置光标、边框和图标,以及限制输入

    控制边框形状,先在drawable中建一个xml文件:shape.xml <?xml version="1.0" encoding="utf-8"?> ...

  5. Maven学习日记(二)---MAVEN创建多模块的项目

    手动构建多模块maven项目,这个simple-parent项目下有两个子模块,一个是jar包型的simple-weather和一个war型的simple-webapp1.创建一个父的simple-p ...

  6. 第二百九十三,Memcached缓存

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  7. 不可在 for 循环体内修改循环变量,防止 for 循环失去控制

    不可在 for 循环体内修改循环变量,防止 for 循环失去控制. #include <iostream> /* run this program using the console pa ...

  8. 从头认识java-18.2 主要的线程机制(7)join

    这一章节我们来讨论一下join. 1.什么是join? 阻塞当前线程,让.join的线程先做完.然后再继续当前线程 以下是api的解释: A.join,在API中的解释是,阻塞当前线程B,直到A运行完 ...

  9. par函数的xaxt函数-控制x轴刻度的显示

    xaxt 参数控制x轴的刻度以及刻度对应的标签时候显示 默认值为‘s’, 表示显示,代码示例 par(xaxt = 's') plot(1:5, 1:5, main = "title&quo ...

  10. RSQLite 操作sqlite数据库

    RSQLite 可以在R中方便的创建sqlite数据库,并进行检索, 这个R包依赖于DBI包 github 上的地址:https://github.com/rstats-db/RSQLite gith ...