全局 - 搜索升級 - 分詞搜索

數據庫配置修改:
1. 設置ngram_token_size = 1
2. 設置innodb_ft_server_stopword_table
2.1 建立自定義中止字規則
CREATE TABLE bbavip_stopwords(value VARCHAR(30)) ENGINE = INNODB;
2.2 設置全局使用指定的中止字規則
SET GLOBAL innodb_ft_server_stopword_table = 'bbavip/bbavip_stopwords';
3. 重建fulltext index
4. 規劃指定表的fulltext index
4.1
SET GLOBAL innodb_optimize_fulltext_only=ON;
4.2
OPTIMIZE TABLE product_base;
4.3
SET GLOBAL innodb_ft_aux_table = 'fulltextsearch/product_base';
4.4
select * from information_schema.INNODB_FT_INDEX_TABLE where word = 'a';
4.5
SET GLOBAL innodb_optimize_fulltext_only=OFF;
 
 
1. 合併fullTextSearch分支
2.新增Fulltext index
3. 新增類 繼承 Against 私有 靜態 抽象類
public static class MatchProductBaseInBoolean extends Against {
private static String matchParamDefault = "skuid";
 
public MatchProductBaseInBoolean(String keyword) {
super(null, keyword, matchParamDefault);
}
 
public MatchProductBaseInBoolean(String title, String keyword) {
super(title, keyword, matchParamDefault);
}
}
3.1 配置 matchParamDeafult 爲 要全文索引的列。多個列用","隔開,如(skuid,name,bar_code)
3.2 根據須要實現Agaist構造方法。
3.2.1 title 用來作連表查詢時使用。
3.2.2 keyword 做爲查詢關鍵詞
4. 對外暴露工廠方法
 
public static String buildMatchInBoolean(String matchParamList, String keyWord) {
return new MatchInBoolean(matchParamList, keyWord).toString();
}
 
public static String buildMatchInBoolean(String tableAlias, String matchParamList, String keyWord) {
return new MatchInBoolean(tableAlias, matchParamList, keyWord).toString();
}
 
public static String buildMatchInBooleanWithClazz(String keyWord, Class<? extends Against> clazz) {
try {
return clazz.getConstructor(String.class).newInstance(keyWord).toString();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return "";
}
 
public static String buildMatchInBooleanWithClazz(String tableAlias, String keyWord, Class<? extends Against> clazz) {
try {
return clazz.getConstructor(String.class, String.class).newInstance(tableAlias, keyWord).toString();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return "";
}
4.1 buildMatchInBoolean 適用於在嵌套查詢中在父查詢中使用子查詢的全文索引的狀況,或沒法定義Against衍生類的狀況
Long storeBaseId = (Long) map.get("storeBaseId");
String keyWord = (String) map.get("keyWord");
StringBuffer sql = new StringBuffer();
sql.append("SELECT c.*, e.mobile FROM \n" +
"(\n" +
" SELECT a.id, a.first_name, a.last_name, \n" +
" a.gender, a.age , a.car_amount, a.partner_base_id,\n" +
" b.car_times, b.pay_expenses,\n" +
" a.channel ,\n" +
" b.arrive_time, b.car_id FROM car_owner AS a INNER JOIN owner_car_relevance as b\n" +
" WHERE a.id = b.car_owner_id \n" +
" AND\n" +
" b.store_base_id = #{storeBaseId}) as c LEFT JOIN user_car_relation as d ON c.car_id = d.car_id\n" +
" LEFT JOIN car_owner_user AS e ON d.user_id = e.id\n");
if(StringUtils.isNotEmpty(keyWord)){
sql.append(FullTextUtils.buildMatchInBoolean("c", "first_name,last_name", keyWord));
}
sql.append("ORDER BY id DESC\n");
return sql.toString();
4.2 buildMatchInBooleanWithClazz 適用於能夠定義Against衍生類的狀況。本質上依舊是buildMatchInBoolean方法的調用,只是經過反射處理。
因爲多用於列表查詢,異常沒有向上拋出,調用方須要確保數據的正確性。
if (StringUtils.isNotEmpty(skuid)) {
WHERE(FullTextUtils.buildMatchInBooleanWithClazz(skuid, FullTextUtils.MatchProductBaseInBoolean.class));
}else {
if (StringUtils.isNotEmpty(orderParam)) {
ORDER_BY(orderParam);
} else {
ORDER_BY("id desc");
}
}
5. 使用全文索引時儘可能避免其餘因素的排序,全文索引會有本身的相關性值。添加其餘因素的排序時,返回結果集的順序會被打亂。
相關文章
相關標籤/搜索