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. Phalcon的MVC框架解析

    1. mvc/simple从最简单的入手吧. 把一些能及时说明白的东西写在注释里了,需要扩展的知识列在下面. public/index.php <?php $loader = new \Phal ...

  2. LINQ操作符二:SelectMany

    SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: student类: using System; using ...

  3. Java-jdbc增删改查操作

    java jdbc增删改查操作: package com.gordon.jdbc; import java.sql.Connection; import java.sql.DriverManager; ...

  4. mongo-查询(2)——比较/$in/$nin/$or/$not

    mongo通find来查找文档.可以执行精确匹配和模糊匹配. 2. 模糊匹配 2.1比较 > $gt , >= $gte, < $lt, <= $lte, != $ne > ...

  5. 06 Locking and Latching

    本章提要---------------------------------------------------------------6,7,8,9,10,11 这 6 章要细看, 从本章开始how ...

  6. kcp协议详解

    kcp协议是传输层的一个具有可靠性的传输层ARQ协议.它的设计是为了解决在网络拥堵情况下tcp协议的网络速度慢的问题.kcp力求在保证可靠性的情况下提高传输速度.kcp协议的关注点主要在控制数据的可靠 ...

  7. 转载------让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法

    本文是转载及收藏 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在c ...

  8. selenium测试(Java)--下载文件(十六)

    下载文件需要在Firefox 的profile属性中配置一些参数,如下面的代码: package com.test.download; import java.io.File; import org. ...

  9. WinInet 小例子

    #include <windows.h> #include <wininet.h> #include <string> #include <iostream& ...

  10. ‘,’之后要留空格,如 Function(x, y, z)

    ‘,’之后要留空格,如 Function(x, y, z).如果‘;’不是一行的结束 符号,其后要留空格,如 for (initialization; condition; update). #inc ...