如何赋予完整匹配相对于部分匹配的权重 (PostgreSQL)

发布于 2024-10-10 08:46:23 字数 658 浏览 2 评论 0原文

我有一个查询,它需要输入搜索包含数万个条目(应该几乎是美国的每个城市)的位置表中邮政编码/地区/城市/地铁代码中最匹配的匹配项。我使用的查询是:

select
  metrocode,
  region,
  postalcode,
  region_full,
  city
from
  dv_location
where
(
  region ilike '%Chicago%'
 or
  postalcode ilike '%Chicago%'
 or
  city ilike '%Chicago%'
 or
  region_full ilike'%Chicago%'
)

 and metrocode is not null

奇怪的是,我得到的结果集看起来像这样:

metrocode;region;postalcode;region_full;city
862;CA;95712;California;Chicago Park
862;CA;95712;California;Chicago Park
602;IL;60611;Illinois;Chicago
602;IL;60610;Illinois;Chicago

我做错了什么?我的想法是,芝加哥比芝加哥公园具有更大的权重,因为芝加哥与该术语完全匹配(尽管我要求对该术语进行通配符匹配)。

I've got a query that takes an input searches for the closet match in zipcode/region/city/metrocode in a location table containing a few tens of thousands of entries (should be nearly every city in the US). The query I'm using is:

select
  metrocode,
  region,
  postalcode,
  region_full,
  city
from
  dv_location
where
(
  region ilike '%Chicago%'
 or
  postalcode ilike '%Chicago%'
 or
  city ilike '%Chicago%'
 or
  region_full ilike'%Chicago%'
)

 and metrocode is not null

Odd thing is, the results set I'm getting back looks like this:

metrocode;region;postalcode;region_full;city
862;CA;95712;California;Chicago Park
862;CA;95712;California;Chicago Park
602;IL;60611;Illinois;Chicago
602;IL;60610;Illinois;Chicago

What am I doing wrong? My thinking is that Chicago would have greater weight than Chicago Park since Chicago is an exact match to the term (even though I'm asking for a wildcard match on the term).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2024-10-17 08:46:23

您可以在 order by 子句中显式设置顺序。

...
order by
case
  when city ilike 'Chicago' then 1
  when city ilike 'Chicago%' then 2
  when city ilike '%Chicago%' then 3
  else 4
end

或者您希望对结果进行排序...

You can set the order explicitly in the order by clause.

...
order by
case
  when city ilike 'Chicago' then 1
  when city ilike 'Chicago%' then 2
  when city ilike '%Chicago%' then 3
  else 4
end

or however you would like your results to be ordered ...

野却迷人 2024-10-17 08:46:23

试试这个:

select
  metrocode,
  region,
  postalcode,
  region_full,
  city,
  (region = 'Chicago'
   OR postalcode = 'Chicago'
   OR city = 'Chicago'
   OR region_full = 'Chicago') AS full_match
from
  dv_location
where
(
  region ilike '%Chicago%'
 or
  postalcode ilike '%Chicago%'
 or
  city ilike '%Chicago%'
 or
  region_full ilike'%Chicago%'
)
 and metrocode is not null
 order by full_match desc;

Try this:

select
  metrocode,
  region,
  postalcode,
  region_full,
  city,
  (region = 'Chicago'
   OR postalcode = 'Chicago'
   OR city = 'Chicago'
   OR region_full = 'Chicago') AS full_match
from
  dv_location
where
(
  region ilike '%Chicago%'
 or
  postalcode ilike '%Chicago%'
 or
  city ilike '%Chicago%'
 or
  region_full ilike'%Chicago%'
)
 and metrocode is not null
 order by full_match desc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文