Workplace Search API

edit

Content Sources

edit
# Create a custom content source
client.create_content_source(name: 'test')

# Retrieve a content source by ID
content_source_id = client.create_content_source(name: 'books').body['id']
client.content_source(content_source_id)

# Delete a content source by ID
client.delete_content_source(content_source_id)

# Retrieve all content sources
client.list_content_sources

# Update a custom content source
body = {
  name: new_name,
  schema: { title: 'text', body: 'text', url: 'text' },
  display: { title_field: 'title', url_field: 'url', color: '#f00f00' },
  is_searchable: true
}
client.put_content_source(id, body: body)

Documents

edit
# Index Documents
documents = [
  { id: 'e68fbc2688f1', title: 'Frankenstein; Or, The Modern Prometheus', author: 'Mary Wollstonecraft Shelley' },
  { id: '0682bb06af1a', title: 'Jungle Tales', author: 'Horacio Quiroga' },
  { id: '75015d85370d', title: 'Lenguas de diamante', author: 'Juana de Ibarbourou' },
  { id: 'c535e226aee3', title: 'Metamorphosis', author: 'Franz Kafka' }
]

response = client.index_documents(content_source_id, body: documents)

# Retrieve a document by ID from a specified content source
client.document(content_source_id, document_id: '75015d85370d')

# Delete Documents
client.delete_documents(content_source_id, document_ids: ['e68fbc2688f1', 'c535e226aee3'])

# Delete all documents for a given content source
client.delete_all_documents(content_source_id)

OAuth Authentication

edit

You need to configure the OAuth Application for Search in order to use the Workplace Search client’s search and create_analytics endpoints. You need to follow the steps in Configuring the OAuth Application for Search to retrieve the credentials: Client ID and Client Secret to request access tokens from the authentication server.

client = Elastic::EnterpriseSearch::WorkplaceSearch::Client.new(http_auth: <access_token>)

client_id = <client_id>
client_secret = <client_secret>
redirect_uri = <redirect_uri>

# Get the authorization URL:
client.authorization_url(client_id, redirect_uri)
> https://host:port/ws/oauth/authorize?response_type=code&client_id=client_id&redirect_uri=https%3A%2F%2Flocalhost%3A3002

Open the URL to authorize your application. Successful authorization redirects the user in accordance to the redirect URI provided (and configured for the application). The application server must now request for an access_token, which is generated by Workplace Search using the oauth/token endpoint, using the Client ID and Client Secret.

authorization_code = '<paste code from redirect>'

access_token = client.request_access_token(client_id, client_secret, authorization_code, redirect_uri)

# The access_token can now be used to issue a search request:
client.search(body: {query: 'search query'}, access_token: access_token)

See Search API Overview for more search parameters.

Permissions

edit
# List permissions
client.list_permissions(content_source_id)

# Get a user permissions
response = client.user_permissions(content_source_id, { user: 'enterprise_search' })

# Clear user permissions
client.put_user_permissions(content_source_id, { permissions: [], user: 'enterpise_search' })

# Add permissions to a user
client.add_user_permissions(
  content_source_id,
  { permissions: ['permission1', 'permission2'], user: user }
)

# Updates user permissions
client.put_user_permissions(content_source_id, { permissions: [], user: user })

# Remove permissions from a user
client.remove_user_permissions(
  content_source_id,
  { permissions: ['permission1', 'permission2'], user: user }
)

External Identities

edit
# Create external identities
body = { user: 'elastic_user', source_user_id: '[email protected]' }
client.create_external_identity(content_source_id, body: body)

# Retrieve an external identity
client.external_identity(content_source_id, user: 'elastic_user')

# List external identities
client.list_external_identities(content_source_id)

# Update external identity
body = { source_user_id: '[email protected]' }
client.put_external_identity(content_source_id, user: 'elastic_user', body: body)

# Delete an external identity
client.delete_external_identity(content_source_id, user: 'elastic_user')

Search

edit

You need to set up OAuth Authentication and use the access token for Search. See Search API Reference for available parameters and more details about search.

client.search(body: {query: 'search query'}, access_token: access_token)

Create Analytics Event

edit

You need to set up OAuth Authentication to use analytics events.

body = {
  type: 'click',
  query_id: 'search_query_id',
  document_id: 'document_id',
  page: 1,
  content_source_id: 'content_source_id',
  rank: 1,
  event: 'api'
}

client.create_analytics_event(access_token: oauth_access_token, body: body)