I can't figure out how to query a (PostgreSQL) jsonb document by index.
I have the following to create the db schema (in the up.sql):
CREATE TABLE public.photo
(
id BIGSERIAL PRIMARY KEY NOT NULL,
datadoc JSONB NOT NULL,
created_timestamp TIMESTAMP NOT NULL,
modified_timestamp TIMESTAMP NOT NULL
);
CREATE UNIQUE INDEX photo_id_uindex ON public.photo (id);
CREATE INDEX index_tags ON photo USING GIN ((datadoc->'tags') jsonb_path_ops);
CREATE INDEX photo_created_timestamp_uindex ON public.photo (created_timestamp);
CREATE INDEX photo_modified_timestamp_uindex ON public.photo (modified_timestamp);
And, say I'd like to retrieve all datadoc jsonb docs with tags "fashion"...
and I have this code:
let results = photo.filter( ... how to filter here? ... ) .load::<Photo>(&connection) .expect("Error loading photos"); println!("Displaying {} photos", results.len());
but can't figure out how to use the filter function. I tried "like" but it says some of the traits are not satisfied.
I think there are 3 ways to solve my problem:
- Figure out how to use the filter() method as described above
- Figure out how to use raw select statement
- Retrieve all docs, convert to json objects, and then manually filter them (but I think this is not good for performance).
Appreciate if someone could help. Thanks!