Elasticsearch进阶笔记第十四篇

125 阅读4分钟

Elasticsearch高手进阶篇(27)

深度探秘搜索技术_深入揭秘lucene的相关度分数算法

整个es的相关度评分的

  • 算法思想

    • TF/IDF
    • vector model
    • boolean model
  • 实际的公式

    • query norm
    • query coordination
    • boost

对相关度评分进行调节和优化的常见的4种方法

  • query-time boost
    • 添加boost,针对于不同的query的请求的权重
 GET /waws/article/_search
 {
   "query": {
     "bool": {
       "should": [
         {
           "match": {
             "title": {
               "query": "java spark",
               "boost": 2
             }
           }
         },
         {
           "match": {
             "content": "java spark"
           }
         }
       ]
     }
   }
 }
 
 {
   "took": 14,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 4,
     "max_score": 1.258609,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 1.258609,
         "_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 spark",
           "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"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1.083544,
         "_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.53484553,
         "_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": "4",
         "_score": 0.310936,
         "_source": {
           "articleID": "QQPX-R-3956-#aD8",
           "userID": 2,
           "hidden": true,
           "postDate": "2017-01-02",
           "tag": [
             "java",
             "elasticsearch"
           ],
           "tag_cnt": 2,
           "view_cnt": 80,
           "title": "this is java, elasticsearch, hadoop blog",
           "content": "elasticsearch and hadoop are all very good solution, i am a beginner",
           "sub_title": "both of them are good",
           "author_first_name": "Robbin",
           "author_last_name": "Li",
           "new_author_last_name": "Li",
           "new_author_first_name": "Robbin"
         }
       }
     ]
   }
 }
  • 重构查询结构
    • 重构查询结果,在es新版本中,影响越来越小了。一般情况下,没什么必要的话,大家不用也行
    • 影响到的是不同层次中query的权重
 GET /waws/article/_search 
 {
   "query": {
     "bool": {
       "should": [
         {
           "match": {
             "content": "java"
           }
         },
         {
           "match": {
             "content": "spark"
           }
         },
         {
           "bool": {
             "should": [
               {
                 "match": {
                   "content": "solution"
                 }
               },
               {
                 "match": {
                   "content": "beginner"
                 }
               }
             ]
           }
         }
       ]
     }
   }
 }
 ​
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 4,
     "max_score": 1.113083,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "4",
         "_score": 1.113083,
         "_source": {
           "articleID": "QQPX-R-3956-#aD8",
           "userID": 2,
           "hidden": true,
           "postDate": "2017-01-02",
           "tag": [
             "java",
             "elasticsearch"
           ],
           "tag_cnt": 2,
           "view_cnt": 80,
           "title": "this is java, elasticsearch, hadoop blog",
           "content": "elasticsearch and hadoop are all very good solution, i am a beginner",
           "sub_title": "both of them are good",
           "author_first_name": "Robbin",
           "author_last_name": "Li",
           "new_author_last_name": "Li",
           "new_author_first_name": "Robbin"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 0.970927,
         "_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 spark",
           "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"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 0.68640786,
         "_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": "3",
         "_score": 0.26742277,
         "_source": {
           "articleID": "JODL-X-1937-#pV7",
           "userID": 2,
           "hidden": false,
           "postDate": "2017-01-01",
           "tag": [
             "hadoop"
           ],
           "tag_cnt": 1,
           "view_cnt": 100,
           "title": "this is elasticsearch blog",
           "content": "i am only an elasticsearch beginner",
           "sub_title": "we have a lot of fun",
           "author_first_name": "Jack",
           "author_last_name": "Ma",
           "new_author_last_name": "Ma",
           "new_author_first_name": "Jack"
         }
       }
     ]
   }
 }
  • negative boost
    • 搜索包含java,不包含spark的doc,但是这样子很死板
    • 搜索包含java,尽量不包含spark的doc,如果包含了spark,不会说排除掉这个doc,而是说将这个doc的分数降低
    • 包含了negative term的doc,分数乘以negative boost,分数降低
 GET /forum/article/_search 
 {
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "content": "java"
           }
         }
       ],
       "must_not": [
         {
           "match": {
             "content": "spark"
           }
         }
       ]
     }
   }
 }
 ​
 {
   "took": 50,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.68640786,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 0.68640786,
         "_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"
         }
       }
     ]
   }
 }

 GET /waws/article/_search 
 {
   "query": {
     "boosting": {
       "positive": {
         "match": {
           "content": "java"
         }
       },
       "negative": {
         "match": {
           "content": "spark"
         }
       },
       "negative_boost": 0.2
     }
   }
 }
 
 {
   "took": 53,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 0.68640786,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 0.68640786,
         "_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": "5",
         "_score": 0.05753642,
         "_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 spark",
           "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"
         }
       }
     ]
   }
 }

negative的doc,会乘以negative_boost,降低分数

  • constant_score
    • 如果你压根儿不需要相关度评分,直接走constant_score加filter,所有的doc分数都是1,没有评分的概念了
 GET /waws/article/_search 
 {
   "query": {
     "bool": {
       "should": [
         {
           "constant_score": {
             "query": {
               "match": {
                 "title": "java"
               }
             }
           }
         },
         {
           "constant_score": {
             "query": {
               "match": {
                 "title": "spark"
               }
             }
           }
         }
       ]
     }
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 4,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 1,
         "_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 spark",
           "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"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1,
         "_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": "4",
         "_score": 1,
         "_source": {
           "articleID": "QQPX-R-3956-#aD8",
           "userID": 2,
           "hidden": true,
           "postDate": "2017-01-02",
           "tag": [
             "java",
             "elasticsearch"
           ],
           "tag_cnt": 2,
           "view_cnt": 80,
           "title": "this is java, elasticsearch, hadoop blog",
           "content": "elasticsearch and hadoop are all very good solution, i am a beginner",
           "sub_title": "both of them are good",
           "author_first_name": "Robbin",
           "author_last_name": "Li",
           "new_author_last_name": "Li",
           "new_author_first_name": "Robbin"
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "1",
         "_score": 1,
         "_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"
         }
       }
     ]
   }
 }