`
pan_java
  • 浏览: 280164 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

lucene 创建索引工具类(Annotation运用)

阅读更多
我们在创建lucene索引的时候经常是从数据库取出相关的记录封装成一个JavaBean,然后将JavaBean的相关字段分别再创建索引.如果JavaBean增加和删除一个字段的话,我们必须修改我们创建索引的程序对应的增加和删除索引字段.如果我们索引创建分布不同程序中,这样修改就比较麻烦,下面是我运用Annotation写的一个lucene 创建索引工具类.希望对学习Annotation和创建索引有用.

索引字段声明注解类,可以扩展此类来增加自己想要的相关属性
@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;
	}
}



索引对应javabean定义
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);
    }
}


创建索引
    fsWriter.addDocument(IndexDocumentUtils.createDocument(searchIndex));




1
1
分享到:
评论

相关推荐

    web开发常用jar

    Apache Commons包中的一个,包含了一些Bean工具类类。必须使用的jar包。 commons-collections.jar Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大 commons-lang.jar Apache ...

    java开发常用jar包

    Apache Commons包中的一个,包含了一些Bean工具类类。必须使用的jar包。 commons-collections.jar Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大 commons-lang.jar Apache ...

    JAVA上百实例源码以及开源项目

    6个目标文件,EJB来模拟银行ATM机的流程及操作:获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,并将当前的计数器初始化,调用每一个EJB对象的count()方法,保证Bean正常被激活和钝化,EJB对象是用...

    JAVA上百实例源码以及开源项目源代码

    Java二进制IO类与文件复制操作实例 16个目标文件 内容索引:Java源码,初学实例,二进制,文件复制 Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系...

    java开源包1

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包11

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包2

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包3

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包6

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包5

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包10

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包4

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包8

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包7

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包9

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包101

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    Java资源包01

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

Global site tag (gtag.js) - Google Analytics