欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-01-07 08:50:34
...
我们在创建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));