Home > Blockchain >  Fuzzy searching through multiple fields in postgreSQL
Fuzzy searching through multiple fields in postgreSQL

Time:05-20

I've to do fuzzy search on multiple fields (in an attempt to create something like autocomplete similar to product search in amazon). I tried this through ElasticSearch but was wondering if there's something equivalent to it in postgreSQL.

Here's sample code for elasticsearch: (both the fields, title and description, are index as type: text)

GET index_name/_search
{
    "query": {
          "bool": {
                "must": [
                      {
                           "multi_match": {
                                 fields: ["description", "title"],
                                 query: "postgres",
                                 fuzziness: 1
                           }
                      }
                ]
          }
    }
}

I've tried the same using pg_tram in postgreSQL, it worked for one field with similarity() but I don't know how to extend this on multiple fields.

This is what I did in postgreSQL:

select * from table t
   where similarity("title", "postgres") > 0.5;
select * from table t
   where similarity("title", "postgres") > 0.5 OR similarity("description", "postgres") > 0.5;

Will appreciate any help/guidance in this context.
P.S: let me know if my description is missing something.

Thanks :)

CodePudding user response:

I asked the same question in dba.stackexchange and got some answers.

Here's the link to that.

  • Related