Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fuzzy match search example to custom query docs #1555

Merged
merged 3 commits into from
Feb 28, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/graphql/manual/queries/custom-functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,83 @@ as follows:
}
}

Example: Fuzzy match search functions
*************************************

Let's look at an example of a street address text search with support for misspelled queries.

First install the `pg_trgm <https://www.postgresql.org/docs/current/pgtrgm.html>`__ PostgreSQL extension:

.. code-block:: sql

CREATE EXTENSION pg_trgm;

Next create a GIN (or GIST) index in your database for the columns you'll be querying:

.. code-block:: sql

CREATE INDEX address_gin_idx ON property
USING GIN ((unit || ' ' || num || ' ' || street || ' ' || city || ' ' || region || ' ' || postcode) gin_trgm_ops);

And finally create the custom SQL function in the Hasura console:

.. code-block:: plpgsql

CREATE FUNCTION search_property(search text)
RETURNS SETOF property AS $$
SELECT *
FROM property
WHERE
search <% (unit || ' ' || num || ' ' || street || ' ' || city || ' ' || region || ' ' || postcode)
ORDER BY
similarity(search, (unit || ' ' || num || ' ' || street || ' ' || city || ' ' || region || ' ' || postcode)) DESC
LIMIT 5;
$$ LANGUAGE sql STABLE;

Assuming the ``property`` table is being tracked, you can use the custom function as follows:

.. graphiql::
:view_only:
:query:
query {
search_property(
args: {search: "Unit 2, 25 Foobar St, Sydney NSW 2000"}
){
id
unit
num
street
city
region
postcode
}
}
:response:
{
"data": {
"search_property": [
{
"id": 1,
"unit": "UNIT 2",
"num": "25",
"street": "FOOBAR ST",
"city": "SYDNEY",
"region": "NSW",
"postcode": "2000"
},
{
"id": 2,
"unit": "UNIT 12",
"num": "25",
"street": "FOOBAR ST",
"city": "SYDNEY",
"region": "NSW",
"postcode": "2000"
}
]
}
}

Example: PostGIS functions
**************************

Expand Down