Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Added Meta Engine support (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz authored Feb 12, 2020
1 parent 6c4a565 commit c498181
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,33 @@ Creating a search key that will only search over the body field.
>>> client = Client(host_identifier, signed_search_key)
```

### Create a Meta Engine

```python
>>> client.create_engine('my-meta-engine', options={
'type': 'meta',
'source_engines': [
'source-engine-1',
'source-engine-2'
]
})
{'source_engines': ['source-engine-1', 'source-engine-2'], 'type': 'meta', 'name': 'my-meta-engine'}
```

### Add a Source Engine to a Meta Engine

```python
>>> client.add_source_engines('my-meta-engine', ['source-engine-3'])
{'source_engines': ['source-engine-1', 'source-engine-2', 'source-engine-3'], 'type': 'meta', 'name': 'my-meta-engine'}
```

### Remove a Source Engine from a Meta Engine

```python
>>> client.remove_source_engines('my-meta-engine', ['source-engine-3'])
{'source_engines': ['source-engine-1', 'source-engine-2'], 'type': 'meta', 'name': 'my-meta-engine'}
```

## Running tests

```python
Expand Down
12 changes: 11 additions & 1 deletion elastic_app_search/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,19 @@ def get_engine(self, engine_name):
"""
return self.session.request('get', "engines/{}".format(engine_name))

def create_engine(self, engine_name, language=None):
def create_engine(self, engine_name, language=None, options=None):
"""
Creates an engine with the specified name.
:param engine_name: Name of the new engine.
:param language: Language of the new engine.
:param options: Engine configuration.
:return: A dictionary corresponding to the new engine.
"""
data = { 'name': engine_name }
if language is not None:
data['language'] = language
if options is not None:
data.update(options)
return self.session.request('post', 'engines', json=data)

def destroy_engine(self, engine_name):
Expand Down Expand Up @@ -289,6 +292,13 @@ def click(self, engine_name, options):
endpoint = "engines/{}/click".format(engine_name)
return self.session.request_ignore_response('post', endpoint, json=options)

def add_source_engines(self, engine_name, source_engines):
endpoint = "engines/{}/source_engines".format(engine_name)
return self.session.request('post', endpoint, json=source_engines)

def remove_source_engines(self, engine_name, source_engines):
endpoint = "engines/{}/source_engines".format(engine_name)
return self.session.request('delete', endpoint, json=source_engines)

@staticmethod
def create_signed_search_key(api_key, api_key_name, options):
Expand Down
3 changes: 3 additions & 0 deletions elastic_app_search/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class RecordAlreadyExists(ElasticAppSearchError):
class BadRequest(ElasticAppSearchError):
"""Raised when bad request"""

def __init__(self, message):
super(ElasticAppSearchError, self).__init__(message)

class Forbidden(ElasticAppSearchError):
"""Raised when http forbidden"""

Expand Down
2 changes: 1 addition & 1 deletion elastic_app_search/request_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def raise_if_error(self, response):
if response.status_code == requests.codes.unauthorized:
raise InvalidCredentials(response.reason)
elif response.status_code == requests.codes.bad:
raise BadRequest()
raise BadRequest(response.text)
elif response.status_code == requests.codes.conflict:
raise RecordAlreadyExists()
elif response.status_code == requests.codes.not_found:
Expand Down
55 changes: 54 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,29 @@ def test_create_engine(self):
with requests_mock.Mocker() as m:
url = "{}/{}".format(self.client.session.base_url, 'engines')
m.register_uri('POST', url, json=expected_return, status_code=200)
response = self.client.create_engine(engine_name, 'en')
response = self.client.create_engine(
engine_name=engine_name, language='en')
self.assertEqual(response, expected_return)

def test_create_engine_with_options(self):
engine_name = 'myawesomeengine'
expected_return = {'name': engine_name, 'type': 'meta',
'source_engines': [
'source-engine-1',
'source-engine-2'
]}

with requests_mock.Mocker() as m:
url = "{}/{}".format(self.client.session.base_url, 'engines')
m.register_uri('POST', url, json=expected_return, status_code=200)
response = self.client.create_engine(
engine_name=engine_name, options={
'type': 'meta',
'source_engines': [
'source-engine-1',
'source-engine-2'
]
})
self.assertEqual(response, expected_return)

def test_destroy_engine(self):
Expand Down Expand Up @@ -487,3 +509,34 @@ def test_click(self):
m.register_uri('POST', url, json={}, status_code=200)
self.client.click(self.engine_name, {
'query': 'cat', 'document_id': 'INscMGmhmX4'})

def test_add_source_engines(self):
target_source_engine_name = 'source-engine-3'
expected_return = {'source_engines': [
'source-engine-1', 'source-engine-2', target_source_engine_name], 'type': 'meta', 'name': self.engine_name}

with requests_mock.Mocker() as m:
url = "{}/{}".format(
self.client.session.base_url,
"engines/{}/source_engines".format(self.engine_name)
)
m.register_uri('POST', url, json=expected_return, status_code=200)
response = self.client.add_source_engines(
self.engine_name, [target_source_engine_name])
self.assertEqual(response, expected_return)

def test_remove_source_engines(self):
source_engine_name = 'source-engine-3'
expected_return = {'source_engines': [
'source-engine-1', 'source-engine-2'], 'type': 'meta', 'name': self.engine_name}

with requests_mock.Mocker() as m:
url = "{}/{}".format(
self.client.session.base_url,
"engines/{}/source_engines".format(self.engine_name)
)
m.register_uri('DELETE', url, json=expected_return,
status_code=200)
response = self.client.remove_source_engines(
self.engine_name, [source_engine_name])
self.assertEqual(response, expected_return)

0 comments on commit c498181

Please sign in to comment.