Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUIjX-0006nf-9F for pgsql-general@arkaria.postgresql.org; Tue, 04 Dec 2018 21:59:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUIjW-0004DL-0X for pgsql-general@arkaria.postgresql.org; Tue, 04 Dec 2018 21:59:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUIjV-0004D7-Oo for pgsql-general@lists.postgresql.org; Tue, 04 Dec 2018 21:59:57 +0000 Received: from mx2a.mailbox.org ([2001:67c:2050:104:0:2:25:2] helo=mx2.mailbox.org) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUIjS-0004gw-H7 for pgsql-general@postgresql.org; Tue, 04 Dec 2018 21:59:57 +0000 Received: from smtp1.mailbox.org (unknown [IPv6:2001:67c:2050:105:465:1:1:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 1B860A1131; Tue, 4 Dec 2018 22:59:51 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id alO-Br8PPOE1; Tue, 4 Dec 2018 22:59:46 +0100 (CET) Message-ID: <917ede32e461b787966bf1825724edc0bd2787ca.camel@cybertec.at> Subject: Re: querying both text and non-text properties From: Laurenz Albe To: Rob Nikander , Postgres General Date: Tue, 04 Dec 2018 22:59:45 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rob Nikander wrote: > I’ve got an application where I’d like to search a collection of objects > based on various properties, some text and others non-text (bools, enums, > ints, etc). I’ve used full text search before, following the PG docs to > set up a index on a ts_vector. And of course I’ve used normal indexes > before for accelerating basic queries that use non-text columns. > > Any recommendations on combining the two? For example, if I search for > objects where the description text matches [some ts_query] and the > color = red, I can imagine putting the color property into the text > index somehow - maybe with tokens like: ’color_red’, ‘color_blue’, > but for something like an integer … ? You have two options: A combined index: CREATE EXTENSION btree_gin; CREATE INDEX ON fulltext USING gin (to_tsvector('english', doc), color); That is the perfect match for a query with WHERE color = 'red' AND to_tsvector('german', doc) @@ to_tsquery('english', 'word'); But you can also create two indexes: CREATE INDEX ON fulltext USING gin (to_tsvector('english', doc)); CREATE INDEX ON fulltext (color); Then you don't need the extension, and PostgreSQL can still use them for the search, either only one of them if the condition is selective enough, or a "BitmapAnd" of both. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com