搜索系统 基础教程

搜索 query 分析

搜索系统 索引教程

搜索系统 高级教程

搜索系统 排序层

搜索系统 笔记

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/search-query.html

搜索系统 召回层


搜索系统召回层主要基于搜索引擎,针对 query 进行相关的召回,召回策略主要是类目等实体匹配文本相关性,再结合相应的粗排公式。本文主要以当前流行的现成 elasticsearch 作为搜索引擎进行搜索召回层的实战分享。

Query 模板

query 模板即查询模板,把当前 query 分析之后的结果匹配到结构化的 query 语句模板内,对于工程实现及维护扩展都有比较大的优点。query 模版的设计要结合实际的业务,基于类目等实体匹配及文本相关性的原则去实现。

以下是作者在垂类细分领域的搜索场景中,基于 elasticsearch 设计的 query 模板:

{
  "query": {
    "bool": {//  第一层 bool
      "must_not": [//  一些不匹配的字段值
        { "terms": { "boost": 1.0, "status": [0, 2, 3] }},
        { "term": { "type": { "boost": 1.0, "value": 0 }}},
        ......
      ],
      "filter": [//  过滤的匹配字段值
        { "term": { "valid": { "boost": 1.0, "value": 1 }}},
        { "terms": { "boost": 1.0, "source": [1, 4]}}
        ......   //  若 query 词精确识别为类目,也可以在这里加类目 filter,以加快后续匹配的效率等
      ],
      "must": [{//  相关性匹配放在第一层 bool 的 must 子句中
        "bool": {//  第二层 bool,不同切词放在 should 中
          "minimum_should_match": "1",//  至少匹配一种完整切词
          "should": [{
            "bool": {//  第三层 bool,类目等实体匹配和文本相关性
              "minimum_should_match": "1",//  至少匹配一个
              "should": [{
                "bool": {//  第四层 bool,类目等实体匹配
                "minimum_should_match": "1",//  至少一个匹配
                "should": [
                  { "term": { "uid": { "boost": 1.0, "value": 118 }}},
                  { "terms": { "boost": 1.0, "category3": [66, 88]}},
                  ......  //  类目等实体匹配
                ],
                "boost": 1.0,
                "adjust_pure_negative": true,
                "disable_coord": false
                }
              },{
                "bool": {//  第四层 bool 文本相关性,每个 term 都需要匹配
                "minimum_should_match": "100%",//  都匹配
                "should": [{
                  "bool": {//  第五层 bool,term 匹配
                    "minimum_should_match": "1",//  匹配一个即可
                    "should": [{
                      "multi_match": {
                        "query": "elasticsearch",
                        "fields": ["all_string^1.0", "title^4.0", "summary^2.0", "content^1.0"],
                        "minimum_should_match": "100%",
                        "type": "best_fields",
                        "operator": "OR",
                        "zero_terms_query": "NONE",
                        "boost": 1.0,
                        "prefix_length": 0,
                        "lenient": false,
                        "slop": 0,
                        "max_expansions": 50
                      }
                    },{//  若存在 term 的同义词/扩展词
                      "multi_match": {
                        "query": "es",
                        "fields": ["all_string^1.0", "title^4.0", "summary^2.0", "content^1.0"],
                        "minimum_should_match": "100%",
                        "type": "best_fields",
                        "operator": "OR",
                        "zero_terms_query": "NONE",
                        "boost": 1.0,
                        "prefix_length": 0,
                        "lenient": false,
                        "slop": 0,
                        "max_expansions": 50
                      }
                    }],
                    "adjust_pure_negative": true,                
                    "boost": 1.0,
                    "disable_coord": false
                  }
                },{
                  "bool": {//  第五层 bool,term 匹配
                    "minimum_should_match": "1",                
                    "should": [{
                      "multi_match": {
                        "query": "搜索引擎",
                        "fields": ["all_string^1.0", "title^4.0", "summary^2.0", "content^1.0"],
                        "minimum_should_match": "100%",
                        "type": "best_fields",
                        "operator": "OR",
                        "zero_terms_query": "NONE",
                        "boost": 1.0,
                        "prefix_length": 0,
                        "lenient": false,
                        "slop": 0,
                        "max_expansions": 50
                      }
                    }],
                    "adjust_pure_negative": true,
                    "boost": 1.0,
                    "disable_coord": false
                  }
                }],
                "adjust_pure_negative": true,
                "boost": 1.0,
                "disable_coord": false
              }
            }],
            "adjust_pure_negative": true,
            "boost": 1.0,
            "disable_coord": false
          },{
            ...... // 若存在其他的分词情况
          }],
          "adjust_pure_negative": true,
          "boost": 1.0,
          "disable_coord": false
        }
      }],
      "boost": 1.0,
      "disable_coord": false
    }
  }
}