es常用搜索的时候分词为1个字的时候会召回较多不太准确的内容,为增加召回准确性,设定分词去掉单个字的内容,需要自定义分词器,比如使用ik_max_word,可以自定义之后为其增加filter,设置为最少长度为2,即
"filter": {
"len": {
"type": "length",
"min": 2
}
}
然后在自定义的分词器进行引用
es默认使用tf/idf算法,简单理解就是当一个字段的内容越多,命中同样内容的时候,权重会减小,但是有时候不想被内容长度进行影响,比如标签,就根据命中的内容的多少来确定,这时候可以通过调整BM25参数变为0来让内容长度不再影响评分
"similarity": {
"BM25_b_0": {
"type": "BM25",
"b": "0.0"
}
}
然后在需要的字段进行引用,最后完整的示例如下
PUT test
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"custom_max_word": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["len"]
}
},
"filter": {
"len": {
"type": "length",
"min": 2
}
}
},
"similarity": {
"BM25_b_0": {
"type": "BM25",
"b": "0.0"
}
}
}
},
"mappings" : {
"properties" : {
"content" : {
"type" : "text",
"analyzer" : "custom_max_word"
},
"id" : {
"type" : "long"
},
"tagNameList" : {
"type" : "text",
"analyzer" : "custom_max_word",
"similarity": "BM25_b_0"
},
"title" : {
"type" : "text",
"analyzer" : "custom_max_word"
}
}
}
}