*lucene索引_创建_域选项
【索引建立步骤】
【创建Directory】

【创建writer】

【创建文档并添加索引】
文档和域的概念很重要
文档相当于表中的每一条记录,域相当于表中的每一个字段。

【查询索引的基本信息】
使用IndexReader进行查询。

【实践】
附:

IndexUtil.java:
package cn.hk.index; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version; public class IndexUtil {
private String[] ids = {"1","2","3","4","5","6"};
private String[] emails = {"aa@hk.arg","bb@hk.org","cc@hk.arg",
"dd@hk.org","ee@hk.org","ff@hk.org"};
private String[] content = {
"welcome to visited the space","hello boy","my name is aa","i like football",
"I like football and I like Basketball too","I like movie and swim"
};
private int[] attachs = {2,3,1,4,5,5};
private String[] names = {"zhangsan","lisi","john","mike","jetty","jake"}; private Directory directory = null; public IndexUtil(){
try {
directory = FSDirectory.open(new File("d://lucene/index02"));
} catch (IOException e) {
e.printStackTrace();
}
} public void query(){
try {
IndexReader reader = IndexReader.open(directory);
//通过reader可以获取文档的数量
System.out.println("numDocs:" + reader.numDocs());
System.out.println("maxDocs" + reader.maxDoc());
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} public void index(){
IndexWriter writer = null;
try {
writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc = null;
for(int i=0;i<ids.length;i++){
doc = new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("content",content[i],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(writer != null)
try {
writer.close();
} catch (CorruptIndexException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} }
} }
TestIndex.java:
package cn.hk.test;
import org.junit.Test;
import cn.hk.index.IndexUtil;
public class TestIndex {
@Test
public void testIndex(){
IndexUtil iu = new IndexUtil();
iu.index();
}
@Test
public void testQuery(){
IndexUtil iu = new IndexUtil();
iu.query();
}
}
index()运行结果:

query()运行结果

*lucene索引_创建_域选项的更多相关文章
- Lucene——Field.Store(存储域选项)及Field.Index(索引选项)
Field.Store.YES或者NO(存储域选项) 设置为YES表示或把这个域中的内容完全存储到文件中,方便进行文本的还原 设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完 ...
- Lucene——索引的创建、删除、修改
package cn.tz.lucene; import java.io.File; import java.util.ArrayList; import java.util.List; import ...
- lucene索引的创建与搜索
package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...
- Lock锁_线程_线程域
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- lucene 索引创建步骤
一.步骤: 1.存储位置:1)文件: Directory dir= FSDirectory.open(new File("D:\\LuceneIndex")); 2)内存: new ...
- C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节
C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节 1.Qt概述 1.1 什么是Qt Qt是一个跨平台的C++图形用户界面应用程序框架.它为应用程序开发者提供建立艺术级图形界面 ...
- JAVAEE——Lucene基础:什么是全文检索、Lucene实现全文检索的流程、配置开发环境、索引库创建与管理
1. 学习计划 第一天:Lucene的基础知识 1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5 ...
- Lucene中的域选项
Field类是lucene在索引过程中的一个很重要的类,该类控制着域值被索引的方式 域选项主要包括以下几种选项:域索引选项.域存储选项.域的项向量选项.域的排序选项等 域索引选项:通过倒排索引来控制域 ...
- lucene&solr学习——创建和查询索引(理论)
1.Lucene基础 (1) 简介 Lucene是apache下的一个开放源代码的全文检索引擎工具包.提供完整的查询引擎和索引引擎:部分文本分析引擎. Lucene的目的是为软件开发人员提供一个简单易 ...
随机推荐
- A - Supercentral Point CodeForces - 165A
One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of point ...
- 牛客网暑期ACM多校训练营(第五场)
J-plan(贪心) 题目描述 There are n students going to travel. And hotel has two types room:double room and t ...
- Nuget 自定义配置(官网)
<?xml version="1.0" encoding="utf-8"?> <configuration> <config> ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- div 弹出 居中
function show_tc_conv(){var x=jq13(window).width()/2;var y=jq13(window).height()/2; var div_w=jq13(& ...
- JAVA一些错误代码
//算术异常 ArithmeticExecption //空指针异常类 NullPointerException //类型强制转换异常 ClassCastException //数组负下标异常 Neg ...
- linux centos7 tomcat8 配置成服务启动
1. tomact 解压到/usr/local/tomcat下 2.vim /usr/local/tomcat/bin/catalina.sh 在OS specific support.前加上 (注意 ...
- Redis 存储字符串和对象
今天用redis存储,发现客户端jedis提供的存储方法中存储的类型只有String和byte数据,没有能够存储对象的,网上发现可以序列化存储对象.这就开始了我第一次序列化之旅. 1 测试类 ...
- IE盒模型和标准w3c盒模型
Margin(外边距) - 清除边框外的区域,外边距是透明的.Border(边框) - 围绕在内边距和内容外的边框.Padding(内边距) - 清除内容周围的区域,内边距是透明的.Content(内 ...
- codevs 1082 线段树练习 3 --分块练习
时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[ ...