我的PostgreSQL中有两个实体
@Entity()
export class Listing extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type: 'varchar', length: 255 })
title: string // For example, 'Best restaurant for work and english learning'
@Column({ type: 'double precision', nullable: true })
latitude: number // For example: 41.87194
@Column({ type: 'double precision', nullable: true })
longitude: number // For example: 12.56738
// Solution 1
// @ManyToMany(() => City, city => city.listing)
// city: City
@OneToMany(() => Offer, offer => offer.listing)
offerList: Offer[]
}
@Entity()
export class Offer extends BaseEntity {
@PrimaryGeneratedColumn() id: number
@Column({ type:'text' })
position: string // For example: 'cashier'
@Column({ type:'int' })
wage: number // For example: 6
@ManyToOne(() => Listing, listing => listing.offerList)
listing: Listing
}如果我100%存储city/county latitude 和 longitude**?**,那么通过city/county过滤的最佳方法是什么?
搜索请求示例:waiter offers in Italy
Solution1:存储在Listing entity附加字段中,如city (和country?)。(实际上,它将是一个多对多(城市<= =>列表)实体,以避免用户混乱,优化SQL搜索请求,并轻松地将新城市添加到production database中,对吗?)
Solution2:当用户键入Italy时,客户端将此查询发送到后端。在此之后,解析并获取longitude和latitude for Italy查询(我假设会出现一些bug,比如strange query)。然后后端开始搜索靠近接收到的地理位置的列表(地点)。搜索级别越近越好
Solution3:你的解决方案..。=)
这里是airbnb搜索引擎的工作原理,他们没有硬编码城市的列表

发布于 2019-01-20 17:16:33
你可以试试https://postgis.net
它增加了对地理对象的支持,允许在SQL中运行位置查询。
SELECT superhero.name
FROM city, superhero
WHERE ST_Contains(city.geom, superhero.geom)
AND city.name = 'Gotham';https://stackoverflow.com/questions/54278399
复制相似问题