gpt4 book ai didi

mysql - 距离+关键词搜索方案

转载 作者:可可西里 更新时间:2023-11-01 08:36:13 27 4
gpt4 key购买 nike

对于一个房地产网站,我需要实现一个允许搜索文本和距离的搜索机制。
lat 和 lon 记录在单独的列中时,在 MySQL 表上进行距离计算很容易,但房子往往有 LOT true/false 属性。

我需要将所有这些字段存储在数据库中,因为它们需要可编辑,所以我打算使用像 | 这样的简单表格房屋编号 | property | 我在其中存储所有为真(设置)的属性。

这将使我免于创建一个包含数百列的宽得离谱的表,但搜索该数据库将不太可行。

我考虑过在每个房屋的主记录中添加一个类型为 text 的列,其中包含所有 true 属性的字段名。然后我会搜索 human 文本描述和那个文本列,但我觉得这仍然不是最好的方法。

我怎样才能以干净的方式解决这个问题?

提前致谢!

最佳答案

我推荐使用Entity Attribute ValueEAV 模型来存储数据。 http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model (这就是 Wordpress Posts 和 Post Meta 的工作方式)。

所以假设表格如下:

ENTITY_TABLE: (id,title,author,content,date_created,date_modified)
ATTRIBUTE_TABLE: (id, entity_id, akey, avalue)

使用这样的查询:

SELECT e.*, 
MAX( IF(m.akey= 'has_ac', m.avalue, 0) ) as 'has_ac',
MAX( IF(m.akey= 'has_garage', m.avalue, 0) ) as 'has_garage',
MAX( IF(m.akey= 'has_fridge', m.avalue, 0) ) as 'has_fridge',
MAX( IF(m.akey= 'latitude', m.avalue, 0) ) as 'latitude',
MAX( IF(m.akey= 'longitude', m.avalue, 0) ) as 'longitude'
FROM ENTITY_TABLE e
JOIN ATTRIBUTE_TABLE m ON e.id = m.entity_id
WHERE has_ac=1

这将选择实体及其相关属性(has_ac、has_garage、has_fridge、纬度和经度)并要求所有选定实体的 has_ac 等于 1(真)

现在是地理信息:

SELECT e.*, 
MAX( IF(m.akey= 'has_ac', m.avalue, 0) ) as 'has_ac',
MAX( IF(m.akey= 'has_garage', m.avalue, 0) ) as 'has_garage',
MAX( IF(m.akey= 'has_fridge', m.avalue, 0) ) as 'has_fridge',
MAX( IF(m.akey= 'latitude', m.avalue, 0) ) as 'latitude',
MAX( IF(m.akey= 'longitude', m.avalue, 0) ) as 'longitude',
(
3959 *
acos(
cos( radians( MAX( IF(m.akey= 'latitude', m.avalue, 0) ) ) ) *
cos( radians( CUSTOMER_LAT ) ) *
cos( radians( CUSTOMER_LONG ) - radians( MAX( IF(m.akey= 'longitude', m.avalue, 0) ) ) ) +
sin( radians( MAX( IF(m.akey= 'latitude', m.avalue, 0) ) ) ) *
sin( radians( CUSTOMER_LAT ) )
)
) AS distance
FROM ENTITY_TABLE e
JOIN ATTRIBUTE_TABLE m ON e.id = m.entity_id
WHERE has_ac=1
ORDER BY distance ASC

关于mysql - 距离+关键词搜索方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com