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. 判断list为空的条件

    if(list!=null&&!list.isEmpty()){                    hql.append(" and (status = ? or sta ...

  2. .net提供的5种request-response方法一

    .net提供了三种基本方法和两种底层方法来发送http请求和接收http响应,通过这些方法,我们可以模仿在浏览器地址栏输入URL地址访问网页的方法.我们发送http请求,接收服务器返回的响应(通常就是 ...

  3. JQuery下拉控件select的操作汇总

    JQuery获取和设置Select选项方法汇总如下: 获取select 先看看下面代码:   $("#select_id").change(function(){//code... ...

  4. Linux 精确获取指定目录对应的块的剩余空间

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/statfs ...

  5. QT全局宏变量的实现

    qt中如何实现定义一个宏,在整个工程中都可以实现呢?下面我来写一个亲测的例子: pro中添加如下宏定义代码: DEFINES += HELLO=\"$$PWD/\" DEFINES ...

  6. 关于Cocos2d-x中addchild和removeChild方法的参数的解析

    一.addchild virtual void addchild( Node * child , int localZOrder , int tag )添加一个子节点到容器中,有Z轴顺序和一个标记. ...

  7. ZooKeeper的架构

    ZooKeeper的架构 看看下面的图表.它描述了ZooKeeper的“客户端-服务器架构”. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客 ...

  8. CentOS 7拨号上网(ADSL & PPPoE)

    步骤概述: 1.搜寻PPPoE相关软件,本人使用的是rp-pppoe yum search pppoe 2.使用yum安装rp-pppoe yum install rp-pppoe -y 3.开始配置 ...

  9. Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)

    主要思路是:override move系列的方法,让cursor以自己想要的顺序来移动,从而达到对cursor排序的目的.比如数组A0里有 4(0),3(1),1(2),2(3),括号内为位置,排序后 ...

  10. R语言低级绘图函数-rect

    rect 函数用来在一张图上添加矩形,只需要指定左下角和右上角的坐标的位置,就可以画出一个矩形 基本用法: plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), ...