虽然docker方便.但是由于163镜像版本太低.所以在本地搭了一个es.版本6.3.0;
首先说下这次研究的方向: 搜索框搜西红柿,那么番茄是出不来的. 搜索米的时候.虾米条比大米的相关度还高
暂时考虑的方案如下:
1.加入同义词
2.相关度优化.将字段排优先级,名字的相关度提升
3.未雨绸缪,增加类似某宝某东一样的,销量高的排名也提升一点,具体如下
同义词插件
插件1 :同义词文件配置方式
插件2 :数据库配置方式
原本想使用插件2. 两种方式都是可以动态加载同义词词库.所以配置好之后不需要修改词库就重启es,
但是插件2的大佬一直没有更新,所以我在6.3.0中加入插件报错我没有处理好暂时先使用插件1
插件2
项目支持6.x版本.但是tag上没有,因此clone到本地.修改版本.然后maven打包.得到插件放到es的plugins下重启es
github上有使用的实例.我的测试用例,这个成功之后,暂时先放一边.先搞下相关度优化
DELETE prod
PUT /prod
{
"index" : {
"analysis" : {
"analyzer" : {
"synonym" : {
"tokenizer" : "ik_max_word",
"filter" : ["remote_synonym"]
}
},
"filter" : {
"remote_synonym" : {
"type" : "dynamic_synonym",
"synonyms_path" : "synonym.txt", //文件没有就创建一个
"interval": 30 //由于本地跑.所以30s重新查一次.线上肯定不可以30s
},
"local_synonym" : {
"type" : "dynamic_synonym",
"synonyms_path" : "synonym.txt"
}
}
}
}
}
将项目中需要优化的几个字段抽取出来自己定义了一个精简版的demo
PUT /prod/demo/_mapping
{
"properties":{
"name":{
"type":"text",
"analyzer": "synonym"
},
"description":{
"type":"text",
"analyzer": "ik_max_word"
},
"brandName":{
"type":"text",
"analyzer": "ik_max_word"
},
"labelName": {
"type": "text",
"analyzer": "ik_max_word"
},
"menuCategoryNamePath": {
"type": "text",
"analyzer": "ik_max_word"
},
"num":{
"type": "integer"
}
}
}
原先的查询代码:
QueryBuilders.multiMatchQuery(((SearchProductReq) req).getSearchContent(),
PRODUCT_NAME,
BRAND_NAME,
DESCRIPTION,
LABEL_NAME,
MENU_CATEGORY_NAME_PATH);
等同于:
GET prod/demo/_search
{
"query":{
"multi_match": {
"query": "米",
"fields": ["name","description","brandName","labelName","menuCategoryNamePath"]
}
}
}
修改后,排分还待修正.暂时按这样的分数:
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("name", "米").boost(0.8f))
.should(QueryBuilders.matchQuery("brandName", "米").boost(0.6f))
.should(QueryBuilders.matchQuery("labelName", "米").boost(0.6f))
.should(QueryBuilders.matchQuery("menuCategoryNamePath", "米").boost(0.2f))
.should(QueryBuilders.matchQuery("description", "米").boost(0.4f));
FieldValueFactorFunctionBuilder factorFunctionBuilder = new FieldValueFactorFunctionBuilder("num");
factorFunctionBuilder.factor(0.1f);
factorFunctionBuilder.modifier(FieldValueFactorFunction.Modifier.LOG1P);
FunctionScoreQueryBuilder boostMode = QueryBuilders
.functionScoreQuery(queryBuilder, factorFunctionBuilder)
.boostMode(CombineFunction.SUM);
SearchRequestBuilder requestBuilder = client.prepareSearch(ESConstant.PRODUCT_INDEX)
.setTypes(ESConstant.PRODUCT_TYPE);
requestBuilder.setQuery(boostMode);
等同于:
GET prod/demo/_search
{
"query":{
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"name": {"query": "大米","boost":0.8}
}
},
{
"match": {
"brandName": {"query": "大米","boost":0.6}
}
},
{
"match": {
"labelName": {"query": "大米","boost":0.6}
}
},
{
"match": {
"description": {"query": "大米","boost":0.5}
},
{
"match": {
"menuCategoryNamePath": {"query": "大米","boost":0.2}
}
}
]
}
},
"field_value_factor": {
"field": "num",
"modifier": "log1p",
"factor": 0.1
},
"boost_mode": "sum"
}
}
}
提升相关度使用function_score参考官方文档
加入同义词,这个需要修改mapping.
PUT /prod/demo/_mapping
{
"properties":{
"name":{
"type":"text",
"analyzer": "synonym"
},
"description":{
"type":"text",
"analyzer": "synonym"
},
"brandName":{
"type":"text",
"analyzer": "synonym"
},
"labelName": {
"type": "text",
"analyzer": "synonym"
},
"menuCategoryNamePath": {
"type": "text",
"analyzer": "synonym"
},
"num":{
"type": "integer"
}
}
}
这时候在config的synonym.txt文件中增加同义词.比如比如插入数据:
POST /prod/demo/2
{
"name":"大米",
"description":"稻香大米",
"brandName":"COCO",
"labelName":"大米",
"menuCategoryNamePath":"食品|饮料",
"num":3
}
POST /prod/demo/3
{
"name":"虾米条",
"description":"虾米条",
"brandName":"COCO",
"labelName":"虾米条",
"menuCategoryNamePath":"食品|零食",
"num":1
}
POST /prod/demo/4
{
"name":"惠宜 珍珠米 10KG",
"description":"惠宜 珍珠米 10KG",
"brandName":"COCO",
"labelName":"惠宜",
"menuCategoryNamePath":"食品|饮料",
"num":5
}
POST /prod/demo/5
{
"name":"口口牌 泰国进口 泰国茉莉香米",
"description":"口口牌 泰国进口 泰国茉莉香米",
"brandName":"COCO",
"labelName":"口口牌",
"menuCategoryNamePath":"食品|饮料",
"num":3
}
ik分词器不会拆大米.所以大米和米是两个条件. 如果把米/大米设置成近义词.name这时候.搜米和大米都能收到上述商品.并且相关度也有有一定变化.
首先没有淘宝京东那么智能,但是由于我刚接触es.暂时先这样优化下,
测试数据.很假,主要测试下效果.