I can not understand why index work but I got empty result set.
https://www.db-fiddle.com/f/n9SyXK6GY3va2CZ41jNGQ5/2
I have table:
create table content
(
id bigserial not null constraint content_pk primary key,
created_at timestamp with time zone not null,
form json not null
);
Field form store data in format:
{
"title_text": "Test test",
"content": {
"blocks": [
{
"key": "6j131",
"text": "Lorem ipsum dolor sit amet,"
},
{
"key": "6nml9",
"text": "In tincidunt tincidunt porttitor."
}
],
}
}
I tried create index to search by value from title_text and from concatination of all nodes content->blocks[]->text.
My queries:
(function by sample of https://www.facebook.com/afiskon thak you)
CREATE OR REPLACE FUNCTION make_tsvector(title TEXT, content json)
RETURNS tsvector AS
'
BEGIN
RETURN (setweight(to_tsvector(''simple'', title), ''A'')
|| setweight(to_tsvector(''simple'', STRING_AGG(content ->> ''text'', '' '')), ''B''));
END
'
LANGUAGE 'plpgsql' IMMUTABLE;
(create index query)
DROP INDEX IF EXISTS idx_content__form__title_text_and_block_text;
CREATE INDEX IF NOT EXISTS idx_content__form__title_text_and_block_text
ON content
USING GIST (make_tsvector(
content.form ->> 'title_text',
content.form -> 'content' -> 'blocks'
));
(and check of my query with EXPLAIN)
EXPLAIN
SELECT c.id, c.form ->> 'title_text'
FROM content c,
json_array_elements(c.form -> 'content' -> 'blocks') block
WHERE make_tsvector(
c.form ->> 'title_text',
c.form -> 'content' -> 'blocks'
) @@ to_tsquery('ipsum')
GROUP BY c.id;
and I see index works (!)
HashAggregate (cost=15.12..15.15 rows=2 width=40)
Group Key: c.id
-> Nested Loop (cost=4.41..14.62 rows=200 width=64)
-> Bitmap Heap Scan on content c (cost=4.41..10.62 rows=2 width=64)
Recheck Cond: (make_tsvector((form ->> 'title_text'::text), ((form -> 'content'::text) -> 'blocks'::text)) @@ to_tsquery('ipsum'::text))
-> Bitmap Index Scan on idx_content__form__title_text_and_block_text (cost=0.00..4.40 rows=2 width=0)
Index Cond: (make_tsvector((form ->> 'title_text'::text), ((form -> 'content'::text) -> 'blocks'::text)) @@ to_tsquery('ipsum'::text))
-> Function Scan on json_array_elements block (cost=0.01..1.01 rows=100 width=0)
but if I use this query I will get empty result.
Is it problem of STRING_AGG call in index build function?