lucene 创建索引

@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);
    }
}

你可能感兴趣的:(apache,html,Lucene)