Forráskód Böngészése

add index/updateByNewWords to update index by new words

sunyj 8 éve
szülő
commit
cc8d5815fe

+ 22 - 6
src/main/java/com/uas/search/controller/IndexController.java

@@ -42,15 +42,25 @@ public class IndexController {
     @RequestMapping("/create")
     @ResponseBody
     public String initIndexes(String tableNames, HttpServletRequest request) {
-        List<String> tableNameList = new ArrayList<>();
-        if (!StringUtils.isEmpty(tableNames)) {
-            String[] strs = tableNames.split(",");
+        return String.format("Indexes created success in %.2fs",
+                indexService.createIndexes(toList(tableNames), true) / 1000.0);
+    }
+
+    /**
+     * 根据逗号进行分隔,将字符串转为 List
+     *
+     * @param input 输入的字符串
+     * @return 转为的 List
+     */
+    private List<String> toList(String input) {
+        List<String> list = new ArrayList<>();
+        if (!StringUtils.isEmpty(input)) {
+            String[] strs = input.split(",");
             for (String str : strs) {
-                tableNameList.add(str.toLowerCase());
+                list.add(str.toLowerCase());
             }
         }
-        return String.format("Indexes created success in %.2fs",
-                indexService.createIndexes(tableNameList, true) / 1000.0);
+        return list;
     }
 
     @RequestMapping("/multiDownloadComponent")
@@ -117,4 +127,10 @@ public class IndexController {
         luceneMessageDao.dequeueLuceneMessage(id);
         return true;
     }
+
+    @RequestMapping("/updateByNewWords")
+    @ResponseBody
+    public List<Object> updateIndexByNewWords(String newWords, HttpServletRequest request) {
+        return indexService.updateIndexByNewWords(toList(newWords));
+    }
 }

+ 54 - 56
src/main/java/com/uas/search/service/IndexService.java

@@ -7,22 +7,20 @@ import java.util.List;
 
 /**
  * 索引处理
- * 
+ *
  * @author sunyj
  * @since 2016年8月5日 下午2:33:58
  */
 public interface IndexService {
 
-	/**
-	 * 创建指定表的索引
-	 * 
-	 * @param tableNames
-	 *            可为空,指定的表,默认创建所有表的索引
-	 * @param componentFromFiles
-	 *            可为空,器件索引数据是否来自文件,默认来自文件
-	 * @return 创建的索引花费总时间 ms
-	 */
-	public Long createIndexes(List<String> tableNames, Boolean componentFromFiles);
+    /**
+     * 创建指定表的索引
+     *
+     * @param tableNames         可为空,指定的表,默认创建所有表的索引
+     * @param componentFromFiles 可为空,器件索引数据是否来自文件,默认来自文件
+     * @return 创建的索引花费总时间 ms
+     */
+    public Long createIndexes(List<String> tableNames, Boolean componentFromFiles);
 
     /**
      * 多线程下载器件的数据至本地文件中,以供建索引用
@@ -49,54 +47,54 @@ public interface IndexService {
      */
     public void multiDownloadGoods(Integer threads, Integer startFileIndex, Integer endFileIndex);
 
-	/**
-	 * 将新对象添加在lucene索引中
-	 * 
-	 * @param obj
-	 *            将添加的对象
-	 * @return 添加的对象
-	 */
-	public Object save(Object obj);
+    /**
+     * 将新对象添加在lucene索引中
+     *
+     * @param obj 将添加的对象
+     * @return 添加的对象
+     */
+    public Object save(Object obj);
 
-	/**
-	 * 将新对象添加在lucene索引中
-	 * 
-	 * @param obj
-	 *            将更新的对象
-	 * @return 更新的对象
-	 */
-	public Object update(Object obj);
+    /**
+     * 将新对象添加在lucene索引中
+     *
+     * @param obj 将更新的对象
+     * @return 更新的对象
+     */
+    public Object update(Object obj);
 
-	/**
-	 * 将对象从lucene索引中删除
-	 * 
-	 * @param obj
-	 *            将删除的对象
-	 * @return 删除的对象
-	 */
-	public Object delete(Object obj);
+    /**
+     * 将对象从lucene索引中删除
+     *
+     * @param obj 将删除的对象
+     * @return 删除的对象
+     */
+    public Object delete(Object obj);
 
-	/**
-	 * 根据自队列消息解析出来的对象,对lucene索引进行添加、更新或删除操作
-	 * 
-	 * @param parsedQueueMessage
-	 *            自队列消息解析出来的对象
-	 * @return 维护的对象
-	 */
-	public List<Object> maintainIndexes(@NotEmpty("parsedQueueMessage") ParsedQueueMessage parsedQueueMessage);
+    /**
+     * 根据自队列消息解析出来的对象,对lucene索引进行添加、更新或删除操作
+     *
+     * @param parsedQueueMessage 自队列消息解析出来的对象
+     * @return 维护的对象
+     */
+    public List<Object> maintainIndexes(@NotEmpty("parsedQueueMessage") ParsedQueueMessage parsedQueueMessage);
 
-	/**
-	 * 根据实体类型名维护出问题的表的索引
-	 * 
-	 * @param tableName
-	 *            需维护的实体类型名
-	 * @param dataId
-	 *            需维护的id
-	 * @param methodType
-	 *            需对索引做何种更改
-	 * @param data 数据
-	 * @return 维护的对象
-	 */
-	public List<Object> maintainIndexes(@NotEmpty("tableName") String tableName, @NotEmpty("dataId") Long dataId, @NotEmpty("methodType") String methodType, String data);
+    /**
+     * 根据实体类型名维护出问题的表的索引
+     *
+     * @param tableName  需维护的实体类型名
+     * @param dataId     需维护的id
+     * @param methodType 需对索引做何种更改
+     * @param data       数据
+     * @return 维护的对象
+     */
+    public List<Object> maintainIndexes(@NotEmpty("tableName") String tableName, @NotEmpty("dataId") Long dataId, @NotEmpty("methodType") String methodType, String data);
 
+    /**
+     * 根据(录入的词典)新词更新索引
+     *
+     * @param newWords 词典新词
+     * @return 更新的对象
+     */
+    List<Object> updateIndexByNewWords(List<String> newWords);
 }

+ 41 - 0
src/main/java/com/uas/search/service/impl/IndexServiceImpl.java

@@ -7,6 +7,7 @@ import com.uas.search.annotation.NotEmpty;
 import com.uas.search.constant.SearchConstants;
 import com.uas.search.constant.model.PageInfo;
 import com.uas.search.constant.model.PageParams;
+import com.uas.search.constant.model.SPage;
 import com.uas.search.dao.*;
 import com.uas.search.exception.SearchException;
 import com.uas.search.jms.JmsListener;
@@ -21,6 +22,8 @@ import com.uas.search.util.SearchUtils;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -857,4 +860,42 @@ public class IndexServiceImpl implements IndexService {
 		return maintainIndexes(parsedQueueMessage);
 	}
 
+    @Override
+    public List<Object> updateIndexByNewWords(List<String> newWords) {
+        List<Object> updatedObjects = new ArrayList<>();
+        updatedObjects.add(updateIndexByNewWords(newWords, SearchConstants.KIND_TABLE_NAME, SearchConstants.KIND_ID_FIELD, SearchConstants.KIND_NAMECN_FIELD));
+        updatedObjects.add(updateIndexByNewWords(newWords, SearchConstants.BRAND_TABLE_NAME, SearchConstants.BRAND_ID_FIELD, SearchConstants.BRAND_NAMECN_FIELD, SearchConstants.BRAND_NAMEEN_FIELD));
+        updatedObjects.add(updateIndexByNewWords(newWords, SearchConstants.COMPONENT_TABLE_NAME, SearchConstants.COMPONENT_ID_FIELD, SearchConstants.COMPONENT_KINDNAME_FIELD));
+        updatedObjects.add(updateIndexByNewWords(newWords, SearchConstants.COMPONENT_TABLE_NAME, SearchConstants.COMPONENT_ID_FIELD, SearchConstants.COMPONENT_BRANDNAMECN_FIELD, SearchConstants.COMPONENT_BRANDNAMEEN_FIELD));
+        return updatedObjects;
+    }
+
+    /**
+     * 根据(录入的词典)新词更新指定表的索引
+     *
+     * @param newWords  词典新词
+     * @param tableName 表名
+     * @param idField   id 字段名称
+     * @param fields    新词可能牵涉到的字段名称
+     * @return 更新的对象
+     */
+    private List<Object> updateIndexByNewWords(List<String> newWords, String tableName, String idField, String... fields) {
+        List<Object> updatedObjects = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(newWords)) {
+            BooleanQuery booleanQuery = new BooleanQuery();
+            for (String newWord : newWords) {
+                for (String field : fields) {
+                    booleanQuery.add(SearchUtils.getBooleanQuery(field, newWord, false, BooleanClause.Occur.MUST), BooleanClause.Occur.SHOULD);
+                }
+            }
+            logger.info(booleanQuery.toString());
+            SPage<Document> documents = SearchUtils.getDocuments(tableName, booleanQuery, null,
+                    SearchConstants.TOP_NUM);
+            for (Document document : documents.getContent()) {
+                updatedObjects.add(maintainIndexes(tableName, Long.parseLong(document.get(idField)), "update", null));
+            }
+        }
+        return updatedObjects;
+    }
+
 }

+ 5 - 3
src/main/java/com/uas/search/util/SearchUtils.java

@@ -75,7 +75,7 @@ public class SearchUtils {
 	 * @return
 	 */
 	public static BooleanQuery getBooleanQuery(String field, String keyword) {
-		return getBooleanQuery(field, keyword, Occur.MUST);
+		return getBooleanQuery(field, keyword, true, Occur.MUST);
 	}
 
 	/**
@@ -85,16 +85,18 @@ public class SearchUtils {
 	 *            搜索的域名
 	 * @param keyword
 	 *            搜索关键词
+     * @param useSmart
+     *            是否以最大粒度进行分词
 	 * @param occur
 	 *            多个Query之间的关系
 	 * @return
 	 */
-	public static BooleanQuery getBooleanQuery(String field, String keyword, Occur occur) {
+	public static BooleanQuery getBooleanQuery(String field, String keyword, boolean useSmart, Occur occur) {
 		if (StringUtils.isEmpty(field) || StringUtils.isEmpty(keyword)) {
 			return null;
 		}
 		BooleanQuery booleanQuery = new BooleanQuery();
-		Analyzer analyzer = new IKAnalyzer(true);
+		Analyzer analyzer = new IKAnalyzer(useSmart);
 		try {
 			TokenStream tokenStream = analyzer.tokenStream(field, keyword);
 			tokenStream.reset();

+ 1 - 0
src/main/webapp/WEB-INF/views/console.html

@@ -96,6 +96,7 @@
 				<li><a target="_blank">listenDetails</a></li>
 				<li><a target="_blank">index/maintain?tableName=v$product$cmpgoods&methodType=update&dataId=115886&data=goId</a></li>
 				<li><a target="_blank">index/dequeue?id=42A34BADEF8A2EBDE050007F01001E6A</a></li>
+				<li><a target="_blank">index/updateByNewWords?newWords=8位,16位</a></li>
 			</ol>
 
 			<h2>4. 文件上传</h2>