Elasticsearch:运用 Pinned query 来提升特定的结果

1023 篇文章 596 订阅

作者:Sagar Patel, Developer and Community Member

在我之前的文章 “Elasticsearch: 运用 Pinned query 来提高文档的排名 (7.5发行版新功能)”,我已经详述这个话题了。不过在今天的这篇文章中,它有一些额外的一些知识点可以让我们更加深入地了解这个 Pinned query。算是对之前的文章做一个补充吧。

在本文中,通过实际用例了解 Elasticsearch 中的 pinned query 如何为你服务。

什么是 pinned query,我们该如何运用它?

 

Pinned query 首先在 Elasticsearch 7.4 中发布,用于将所选文档或文档提升到位于特定查询结果顶部的任何其他文档之上。 它可用于文档提升或引导搜索,即使相关性得分较低,某些文档也必须出现在顶部结果中。

以下是 pinned query 的一些实际用例:

  • 你正在开发一个知识中心并监控用户对每篇文章的点击。 给定查询的最常点击文章应显示在页面顶部。
  • 你想在搜索顶部显示新推出的产品作为促销。 此外,对于某些产品,你希望将此功能用于季节性营销。

Pinned query 使用

让我们首先使用示例产品数据集来开发我们的 pinned query。

示例数据索引

PUT product/_bulk
{"index":{"_id":"prod101"}}
{"brand":"Apple","model":"iphone 11","price":800}
{"index":{"_id":"prod102"}}
{"brand":"Samsung","model":"galaxy m32","price":700}
{"index":{"_id":"prod103"}}
{"brand":"Apple","model":"iphone 13","price":1000}
{"index":{"_id":"prod104"}}
{"brand":"Apple","model":"iphone 12","price":900}
{"index":{"_id":"prod105"}}
{"brand":"Apple","model":"iphone 7","price":400}

没有使用 pinned query 的搜索

POST product/_search?filter_path=**.hits
{
  "query": {
    "match": {
      "model": "iphone"
    }
  }
}

上面命令的响应为:

{
  "hits": {
    "hits": [
      {
        "_index": "product",
        "_id": "prod101",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 11",
          "price": 800
        }
      },
      {
        "_index": "product",
        "_id": "prod103",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 13",
          "price": 1000
        }
      },
      {
        "_index": "product",
        "_id": "prod104",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 12",
          "price": 900
        }
      },
      {
        "_index": "product",
        "_id": "prod105",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 7",
          "price": 400
        }
      }
    ]
  }
}

目前,你可以看到产品按相关性排序,并且最近推出的项目不会出现在顶部。 现在让我们看看如何使用 pinned query 在顶部推广最近发布的产品。

Pinned query 示例

在这里,我们正在努力推广最新的 iPhone。

POST product/_search?filter_path=**.hits
{
  "query": {
    "pinned": {
      "ids": [
        "prod103",
        "prod104"
      ],
      "organic": {
        "match": {
          "model": "iphone"
        }
      }
    }
  }
}

上面的命令返回如下的结果:

{
  "hits": {
    "hits": [
      {
        "_index": "product",
        "_id": "prod103",
        "_score": 1.7014124e+38,
        "_source": {
          "brand": "Apple",
          "model": "iphone 13",
          "price": 1000
        }
      },
      {
        "_index": "product",
        "_id": "prod104",
        "_score": 1.7014122e+38,
        "_source": {
          "brand": "Apple",
          "model": "iphone 12",
          "price": 900
        }
      },
      {
        "_index": "product",
        "_id": "prod101",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 11",
          "price": 800
        }
      },
      {
        "_index": "product",
        "_id": "prod105",
        "_score": 0.2876821,
        "_source": {
          "brand": "Apple",
          "model": "iphone 7",
          "price": 400
        }
      }
    ]
  }
}

在 Elasticsearch 返回的结果之上,你可以分别看到 iPhone 13 和 iPhone 12,其他结果直接位于它们下方。

如何使用具有多个索引的 pinned query


如果你想跨多个索引搜索产品,请使用下面的查询和索引名称。

GET /_search
{
  "query": {
    "pinned": {
      "docs": [
        {
          "_index": "product-01",
          "_id": "prod101"
        },
        {
          "_index": "product-01",
          "_id": "prod102"
        },
        {
          "_index": "product-02",
          "_id": "prod105"
        }
      ],
      "organic": {
        "match": {
          "model": "iphone"
        }
      }
    }
  }
}

注意:请注意,你最多只能为 pinned query 提供 100 个 ID。

在以下情况下, pinned query 结果将不存在


Elasticsearch 默认根据相关性返回文档; 但是,假设对于某些查询,你需要特定的排序选项,例如基于日期、产品数量等。在这种情况下,你不会收到固定结果,因为排序没有考虑相关性。

让我们尝试推广更便宜的 iPhone 7 和 iPhone 11 并使用价格排序。

POST product/_search?filter_path=**.hits
{
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "pinned": {
      "ids": [
        "prod105",
        "prod101"
      ],
      "organic": {
        "match": {
          "model": "iphone"
        }
      }
    }
  }
}

上面命令返回的结果是:

{
  "hits": {
    "hits": [
      {
        "_index": "product",
        "_id": "prod103",
        "_score": null,
        "_source": {
          "brand": "Apple",
          "model": "iphone 13",
          "price": 1000
        },
        "sort": [
          1000
        ]
      },
      {
        "_index": "product",
        "_id": "prod104",
        "_score": null,
        "_source": {
          "brand": "Apple",
          "model": "iphone 12",
          "price": 900
        },
        "sort": [
          900
        ]
      },
      {
        "_index": "product",
        "_id": "prod101",
        "_score": null,
        "_source": {
          "brand": "Apple",
          "model": "iphone 11",
          "price": 800
        },
        "sort": [
          800
        ]
      },
      {
        "_index": "product",
        "_id": "prod105",
        "_score": null,
        "_source": {
          "brand": "Apple",
          "model": "iphone 7",
          "price": 400
        },
        "sort": [
          400
        ]
      }
    ]
  }
}

它应该出现在顶部,因为我们正在尝试推广 iPhone 7 和 iPhone 11。但是,在这种情况下,将不会考虑 pinned query,因为我们按 price 降序排序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值