GithubHelp home page GithubHelp logo

isabella232 / swiftype-app-search-node Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swiftype/swiftype-app-search-node

0.0 0.0 0.0 123 KB

Swiftype App Search Node.js Client

Home Page: https://swiftype.com/

License: MIT License

JavaScript 100.00%

swiftype-app-search-node's Introduction

Elastic App Search Logo

⚠️ This repository is deprecated ⚠️

Please visit https://github.com/elastic/app-search-node for the up to date version. Thank you! - Elastic

CircleCI buidl GitHub release

A first-party Node.JS client for building excellent, relevant search experiences with Elastic App Search.

Contents


Getting started 🐣

To install this package, run:

npm install swiftype-app-search-node

Usage

Setup: Configuring the client and authentication

Using this client assumes that you have already created an App Search account, and subsequently created an Engine. You'll need to configure the client with the name of your Engine and your authentication credentials.

  • hostIdentifier -> Your Host Identifier, should start with host-
  • apiKey -> Your API Key. You can use any key type with the client, however each has a different scope. For more information on keys, check out the documentation.
const SwiftypeAppSearchClient = require('swiftype-app-search-node')
const hostIdentifier = 'host-c5s2mj'
const apiKey = 'private-mu75psc5egt9ppzuycnc2mc3'
const client = new SwiftypeAppSearchClient(hostIdentifier, apiKey)

Using with App Search Managed Deploys

The client can be configured to use a managed deploy by using the baseUrlFn parameter. Since managed deploys do not rely on a hostIdentifier, it can be omitted.

const apiKey = 'private-mu75psc5egt9ppzuycnc2mc3'
const baseUrlFn = () => 'http://localhost:3002/api/as/v1/'
const client = new SwiftypeAppSearchClient(undefined, apiKey, baseUrlFn)

API Methods

Indexing: Creating and updating Documents
const engineName = 'favorite-videos'
const documents = [
  {
    id: 'INscMGmhmX4',
    url: 'https://www.youtube.com/watch?v=INscMGmhmX4',
    title: 'The Original Grumpy Cat',
    body: 'A wonderful video of a magnificent cat.'
  },
  {
    id: 'JNDFojsd02',
    url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
    title: 'Another Grumpy Cat',
    body: 'A great video of another cool cat.'
  }
]

client
  .indexDocuments(engineName, documents)
  .then(response => console.log(response))
  .catch(error => console.log(error))
Retrieving Documents
const engineName = 'favorite-videos'
const documentIds = ['INscMGmhmX4', 'JNDFojsd02']

client
  .getDocuments(engineName, documentIds)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Destroying Documents
const engineName = 'favorite-videos'
const documentIds = ['INscMGmhmX4', 'JNDFojsd02']

client
  .destroyDocuments(engineName, documentIds)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Listing Engines
client
  .listEngines({ page: { size: 10, current: 1 } })
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Retrieving Engines
const engineName = 'favorite-videos'

client
  .getEngine(engineName)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Creating Engines
const engineName = 'favorite-videos'

client
  .createEngine(engineName)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Destroying Engines
const engineName = 'favorite-videos'

client
  .destroyEngine(engineName)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Searching
const engineName = 'favorite-videos'
const query = 'cat'
const searchFields = { title: {} }
const resultFields = { title: { raw: {} } }
const options = { search_fields: searchFields, result_fields: resultFields }

client
  .search(engineName, query, options)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Multi-Search
const engineName = 'favorite-videos'
const searches = [
  { query: 'cat', options: {
      search_fields: { title: {} },
      result_fields: { title: { raw: {} } }
  } },
  { query: 'grumpy', options: {} }
]

client
  .multiSearch(engineName, searches)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Query Suggestion
const engineName = 'favorite-videos'
const options = {
  size: 3,
  types: {
    documents: {
      fields: ['title']
    }
  }
}

client
  .querySuggestion(engineName, 'cat', options)
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Listing Curations
const engineName = 'favorite-videos'

client
  .listCurations(engineName) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
  
// Pagination details are optional  
const paginationDetails = {
        page: {
          current: 2,
          size: 10
        }
      }

client
  .listCurations(engineName, paginationDetails) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Retrieving Curations
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'

client
  .getCuration(engineName, curationId) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Creating Curations
const engineName = 'favorite-videos'
const newCuration = {
  queries: ['cat blop'], 
  promoted: ['Jdas78932'], 
  hidden: ['INscMGmhmX4', 'JNDFojsd02']
}

client
  .createCuration(engineName, newCuration) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Updating Curations
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'
// "queries" is required, either "promoted" or "hidden" is required. 
// Values sent for all fields will overwrite existing values. 
const newDetails = {
  queries: ['cat blop'], 
  promoted: ['Jdas78932', 'JFayf782']
}

client
  .updateCuration(engineName, curationId, newDetails) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))
Deleting Curations
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'

client
  .destroyCuration(engineName, curationId) 
  .then(response => console.log(response))
  .catch(error => console.log(error.errorMessages))

Running tests

The specs in this project use node-replay to capture responses.

To capture new responses, run tests with the following commands:

nvm use
REPLAY=record npm test

Otherwise:

npm test

FAQ 🔮

Where do I report issues with the client?

If something is not working as expected, please open an issue.

Where can I learn more about App Search?

Your best bet is to read the documentation.

Where else can I go to get help?

You can checkout the Elastic App Search community discuss forums.

Contribute 🚀

We welcome contributors to the project. Before you begin, a couple notes...

License 📗

MIT © Elastic

Thank you to all the contributors!

swiftype-app-search-node's People

Contributors

brianmcgue avatar christopherjwang avatar dependabot[bot] avatar goodroot avatar jasonstoltz avatar jgr avatar kovyrin avatar marshalium avatar orhantoy avatar zumwalt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.