Create Fulltext Search Views
This page explains how to create a new fulltext search view.
note
Some attributes cannot be set in the Macrometa web console. Only the API allows you to access all fulltext search options.
Prerequisites
- A Macrometa account with sufficient permissions to create search views.
- At least one collection created.
Create a Fulltext Search View
- Web Console
- REST API
- CLI
- Python SDK
- JavaScript SDK
Follow these instructions to create a new fulltext search view using the GDN console web UI.
- Log in to your Macrometa account.
- Click Data > Search Views.
- Click New View.
- Enter a Search View Name.
- In View Type, select Fulltext Search.
- Enter data sources for the search view. You can add multiple collections and fields for the search view
- In the Collection field, select the collection you want to index in the search view.
- Enter a field to be indexed in the search view.
- (Optional) Select a text analyzer or identity analyzer to break up search inputs for improved searching and sorting. If you do not make a selection, then no analyzer is used.
- (Optional) In the Primary Sort section, you can apply sorting to indexed fields. This is the sorting order for each attribute. It cannot be changed after view is created.
- Field - Specify the sorting according to collections and fields in the mapping definition.
- Sort Direction - Set the sorting order to ascending (default) or descending.
- Add Field - Click to add sorting on another field.
- Click Create.
Use our interactive API Reference with code generation in 18 programming languages to Create a Fulltext Search View.
Use our command line interface to Create a Fulltext Search View.
# Import libraries
from c8 import C8Client
# Define constants
URL = "play.paas.macrometa.io"
GEO_FABRIC = "_system"
API_KEY = "<API Key>" # Change this to your API key.
# Choose one of the following methods to access the GDN. API key is recommended.
# Authenticate with API key.
client = C8Client(protocol='https', host=URL, port=443, apikey=API_KEY, geofabric=GEO_FABRIC)
print("Connected to GDN.")
# Update the search view name, collection name, field names, and sorting with the values you want in your search view.
search_view_name = "example_search_view"
collection_name = "your_collection_name"
properties = {
collection_name: {
"fields": {
"title": {"analyzers": ["text_en"]},
"content": {"analyzers": ["text_en"]}
}
}
}
primary_sort = [{"field": "title", "direction": "asc"}]
# Check if collection exists
if not client.has_collection(collection_name):
print(f"Collection '{collection_name}' does not exist.")
else:
list_views = client.list_all_views()
if all(view.get('name') != search_view_name for view in list_views):
# Create the search view if view does not exists
response = client.create_view(search_view_name, properties, primary_sort)
print(f"Successfully created search view: {response['name']}.")
else:
print(f"Search view {search_view_name} already exists.")
// Connect to GDN.
const jsc8 = require("jsc8");
const client = new jsc8({url: "https://play.paas.macrometa.io", apiKey: "<API Key>", fabricName: "_system"});
console.log("Connected to GDN.");
// Define constants.
const collectionName = "example_collection"; // Replace this with your collection name.
const searchViewName = "example_search_view"; // Replace this with your search view name.
// Change the fields, analyzers, and sorting to match what you want in your search view.
const properties = {
[collectionName]: {
"fields": {
"title": {"analyzers": ["text_en"]},
"content": {"analyzers": ["text_en"]}
}
}
};
const primarySort = [{"field": "title", "direction": "asc"}]
// Function to create the search view.
async function createMySearchView () {
if (!await client.hasCollection(collectionName)) {
console.log(`Collection "${collectionName}" does not exist`);
return;
}
let searchView = { "name": "" };
const listOfViews = await client.getListOfViews();
if (listOfViews.result.some(e => e.name === searchViewName)) {
searchView.name = searchViewName;
console.log(`Search view "${searchView.name}" already exists`);
} else {
searchView = await client.createView(searchViewName, properties, primarySort);
console.log(`Successfully created search view "${searchView.name}"`);
}
}
createMySearchView();