Getting Started with Search
This page guides you through creating your first search view.
Prerequisites
- A Macrometa account with sufficient permissions to create search views.
- An address collection created during the Quickstart.
Create a Search View
Refer to the Search Views section for an explanation of how search views function.
- Log in to your Macrometa account.
- Click Search Views.
- Click New View.
- In the Name field, enter
addresses_view
. - Click the plus under Mapping Definition.
- Select the
addresses
collection from the COLLECTION list. - Enter
lastName
infield
and select text_en in the ANALYZER list. - Perform the previous three steps again for
firstName
andemail
. - Click Create.
Your three address collection key values are indexed and ready to query. Macrometa distributes this view index to every location in the global fabrics. If you're curious about the locations, click Dashboard to see the default GeoFabric locations.
Query the View
To query the search view, we can create a query in Macrometa to run it on the fly or save it as a query worker.
On the side menu, click Query Workers.
The code block below is a search query that will allow you to pass in a query parameter to search you index. Copy and paste it in the code editor on line 1.
FOR contact IN addresses_view
SEARCH ANALYZER(
BOOST(STARTS_WITH(contact.firstName, @searchTerm), 3) OR
BOOST(STARTS_WITH(contact.lastName, @searchTerm), 2) OR
BOOST(STARTS_WITH(contact.email, @searchTerm), 1),
"text_en"
)
SORT BM25(contact) DESC
RETURN UNSET(contact, ["_id", "_rev"])Add the word
nemo
to the searchTerm value to the right of the query editor window.Click Run Query. You will see your results show up below the Run Query button.
(Optional) Click Query Info in the Query Result to see what the query did and how long each step took. Pretty cool if you're into performance metrics.
Save the Query as an API Endpoint
When you save a query worker, you can access it via REST API from anywhere in the world in under 50ms.
- Click Save Query.
- Name the query getContactBySearchTerm and then click Save. This saves the query so you can use it again.
- Click Run Query.
- Click API Endpoint.
This query requires a value to be passed with each request, so you must enter one in the bind parameters or else the query will fail with an error.