Workplace Search API

edit

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)

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

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')

Create Analytics Event

edit

You need to set up OAuth Authentication for 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)