/** * @param ia * @return 返回索引字段是否索引 * @author mawm at 2013-4-15 下午4:30:36 */ private static <T> Index getIndex(SearchableField ia) { if (ia != null) { // 检查注解的值 return ia.index(); } return org.apache.lucene.document.Field.Index.ANALYZED; }
/** * @param ia * @return 返回权重值 * @author mawm at 2013-4-15 下午4:30:24 */ private static <T> float getBoost(SearchableField ia) { if (ia != null) { // 检查注解的值 return ia.boost(); } return 10; }
private static <T> boolean isParseHtml(SearchableField ia) { if (ia != null) { // 检查注解的值 return ia.parseHtml(); } return true; }
/** * @param ia * @return 返回索引字段是否存储 * @author mawm at 2013-4-15 下午4:30:44 */ private static <T> Store getStore(SearchableField ia) { if (ia != null) { // 检查注解的值 return ia.store(); } return org.apache.lucene.document.Field.Store.NO; }
/** * Executes the search * * @return ObservableList<SearchResult> containing the results of the search */ public ObservableList<SearchResult> execute() { ObservableList<SearchResult> results = FXCollections.observableArrayList(); // Loop through all the Searchable objects in the "database" for (Searchable searchable : SearchableItems.getInstance().getSearchables()) { // Check for matches against every string the object allows to be searchable List<SearchableField> searchableFields = searchable.getSearchableStrings(); for (SearchableField searchableField : searchableFields) { // Perform comparison if (searchableField .getFieldValue() .toLowerCase() .matches(".*" + getQueryLowerCase().trim() + ".*")) { results.add(new SearchResult(searchable, getQueryLowerCase().trim())); } break; // Cheats way of only caring about the first element in the array (if there is one) since we // only care about the first searchable string (short name) for this basic search // implementation } } // Sort the results SortStrategy<SearchResult, String, String> sortStrategy = new AnchoredSortStrategy<>(); sortStrategy.setData(getQueryLowerCase().trim()); sortStrategy.setComparableGetter(SearchResult::getResultText); // Sort results alphabetically results.sort( (e1, e2) -> sortStrategy .getComparableGetter() .apply(e1) .compareTo(sortStrategy.getComparableGetter().apply(e2))); // Sort results using sort strategy ArrayList<SearchResult> sorted = new ArrayList<>(sortStrategy.sorted(results)); results.setAll(sorted); return results; }
/** * 创建索引 * * @param bean * @return */ public static <T> Document createDocument(T bean) { Class<?> clzss = bean.getClass(); SearchableBean searchableBean = clzss.getAnnotation(SearchableBean.class); if (searchableBean == null) { return null; } Document doc = new Document(); // Field[] fields = clzss.getDeclaredFields(); Collection<Field> fields = BeanUtils.getAllFieldMap(clzss).values(); int fieldSize = 0; for (Field field : fields) { String name = field.getName(); if (name.equals("serialVersionUID")) continue; SearchableField ia = field.getAnnotation(SearchableField.class); if (ia != null) { fieldSize++; String value = getFieldValue(bean, field); // 对应的属性列名 String fieldName = getFieldName(ia, field); org.apache.lucene.document.Field indexField = null; if (ia.pk() && ia.pkUseDefault()) { // 主键使用默认值 indexField = new org.apache.lucene.document.Field( fieldName, value, org.apache.lucene.document.Field.Store.YES, org.apache.lucene.document.Field.Index.NOT_ANALYZED // 索引不分词 ); } else { // 即使是主键,也使用配置的值 Store store = getStore(ia); Index index = getIndex(ia); indexField = new org.apache.lucene.document.Field(fieldName, value, store, index); } // 设置权重值 float boost = getBoost(ia); indexField.setBoost(boost); doc.add(indexField); } // 处理主键 // Id id = field.getAnnotation(Id.class); // if (id != null) { // // 对象主键也同时进行记录 // // 对应的属性列名 // String fieldName = field.getName(); // String value = getFieldValue(bean, field); // org.apache.lucene.document.Field indexField = new // org.apache.lucene.document.Field( // fieldName, value, // org.apache.lucene.document.Field.Store.YES, // org.apache.lucene.document.Field.Index.NOT_ANALYZED// 索引不分词 // ); // // doc.add(indexField); // } } if (fieldSize > 0) return doc; return null; }
/** * 取得属性的名称 * * @param ia * @param field * @return * @author mawm at 2013-4-16 上午9:10:00 */ public static String getFieldName(SearchableField ia, Field field) { // 对应的属性列名 String fieldName = StringUtils.isBlank(ia.value()) ? field.getName() : ia.value(); return fieldName; }