Search

The Search Aptitude provides the ability to index a set of documents and search the data using query strings and fuzzy searching.

createIndex

Builds a searchable index out of the supplied documents.

import { search } from '@oliveai/ldk';

const indexName = 'myIndex';
const documents = getDocuments();
const config = getConfig();

search.createIndex(indexName, documents, config).then((myNewIndex) => {
    // We can now search the documents on the myNewIndex object
    // See the Index sub-page for more info on searching
});

// There are many ways you could retrieve or build up your document objects
// for now we've just built up a small set of various first and last names
function getDocuments() {
    return getPeople().map(personToDocument);
}

// See https://open-olive.github.io/loop-development-kit/ldk/javascript/interfaces/config.html
// for info on the config object
function getConfig() {
    return {
        sortBy: ['-lastName', 'firstName'], // Sort by last name descending, then first name ascending
        searchSize: 2,
        exactMatchThreshold: 4,
        beginWithSearch: true,
    };
}

function personToDocument(person) {
    return {
        name: person,
        fields: [
            {
                name: 'firstName',
                displayName: 'First Name',        // Optional
                type: search.FieldType.standard,  // Optional - see https://open-olive.github.io/loop-development-kit/ldk/javascript/enums/fieldtype.html
                                                  // for a description of available types
            },
            {
                name: 'lastName',
                displayName: 'Last Name',
                type: search.FieldType.standard,
            },
        ],
        // Note: The data field must be a stringified array of objects
        data: JSON.stringify([{
            firstName: person.split(' ')[0],
            lastName: person.split(' ')[1],
        }]),
    };
}

function getPeople() {
    return ['George Smith', 'George Jones', 'Alex Smith', 'Geoffrey McPerson', 'Jones Gregory'];
}

openIndex

Open a previously created index with a new configuration object to search upon.

import { search } from '@oliveai/ldk';

const indexName = 'myIndex';
const config = {
    sortBy: ['firstName', 'lastName'], 
    searchSize: 100,
    exactMatchThreshold: 20,
    beginWithSearch: true,
};
  
search.openIndex(indexName, config).then((index) => {
    // We can now query the previously created 'myIndex' using an updated config
});

exists

Checks to see if an index with the given name exists.

import { search } from '@oliveai/ldk';

const indexName = 'myIndex';

search.exists(indexName).then((indexExists) => {
    if (indexExists) {
        // The index exists, we could use openIndex here to begin a search on it
    } else {
        // The index does not exist, perhaps we should create it or throw an error
    }
});

Last updated