es5.x之前默认使用TF-IDF算法打分,5.x之后默认使用BM-25算法打分
- TF-IDF算法
- TF : term frequency ,检索词在文档中出现的频率 => 检索词 / 文档词总数
- DF : document frequency ,检索词在所有文档中出现的频率
- IDF : inverse document frequency , 即 log(全部文档数/检索词出现过的文档数)
- TF-IDF本质上就是TF加权求和 => 每个term的 TF*IDF的和
lucene中TF-IDF公式
- BM-25
-
ES有TF-IDF 切换成BM-25 后解决了一个问题,就是当一个词的TF无限增加时,算分的分值就会无限增长,而BM-25算法最后会趋向一个稳定的值。
- bool查询
- bool查询包含四个子句: must、should、must_not、filter, 其中must 、 should会影响相关性打分,而must、filter则不会影响相关性打分比如
- bool查询的结构会影响算分的比如
GET movies_new/_search { "query": { "bool": { "should": [ {"term": { "title.ik_max_word": { "value": "love" } }}, { "term": { "title.ik_max_word": { "value": "in" } } }, { "bool": { "should": [ {"term": { "title.ik_max_word": { "value": "i" } }} ] } } ] } } }
外层的shuould下的term查询条件的权重是一致的,而内层should下的term权重是一致的,大概内层两个term的评分相加才能抵上外层一个term的权重。
-
dis_max最佳匹配查询
GET movies_new/_search { "explain": true, "query": { "dis_max": { "tie_breaker": 0.7, "boost": 1.2, "queries": [ { "match": { "title.ik_max_word": "love" } }, { "match": { "title.ik_max_word": "in" } } ] } } }
返回两个条件中分值最高的一个作为相关性打分,其中可以配置权重boost,以及tie_beaker,用来区别最高分文档与其他文档,以防混淆。
10-30更
- 单字符串多字段查询
三种场景 :
- 最佳字段(Best Fields) : 字段间相互竞争,又相互关联,评分要取自最匹配字段。
- 多字段(Most Fields) : 处理英文内容时,常见手段为再主字段进行英文分词(English Analyzer),抽取词干,加入同义词,用来匹配更多的文档。相同的问嗯,加入子字段标准分词(Standard Analyzer),以提供更加精准的匹配,其他字段作为匹配文档提高相关度的信号,匹配字段越多则越好。
- 混合字段(Cross Field): 查询词项出现再多个字段中,我们希望在多个字段中找到更多可能的词。
10-31更
越临近双十一,越忙。
- hanlp分词插件
POST _analyze { "analyzer": "hanlp_nlp", "text": ["共和国","剑桥分析公司多位高管对卧底记者说,他们确保了唐纳德"] }
分词结果
{ "tokens" : [ { "token" : "共和国", "start_offset" : 0, "end_offset" : 7, "type" : "ns", "position" : 0 }, { "token" : "剑桥", "start_offset" : 8, "end_offset" : 10, "type" : "nsf", "position" : 1 }, { "token" : "分析", "start_offset" : 10, "end_offset" : 12, "type" : "vn", "position" : 2 }, { "token" : "公司", "start_offset" : 12, "end_offset" : 14, "type" : "nis", "position" : 3 }, { "token" : "多", "start_offset" : 14, "end_offset" : 15, "type" : "a", "position" : 4 }, { "token" : "位", "start_offset" : 15, "end_offset" : 16, "type" : "q", "position" : 5 }, { "token" : "高管", "start_offset" : 16, "end_offset" : 18, "type" : "nr", "position" : 6 }, { "token" : "对", "start_offset" : 18, "end_offset" : 19, "type" : "p", "position" : 7 }, { "token" : "卧底", "start_offset" : 19, "end_offset" : 21, "type" : "v", "position" : 8 }, { "token" : "记者", "start_offset" : 21, "end_offset" : 23, "type" : "nnt", "position" : 9 }, { "token" : "说", "start_offset" : 23, "end_offset" : 24, "type" : "v", "position" : 10 }, { "token" : ",", "start_offset" : 24, "end_offset" : 25, "type" : "w", "position" : 11 }, { "token" : "他们", "start_offset" : 25, "end_offset" : 27, "type" : "rr", "position" : 12 }, { "token" : "确保", "start_offset" : 27, "end_offset" : 29, "type" : "v", "position" : 13 }, { "token" : "了", "start_offset" : 29, "end_offset" : 30, "type" : "ule", "position" : 14 }, { "token" : "唐纳德", "start_offset" : 30, "end_offset" : 33, "type" : "nrf", "position" : 15 } ] }
虽然说是自然语言处理,但我个人觉得,分词行为倒没有ik好用,有些词都没有分。。,还是推荐使用ik分词+pinyin