基于SQL索引的搜索
我有一个名为基于SQL索引的搜索,sql,postgresql,search,indexing,Sql,Postgresql,Search,Indexing,我有一个名为Index的表,它有id和value列,其中id是一个自动递增的bigint,value是一个带有英文单词的varchar 我有一个名为Search的表,它与表索引有关系。对于每个搜索,您可以定义它应该在名为Article的表中搜索哪些索引 表文章也与表索引有关系 定义关系的表格包括: 使用列id\u search和id\u index搜索索引 Articles\u索引带有列id\u article和id\u索引 我想找到所有包含相同搜索索引的文章 例如:我有一个搜索,索引为膝上型电
Index
的表,它有id
和value
列,其中id
是一个自动递增的bigint,value
是一个带有英文单词的varchar
我有一个名为Search
的表,它与表索引有关系。对于每个搜索,您可以定义它应该在名为Article
的表中搜索哪些索引
表文章
也与表索引
有关系
定义关系的表格包括:
使用列id\u search
和id\u index
搜索索引
Articles\u索引
带有列id\u article
和id\u索引
我想找到所有包含相同搜索索引的文章
例如:我有一个搜索
,索引为膝上型电脑
和戴尔
,我想检索包含两个索引的所有文章
,而不仅仅是一个索引
到目前为止,我有:
SELECT ai.id_article
FROM articles_indexes AS ai
INNER JOIN searches_indexes AS si
ON si.id_index = ai.id_index
WHERE si.id_search = 1
如何使我的SQL只返回文章
s以及搜索的所有索引
es
编辑:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
样本数据:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
文章:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
搜索:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
索引:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
文章索引:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
Article
withid
1(戴尔笔记本电脑)具有索引
es'dell'、'laptop'、'new'
Article
withid
2(hp笔记本电脑)具有索引
es“笔记本电脑”、“hp”、“未使用”
id_article | id_index
1 | 1
1 | 2
1 | 4
...
2 | 2
2 | 3
2 | 5
...
搜索索引:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
使用id
1搜索
仅包含2个索引
es,“dell”和“laptop”:
id_search | id_index
1 | 1
1 | 2
所需输出:
id | name | description | ...
1 | 'Dell Laptop' | 'New Dell Laptop...' | ...
2 | 'HP Laptop' | 'Unused HP Laptop...' | ...
...
id | name | id_user | ...
1 | 'Dell Laptop Search' | 5 | ...
id | value
1 | 'dell'
2 | 'laptop'
3 | 'hp'
4 | 'new'
5 | 'unused'
...
id_article
1
如果我理解正确,您需要聚合和HAVING
子句。假设索引表中没有重复项:
SELECT ai.id_article
FROM articles_indexes ai INNER JOIN
searches_indexes si
ON si.id_index = ai.id_index
WHERE si.id_search = 1
GROUP BY ai.id_article
HAVING COUNT(*) = (SELECT COUNT(*) FROM searches_indexes si2 WHERE si2.id_search = 1);
这会计算匹配的数量,并确保它与您要查找的数量匹配
我应该补充一点。如果您想同时查找所有搜索,我倾向于这样写:
SELECT si.id_search, ai.id_article
FROM articles_indexes ai INNER JOIN
(SELECT si.*, COUNT(*) OVER (PARTITION BY si.id_index) as cnt
FROM searches_indexes si
) si
ON si.id_index = ai.id_index
GROUP BY si.id_search, ai.id_article, si.cnt
HAVING COUNT(*) = si.cnt;
您可以比较数组。以下是一些例子:
create table article_index(id_article int, id_index int);
create table search_index(id_search int, id_index int);
insert into article_index
select generate_series(1,2), generate_series(1,10);
insert into search_index
select generate_series(1,2), generate_series(1,4);
select
id_article
from article_index
group by id_article
having array_agg(id_index) @> (select array_agg(id_index) from search_index where id_search = 2);
关于postgres中的数组。名为搜索和索引的表。。。您还有哪些其他表-列、位置、选择和值?更新以使其更加清晰。我有两个关系表Searches\u index
和Articles\u index
,它们基本上定义了哪些搜索有哪些索引,哪些文章有哪些索引。您的问题,并添加一些样本数据和基于该数据的预期输出。请发短信,@a_horse_和_no_名字我已经用样本数据更新了我的问题。顺便说一句