Elasticsearch进阶笔记第八篇

180 阅读5分钟

Elasticsearch高手进阶篇(15)

深度探秘搜索技术_使用most_fields策略进行cross-fields search弊端大揭秘

cross-fields搜索

  • cross-fields搜索一个唯一标识,跨了多个field。比如一个人,标识,是姓名;一个建筑,它的标识是地址。姓名可以散落在多个field中,比如first_name和last_name中,地址可以散落在country,province,city中。

  • 跨多个field搜索一个标识,比如搜索一个人名,或者一个地址,就是cross-fields搜索

初步来说,如果要实现,可能用most_fields比较合适。因为best_fields是优先搜索单个field最匹配的结果,cross-fields本身就不是一个field的问题了

 POST /waws/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"}}
  • 搜索数据
 GET /waws/article/_search
 {
   "query": {
     "multi_match": {
       "query":"Peter Smith",
       "type":"most_fields",
       "fields":[ "author_first_name", "author_last_name" ]
     }
   }
 }
 
 # Peter Smith,匹配author_first_name,匹配到了Smith,这时候它的分数很高,为什么啊???
 # 因为IDF分数高,IDF分数要高,那么这个匹配到的term(Smith),在所有doc中的出现频率要低,author_first_name field中,Smith就出现过1次
 # Peter Smith这个人,doc 1,Smith在author_last_name中,但是author_last_name出现了两次Smith,所以导致doc 1的IDF分数较低
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 0.6931472,
     "hits": [
       {
         "_index": "waws",
         "_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"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "1",
         "_score": 0.5753642,
         "_source": {
           "articleID": "XHDK-A-1293-#fJ3",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-01",
           "tag": [
             "java",
             "hadoop"
           ],
           "tag_cnt": 2,
           "view_cnt": 30,
           "title": "this is java and elasticsearch blog",
           "content": "i like to write best elasticsearch article",
           "sub_title": "learning more courses",
           "author_first_name": "Peter",
           "author_last_name": "Smith"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 0.51623213,
         "_source": {
           "articleID": "DHJK-B-1395-#Ky5",
           "userID": 3,
           "hidden": false,
           "postDate": "2017-03-01",
           "tag": [
             "elasticsearch"
           ],
           "tag_cnt": 1,
           "view_cnt": 10,
           "title": "this is spark blog",
           "content": "spark is best big data solution based on scala ,an programming language similar to java",
           "sub_title": "haha, hello world",
           "author_first_name": "Tonny",
           "author_last_name": "Peter Smith"
         }
       }
     ]
   }
 }

问题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前面

Elasticsearch高手进阶篇(16)

深度探秘搜索技术_使用copy_to定制组合field解决cross-fields搜索弊端

上一讲,我们其实说了,用most_fields策略,去实现cross-fields搜索,有3大弊端,而且搜索结果也显示出了这3大弊端

  • 第一个办法:用copy_to,将多个field组合成一个field

问题其实就出在有多个field,有多个field以后,就很尴尬,我们只要想办法将一个标识跨在多个field的情况,合并成一个field即可。比如说,一个人名,本来是first_name,last_name,现在合并成一个full_name,不就ok了吗

 PUT /waws/_mapping/article
 {
   "properties": {
       "new_author_first_name": {
           "type":"string",
           "copy_to":"new_author_full_name" 
       },
       "new_author_last_name": {
           "type":"string",
           "copy_to":"new_author_full_name" 
       },
       "new_author_full_name": {
           "type":"string"
       }
   }
 }

用了这个copy_to语法之后,就可以将多个字段的值拷贝到一个字段中,并建立倒排索引

 POST /waws/article/_bulk
 { "update": { "_id": "1"}}
 { "doc" : {"new_author_first_name" : "Peter", "new_author_last_name" : "Smith"}}
 { "update": { "_id": "2"}}  
 { "doc" : {"new_author_first_name" : "Smith", "new_author_last_name" : "Williams"}}
 { "update": { "_id": "3"}}
 { "doc" : {"new_author_first_name" : "Jack", "new_author_last_name" : "Ma"}}
 { "update": { "_id": "4"}}
 { "doc" : {"new_author_first_name" : "Robbin", "new_author_last_name" : "Li"}}
 { "update": { "_id": "5"}}
 { "doc" : {"new_author_first_name" : "Tonny", "new_author_last_name" : "Peter Smith"}}
  • 搜索数据
 GET /waws/article/_search
 {
   "query": {
     "match": {
       "new_author_full_name":"Peter Smith"
     }
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 0.62191015,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 0.62191015,
         "_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",
           "new_author_last_name": "Williams",
           "new_author_first_name": "Smith"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "1",
         "_score": 0.51623213,
         "_source": {
           "articleID": "XHDK-A-1293-#fJ3",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-01",
           "tag": [
             "java",
             "hadoop"
           ],
           "tag_cnt": 2,
           "view_cnt": 30,
           "title": "this is java and elasticsearch blog",
           "content": "i like to write best elasticsearch article",
           "sub_title": "learning more courses",
           "author_first_name": "Peter",
           "author_last_name": "Smith",
           "new_author_last_name": "Smith",
           "new_author_first_name": "Peter"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 0.5063205,
         "_source": {
           "articleID": "DHJK-B-1395-#Ky5",
           "userID": 3,
           "hidden": false,
           "postDate": "2017-03-01",
           "tag": [
             "elasticsearch"
           ],
           "tag_cnt": 1,
           "view_cnt": 10,
           "title": "this is spark blog",
           "content": "spark is best big data solution based on scala ,an programming language similar to java",
           "sub_title": "haha, hello world",
           "author_first_name": "Tonny",
           "author_last_name": "Peter Smith",
           "new_author_last_name": "Peter Smith",
           "new_author_first_name": "Tonny"
         }
       }
     ]
   }
 }

效果难以复现。比如官网也会给一些例子,说用什么什么文本,怎么怎么搜索,是怎么怎么样的效果。es版本在不断迭代,这个打分的算法也在不断的迭代。所以我们其实很难说,对类似这几讲讲解的best_fields,most_fields,cross_fields,完全复现出来应有的场景和效果。

期望的是说,比如大家自己在开发搜索应用的时候,碰到需要best_fields的场景,知道怎么做,知道best_fields的原理,可以达到什么效果;碰到most_fields的场景,知道怎么做,以及原理;碰到搜搜cross_fields标识的场景,知道怎么做,知道原理是什么,效果是什么

  • 问题的解决

问题1:只是找到尽可能多的field匹配的doc,而不是某个field完全匹配的doc --> 解决,最匹配的document被最先返回

问题2:most_fields,没办法用minimum_should_match去掉长尾数据,就是匹配的特别少的结果 --> 解决,可以使用minimum_should_match去掉长尾数据

问题3:TF/IDF算法,比如Peter Smith和Smith Williams,搜索Peter Smith的时候,由于first_name中很少有Smith的,所以query在所有document中的频率很低,得到的分数很高,可能Smith Williams反而会排在Peter Smith前面 --> 解决,Smith和Peter在一个field了,所以在所有document中出现的次数是均匀的,不会有极端的偏差