15.most_fields策略进行cross-fields search
主要知识点:
cross-fields 的使用场景
cross-fields 使用方法
cross-fields 的缺点
一、cross-fields 的使用场景
cross-fields搜索,一个唯一标识可能存在于多个field。比如一个人的标识是姓名;一个建筑的标识是地址。姓名可以分步在多个field中,比如first_name和last_name中,地址可以分步在country,province,city中。此时做标识符搜索的话就必须跨多个field搜索同一个标识,比如搜索一个人名,或者一个地址,这就是cross-fields搜索。初步来说,如果要实现cross-fields,用most_fields比较合适。因为best_fields是优先搜索单个field最匹配的结果,cross-fields本身就不是一个field的问题了。
二、cross-fields 使用方法
1、准备数据
POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"author_first_name" : "Peter", "author_last_name" : "Smith"} }
{ "update": { "_id": "2"} }
{ "doc" : {"author_first_name" : "Smith", "author_last_name" : "Williams"} }
{ "update": { "_id": "3"} }
{ "doc" : {"author_first_name" : "Jack", "author_last_name" : "Ma"} }
{ "update": { "_id": "4"} }
{ "doc" : {"author_first_name" : "Robbin", "author_last_name" : "Li"} }
{ "update": { "_id": "5"} }
{ "doc" : {"author_first_name" : "Tonny", "author_last_name" : "Peter Smith"} }
2、进行搜索
GET /forum/article/_search
{
"query": {
"multi_match": {
"query": "Peter Smith",
"type": "most_fields",
"fields": [ "author_first_name", "author_last_name" ]
}
}
}
执行结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.6931472,
"hits": [
{
"_index": "forum",
"_type": "article",
"_id": "2",
"_score": 0.6931472,
"_source": {
"articleID": "KDKE-B-9947-#kL5",
"userID": 1,
"hidden": false,
"postDate": "2017-01-02",
"tag": [
"java"
],
"tag_cnt": 1,
"view_cnt": 50,
"title": "this is java blog",
"content": "i think java is the best programming language",
"sub_title": "learned a lot of course",
"author_first_name": "Smith",
"author_last_name": "Williams"
}
},
三、cross-fields 的缺点
1、只是找到尽可能多的field匹配的doc,而不是某个field完全匹配的doc
2、most_fields,没办法用minimum_should_match去掉长尾数据,就是匹配的特别少的结果
3、TF/IDF算法的计算可能和我们预期有差异,比如Peter Smith和Smith Williams,搜索Peter Smith的时候,由于first_name中很少有Smith的,所以query在所有document中的频率很低,得到的分数很高,可能Smith Williams反而会排在Peter Smith前面。