@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD })
public @interface IndexAnnotation {
//存储
public boolean store() default false;
//分词
public boolean analyse() default false;
//删除HTML代码
public boolean parseHtml() default true;
//权重分
public float boost() default 10;
}
@SuppressWarnings("unchecked")
public final class IndexDocumentUtils {
private final static Logger log = LoggerFactory
.getLogger(IndexDocumentUtils.class);
/**
* 创建索引
*
* @param idataIndex
* @return
*/
public static Document createDocument(IdataIndex idataIndex) {
Class clzss = idataIndex.getClass();
Document doc = new Document();
Field[] fields = clzss.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals("serialVersionUID"))
continue;
String value = getFieldValue(idataIndex, field.getName());
org.apache.lucene.document.Field indexField = new org.apache.lucene.document.Field(
field.getName(), value, getStore(idataIndex, field
.getName()), getIndex(idataIndex, field.getName()));
//设置权重值
indexField.setBoost(getBoost(idataIndex, field.getName()));
doc.add(indexField);
}
return doc;
}
/**
* 通过反射获取字段值
*
* @param idataIndex
* @param fieldName
* @return
*/
private static Pattern tagPattern = Pattern
.compile("<.*?>", Pattern.DOTALL);
private static String getFieldValue(IdataIndex idataIndex, String fieldName) {
try {
boolean isMatcher = false;
String value = StringUtil.defaultIfEmpty(BeanUtils.getProperty(
idataIndex, fieldName));
StringBuffer sb = new StringBuffer();
if (isParseHtml(idataIndex, fieldName)) {// 是否解析html内容
if (StringUtils.isNotEmpty(value)) {
Matcher matcher = tagPattern.matcher(value);
while (matcher.find()) {
isMatcher = true;
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
} else {
return "";
}
}
return isMatcher ? sb.toString() : value;
} catch (Exception e) {
log.error(e);
return "";
}
}
/**
* 返回索引字段是否存储
*
* @param idataIndex
* @param fieldName
* @return
*/
private static Store getStore(IdataIndex idataIndex, String fieldName) {
Class clzss = idataIndex.getClass();
try {
Field field = clzss.getDeclaredField(fieldName);
IndexAnnotation ia = field.getAnnotation(IndexAnnotation.class);
if (ia != null) {// 检查注解的值
if (ia.store()) {
return org.apache.lucene.document.Field.Store.YES;
}
}
} catch (Exception e) {
log.error(e);
}
return org.apache.lucene.document.Field.Store.NO;
}
/**
* 返回索引字段是否索引
*
* @param idataIndex
* @param fieldName
* @return
*/
private static Index getIndex(IdataIndex idataIndex, String fieldName) {
Class clzss = idataIndex.getClass();
try {
Field field = clzss.getDeclaredField(fieldName);
IndexAnnotation ia = field.getAnnotation(IndexAnnotation.class);
if (ia != null) {// 检查注解的值
if (ia.analyse()) {
return org.apache.lucene.document.Field.Index.ANALYZED;
}
}
} catch (Exception e) {
log.error(e);
}
return org.apache.lucene.document.Field.Index.ANALYZED;
}
/**
* 返回索引字段是否解析HTML
*
* @param idataIndex
* @param fieldName
* @return
*/
private static boolean isParseHtml(IdataIndex idataIndex, String fieldName) {
Class clzss = idataIndex.getClass();
try {
Field field = clzss.getDeclaredField(fieldName);
IndexAnnotation ia = field.getAnnotation(IndexAnnotation.class);
if (ia != null) {// 检查注解的值
return ia.parseHtml();
}
} catch (Exception e) {
log.error(e);
}
return true;
}
/**
* 返回权重值
* @param idataIndex
* @param fieldName
* @return
*/
private static float getBoost(IdataIndex idataIndex, String fieldName) {
Class clzss = idataIndex.getClass();
try {
Field field = clzss.getDeclaredField(fieldName);
IndexAnnotation ia = field.getAnnotation(IndexAnnotation.class);
if (ia != null) {// 检查注解的值
return ia.boost();
}
} catch (Exception e) {
log.error(e);
}
return 10;
}
}
public class SearchIndex implements Serializable {
/**
*
*/
private static final long serialVersionUID = 153648837940506749L;
//索引编号
@IndexAnnotation(store = true)
private String id;
//资源ID
@IndexAnnotation(store = true)
private String resourceId;
//标题
@IndexAnnotation(store = true, analyse = true,boost=100)
private String title;
//索引内容说明
@IndexAnnotation(store = true, analyse = true,boost=50)
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getResourceId() {
return resourceId;
}
public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
分享到:
相关推荐
Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中
Lucene3.0创建索引 读取目录下的所有txt文档格式的文件,然后生成一个索引文件到某目录下!
java创建Lucene索引
Lucene创建索引,查询索引的简单使用。
lucene索引结构原理
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
在Eclipse环境中运用java,Lucene建索引及查询关键字
1) 提供实时索引的创建、管理 2) Query的创建 详细介绍参照博客:http://blog.csdn.net/xiaojimanman/article/details/20624739 中的介绍
lucene并行索引
lucene创建索引,查询索引的简单使用。
为了快速查询,加快查询速度,减少与数据库交互,经常需要用到lucene实现创建索引和查询
通过lucene创建索引,然后进行各种搜索
lucene 创建索引和使用 快速入门的号东西
基于lucene5.3.1的项目实例 lucene创建索引,删除索引,更新索引,查找索引。
介绍了Lucene的基础知识,包括Lucene的历史和发展情况、使用Lucene创建索引和执行搜索的基本方法以及中文分词的应用,最后做了两个应用项目。第2部分:数据解析。介绍解析不同格式数据(如Word、PDF等)的方法,包括...
基于lucene技术的增量索引,实现索引的首次创建,动态增删改
lucene 对 xml建立索引 建立索引就是怎么简单 呵呵
Lucene创建与搜索索引。个人做的流程总结。
lucene 4.7.2支持java 6 ,之后的版本需要java 7以上,创建、删除、修改索引,搜索支持通用对象(可以根据对象类型搜索),可以范围搜索、排序、高亮,希望有所帮助
一步一步跟我学习lucene是对近期做lucene索引的总结,