定义
- 相关性搜索
- 让搜索引擎理解语义
- 影响召回及排序
举例 - //输入休息、住宿,也可以搜到凯悦
休息、住宿 ==》 凯悦
相关性重塑策略
- 查询时除了must这外,引入should
- 修改“召回策略”(查询方法)
1. 引入shoud
"function_score": {
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{"match": {"name": {"query": "住宿","boost": 0.1}}},
{"term": {"category_id": {"value": 2,"boost": 0.1}}}
]
}
},
{"term": {"seller_disabled_flag": 0 }}
]
}
},
...
"score_mode": "sum",
"boost_mode": "sum"
}
},
当前端输入休息时,会返回"酒店(category_id=2) 的类目
2. 构建分词识别器
//分词函数识别器
private Map<String,Object> analyzeCategoryKeyword(String keyWord) throws IOException {
Map<String,Object> res = new HashMap();
Request req = new Request("GET","/shop/_analyze");
JSONObject jsonObjectReq = new JSONObject();
jsonObjectReq.put("field","name");
jsonObjectReq.put("text",keyWord);
req.setJsonEntity(jsonObjectReq.toJSONString());
Response response = restClient.performRequest(req);
String result = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("tokens");
for (Object s : jsonArray) {
String token = ((JSONObject)s).getString("token");
Integer categoryId = getCategoryByToken(token);
res.put(token,categoryId);
}
return res;
}
//由Token得到类目id
private Integer getCategoryByToken(String token){
for (Integer key : categoryWorkMap.keySet()) {
List<String> tokenList = categoryWorkMap.get(key);
if (tokenList.contains(token)){
return key;
}
}
return null;
}
//利用token与category的关系
private Map<Integer,List<String>> categoryWorkMap = new HashMap<>();
@PostConstruct
public void init(){
categoryWorkMap.put(1,new ArrayList<>());//美食
categoryWorkMap.put(2,new ArrayList<>());//酒店
categoryWorkMap.get(1).add("吃饭");
categoryWorkMap.get(1).add("下午茶");
categoryWorkMap.get(2).add("休息");
categoryWorkMap.get(2).add("睡觉");
categoryWorkMap.get(2).add("住宿");
}
3. 根据新建的分词识别器,修改“召回策略’
分为:识别器模式与无识别器模式,两种查询方式
(过程略)
网友评论