Spinx search query with filtering(带过滤功能的Spinx搜索查询)
问题描述
我需要按POST_CATEGORY_NAME(字符串值)列在Spinx查询中添加额外的筛选, 我的当前索引: source min
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_query = select p.id, p.title, p.description, l.Latitude, l.Longitude FROM post p join location l on l.id = p.location_id
// here I need filter by category name with post_id the same as
// p.id in table post
sql_attr_float = Latitude
sql_attr_float = Longitude
}
我有3个表:POST、LOCATION和POST_CATEGORY
我的数据库关系:1)发布位置一对一,
2)带有POST_CATEGORY的帖子是一对多。
POST_CATEGORY表,此表只有两列:POST_ID和POST_CATEGORY_NAME列,当此表中的POST_ID按距离映射时,我需要按此POST_CATEGORY_NAME进行搜索。
我用这样的查询按位置过滤,它工作得很好:
select *, geodist(48.712002, 2.677411, latitude, longitude) dist from serv1 where match('searchText*') and dist < 20 ;
在我的SELECT查询之后,我希望在结果中包含以下列:
|id|纬度|经度|POST_CATEGORY_NAME|dist
并按POST_CATEGORY_NAME筛选。
所以当我搜索时,我需要这样的内容:
select *, geodist(48.712002, 2.677411, latitude, longitude) dist from serv1 where match('searchText*') and dist < 20 and post_category_name in ("All", "Shop");
请帮帮我。
推荐答案
因为每个帖子(可能)有多个类别,所以必须选择如何编制索引。1)可以保持现在的状态,并且每个帖子都有狮身人面像文档,如果帖子在多个类别中,那么POST_CATEGORY_NAME实际上将包含多个值。
.或者2)可以改为每个帖子类别有一个文档。因此,如果一个文档属于多个类别,则可能会有多个结果。
选项1更简单,但选项2最终会更灵活(可以组合搜索,也可以不组合搜索,但您的狮身人面像查询可能需要一个GROUP BY TO,以获得每个帖子一个结果)
但目前的选项1..。sql_query = SELECT p.id, p.title, p.description, l.Latitude, l.Longitude,
GROUP_CONCAT(c.category_name) AS post_category_name
FROM post p
INNER JOIN location l ON (l.id = p.location_id)
LEFT JOIN category c ON (c.post_id = p.id)
GROUP BY p.id
ORDER BY NULL
sql_field_string = post_category_name
.将类别同时作为字符串属性(用于检索)和字段(用于匹配)
select id, post_category_name , geodist(48.712002, 2.677411, latitude, longitude) dist
from serv1
where match('searchText* @post_category_name All|Shop') and dist < 20;
虽然您可以在WHERE中使用POST_CATEGORY_NAME属性,但如果可以使用全文查询(字段)进行筛选,通常会更好。
这篇关于带过滤功能的Spinx搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!