搜索 debug - 判断文档是由哪个查询语句召回的

如何判断用户输入的查询词,是哪部分命中了 ES 索引中的字段?比如用户输入“葡萄苹果”进行搜索,在查询分析过程会对“葡萄苹果”进行分词,并且进行同义词扩展,最终会以用户输入的查询词和扩展出来的同义词,进行分词之后,再查询 ES spuindex 索引的 spu_name_mt 字段,输入如下查询语句,可以追踪究竟是哪些 term 召回了这些文档。

具体原理文档参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-request-body.html#request-body-search-queries-and-filters

{
  "query":{
    "bool" : {
    "must" : [
      {
        "bool" : {
          "should" : [
            {
              "constant_score" : {
                "filter" : {
                  "bool" : {
                    "must" : [
                      {
                        "term" : {
                          "spu_name_mt" : {
                            "value" : "苹果",
                            "boost" : 1.0,
                            "_name":"spu_name_mt_苹果"
                          }
                        }
                      }
                    ],
                    "adjust_pure_negative" : true,
                    "boost" : 1.0
                  }
                },
                "boost" : 4.0
                
              }
            },
            {
              "constant_score" : {
                "filter" : {
                  "bool" : {
                    "must" : [
                      {
                        "term" : {
                          "spu_name_mt" : {
                            "value" : "平安",
                            "boost" : 1.0,
                            "_name":"spu_name_mt_平安果-平安"
                            
                          }
                        }
                        
                      },
                      {
                        "term" : {
                          "spu_name_mt" : {
                            "value" : "果",
                            "boost" : 1.0,
                            "_name":"spu_name_mt_平安果-果"
                          }
                        }
                      }
                    ],
                    "adjust_pure_negative" : true,
                    "boost" : 1.0
                  }
                },
                "boost" : 4.0
              }
            },
            {
              "constant_score" : {
                "filter" : {
                  "bool" : {
                    "must" : [
                      {
                        "term" : {
                          "spu_name_mt" : {
                            "value" : "葡萄",
                            "boost" : 1.0,
                            "_name":"spu_name_mt_葡萄苹果-葡萄"
                          }
                        }
                      },
                      {
                        "term" : {
                          "spu_name_mt" : {
                            "value" : "苹果",
                            "boost" : 1.0,
                            "_name":"spu_name_mt_葡萄苹果-苹果"
                          }
                        }
                      }
                    ],
                    "adjust_pure_negative" : true,
                    "boost" : 1.0
                  }
                },
                "boost" : 10.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "terms" : {
          "delivery_area_ids" : [
            110000,
            110105
          ],
          "_name":"filter_delivery_ared_ids",
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
  }
}

返回的结果如下,从 matched_queries 字段可以看出是哪些词召回了此文档。非常有利于 debug。

{
  "_shards": {
    "total": 4,
    "failed": 0,
    "successful": 4,
    "skipped": 0
  },
  "hits": {
    "hits": [
      {
        "_routing": "450587",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "450587",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-葡萄",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 14
      },
      {
        "_routing": "741864",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "741864",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "2104922",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "2104922",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "7047221",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "7047221",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "6982891",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "6982891",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "5014682774",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "5014682774",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "6410563",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "6410563",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "7112850",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "7112850",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "1472051",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "1472051",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      },
      {
        "_routing": "7100893",
        "_index": "searchspuindex20210712",
        "_type": "_doc",
        "_id": "7100893",
        "matched_queries": [
          "spu_name_mt_苹果",
          "filter_delivery_ared_ids",
          "spu_name_mt_葡萄苹果-苹果"
        ],
        "_score": 4
      }
    ],
    "total": {
      "value": 142,
      "relation": "eq"
    },
    "max_score": 14
  },
  "took": 4,
  "timed_out": false
}
posted @ 2024-04-11 10:54  大熊猫同学  阅读(13)  评论(0编辑  收藏  举报