简体   繁体   English

双精度Postgres模糊搜索

[英]fuzzy search on doubles postgres

I would like to fuzzy search on decimal numbers instead of strings. 我想模糊搜索十进制数字而不是字符串。 So the idea is searching for 100 should bring a range of 100, 90, 95, 105, 108, 120 number values from rows in database. 因此,想法是搜索100应该从数据库中的行中带出100、90、95、105、108、120个数字值。

I have tried like keyword too but it doesnt work as i wants. 我也尝试过像关键字一样,但它并不能按我的意愿工作。 How can i do a fuzzy search on decimals. 我该如何对小数进行模糊搜索。 thank you 谢谢

Use between . between使用。 The function is an example: 该函数是一个示例:

create or replace function fuzzy_match_numeric
    (number numeric, value numeric, deviation numeric)
returns boolean language sql as $$
    select number between value- value* deviation and value+ value* deviation
$$;

Check for matching the value 100 with deviation of 5%: 检查值100与偏差5%是否匹配:

select
    fuzzy_match_numeric(94, 100, .05) r1,
    fuzzy_match_numeric(95, 100, .05) r2,
    fuzzy_match_numeric(105, 100, .05) r3,
    fuzzy_match_numeric(106, 100, .05) r4

 r1 | r2 | r3 | r4 
----+----+----+----
 f  | t  | t  | f
(1 row)     

I would suggest computing deviation for set of your lookup values and choosing the best candidate. 我建议为您的一组查找值计算偏差并选择最佳候选者。 Below is a sample based on integers, but numeric types will work analogously. 下面是一个基于整数的示例,但是数字类型将类似地工作。

Sample data set: search_table 样本数据集: search_table

postgres=# select * from search_table order by 1;
 value
-------
    90
    95
   100
   101
   103
   105
   108
   120

Sample lookup values set: search_condition 样本查找值集: search_condition

postgres=# select * from search_condition order by 1;
 value
-------
   100
   103
   105

Look for best candidate: 寻找最佳人选:

select 
  distinct on (value) 
  value, 
  lookup_value as best_candidate
from ( 
  select 
    st.value, 
    sc.value as lookup_value, 
    abs(1 - st.value*1.0/sc.value) as deviation 
  from search_table st 
  cross join search_condition sc 
  ) t 
order by value, deviation, best_candidate;

Result: 结果:

 value | best_candidate
-------+----------------
    90 |            100
    95 |            100
   100 |            100
   101 |            100
   103 |            103
   105 |            105
   108 |            105
   120 |            105

In case of ties a lower candidate will be chosen. 如果是平局,将选择较低的候选人。 This can be amended by adding DESC to the best_candidate column within ORDER BY clause, to take the highest candidate. 可以通过将DESC添加到ORDER BY子句中的best_candidate列中以采用最高候选者来进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM