class TestIndexes: client = meilisearch.Client("http://127.0.0.1:7700", "123") """ Client """ def test_get_client(self): """Tests a call to get a client instance of meilisearch""" client = meilisearch.Client("http://127.0.0.1:7700", "123") assert client.config def test_get_client_without_apikey(self): """Tests a call to get a client instance of meilisearch""" client = meilisearch.Client("http://127.0.0.1:7700") assert client.config """ health route """ def test_health(self): """Tests an API call to check the health of meilisearch""" response = self.client.health() assert response.status_code == 204 """ sys-info route """ def test_get_sys_info(self): """Tests an API call to check the system information of meilisearch""" response = self.client.get_sys_info() assert 'memoryUsage' in response """ version route """ def test_get_version(self): """Tests an API call to get the version of meilisearch""" response = self.client.get_version() assert 'pkgVersion' in response """ index route """ def test_create_index(self): """Tests an API call to create an index in meiliSearch""" index = self.client.create_index(name="movies", uid="movies_uid") # TODO : test creating index with schema assert isinstance(index, object) assert index.name == "movies" assert index.uid == "movies_uid" def test_get_indexes(self): """Tests an API call to get all indexes in meiliSearch""" response = self.client.get_indexes() assert isinstance(response, list) def test_get_index_with_name(self): """Tests an API call to get one index with name in meiliSearch""" response = self.client.get_index(name="movies") assert isinstance(response, object) def test_get_index_with_uid(self): """Tests an API call to get one index with uid in meiliSearch""" response = self.client.get_index(uid="movies_uid") assert isinstance(response, object) def test_index_info(self): """Tests an API call to get an index's info in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.info() assert isinstance(response, object) def test_update_index(self): """Tests an API call to update an index in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.update(name="movie") assert isinstance(response, object) """ schema route """ def test_update_schema(self): """Tests an API call to update an schema in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.update_schema({ 'id': ['indexed', 'displayed', 'identifier'], 'title': ['displayed', 'indexed'], 'poster': ['displayed', 'indexed'], 'overview': ['indexed', 'displayed'], 'release_date': ['indexed', 'displayed'] }) assert isinstance(response, object) assert 'updateId' in response def test_update_existing_schema(self): """Tests an API call to update an already existing schema in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.update_schema({ 'id': ['indexed', 'displayed', 'identifier'], 'title': ['displayed', 'indexed'], 'poster': ['displayed', 'indexed'], 'overview': ['indexed', 'displayed'], 'release_date': ['indexed', 'displayed', 'ranked'] }) assert isinstance(response, object) assert 'updateId' in response """ updates route """ def test_get_updates(self): """Tests a call to get updates of a given index""" index = self.client.get_index(uid="movies_uid") response = index.get_updates() assert isinstance(response, list) assert 'status' in response[0] def test_get_update(self): """Tests a call to get an update of a given operation""" index = self.client.get_index(uid="movies_uid") response = index.get_update(0) assert isinstance(response, object) assert 'status' in response assert response['status'] == 'processed' """ documents route """ def test_add_documents(self): """Tests a call to add documents""" json_file = open('./datasets/small_movies.json') data = json.load(json_file) index = self.client.get_index(uid="movies_uid") response = index.add_documents(data) assert isinstance(response, object) assert 'updateId' in response def test_get_documents(self): index = self.client.get_index(uid="movies_uid") time.sleep(1) response = index.get_documents({ 'offset': 1, 'limit': 5, 'attributesToRetrieve': 'title' }) assert isinstance(response, list) assert 'title' in response[0] assert 'overview' not in response[0] def test_get_document(self): time.sleep(1) index = self.client.get_index(uid="movies_uid") response = index.get_document(500682) assert isinstance(response, object) assert 'title' in response assert response['title'] == "The Highwaymen" """ search route """ def test_search_documents(self): index = self.client.get_index(uid="movies_uid") response = index.search({'q': 'How to Train Your Dragon'}) assert isinstance(response, object) assert response["hits"][0]["id"] == '166428' """ stats route """ def test_get_all_stats(self): response = self.client.get_all_stats() assert isinstance(response, object) assert 'databaseSize' in response def test_get_stats(self): index = self.client.get_index(uid="movies_uid") response = index.get_stats() assert isinstance(response, object) assert 'numberOfDocuments' in response assert response['numberOfDocuments'] == 30 """ stop-words route """ def test_add_stop_words(self): index = self.client.get_index(uid="movies_uid") response = index.add_stop_words(['the', 'and']) assert isinstance(response, object) assert 'updateId' in response def test_get_stop_words(self): index = self.client.get_index(uid="movies_uid") response = index.get_stop_words() assert isinstance(response, list) def test_delete_stop_words(self): index = self.client.get_index(uid="movies_uid") response = index.delete_stop_words(['the']) assert isinstance(response, object) assert 'updateId' in response """ key route """ def test_create_key(self): response = self.client.create_key({ "expiresAt": 1575985008, "description": "search key", "acl": ["documentsRead"], "indexes": ["movies"] }) assert 'key' in response assert response['description'] == "search key" def test_get_keys(self): response = self.client.get_keys() assert isinstance(response, list) assert response[0]['description'] == "search key" def test_update_key(self): keys = self.client.get_keys() response = self.client.update_key( keys[0]["key"], { "description": "search key updated", "acl": ["documentsRead"], "indexes": ["movies"] }) assert 'key' in response assert response['description'] == "search key updated" def test_get_key(self): keys = self.client.get_keys() response = self.client.get_key(keys[0]["key"]) assert isinstance(response, object) assert keys[0]['description'] == "search key updated" def test_delete_key(self): keys = self.client.get_keys() response = self.client.delete_key(keys[0]["key"]) assert response.status_code == 204 """ settings route """ def test_add_settings(self): """Tests an API call to add setting to an index in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.add_settings({ "rankingOrder": [ "_sum_of_typos", "_number_of_words", "_word_proximity", "_sum_of_words_attribute", "_sum_of_words_position", "_exact", "release_date" ], "distinctField": "", "rankingRules": { "release_date": "dsc" } }) assert isinstance(response, object) assert 'updateId' in response def test_update_settings(self): """Tests an API call to update settings of an index in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.replace_settings({ "rankingOrder": [ "_sum_of_typos", "_number_of_words", "_word_proximity", "_sum_of_words_attribute", "_sum_of_words_position", "_exact", "release_date" ], "distinctField": "", "rankingRules": { "release_date": "dsc" } }) assert isinstance(response, object) assert 'updateId' in response def test_get_settings(self): """Tests an API call to get settings of an index in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.get_settings() assert isinstance(response, object) assert 'rankingOrder' in response """ update route """ def test_update_documents(self): json_file = open('./datasets/small_movies.json') data = json.load(json_file) index = self.client.get_index(uid="movies_uid") response = index.update_documents([data[0]]) assert isinstance(response, object) assert 'updateId' in response def test_delete_document(self): index = self.client.get_index(uid="movies_uid") response = index.delete_document(299537) assert isinstance(response, object) assert 'updateId' in response def test_delete_documents(self): index = self.client.get_index(uid="movies_uid") response = index.delete_documents([522681, 450465, 329996]) assert isinstance(response, object) assert 'updateId' in response def test_delete_all_documents(self): index = self.client.get_index(uid="movies_uid") response = index.delete_all_documents() assert isinstance(response, object) assert 'updateId' in response def test_delete_index(self): """Tests an API call to delete an index in meiliSearch""" index = self.client.get_index(uid="movies_uid") response = index.delete() assert isinstance(response, object) assert response.status_code == 204
def client(): return meilisearch.Client(common.BASE_URL, common.MASTER_KEY)
def test_get_client_with_wrong_master_key(): """Tests getting a client instance with an invalid MASTER KEY""" client = meilisearch.Client(BASE_URL, MASTER_KEY + "123") with pytest.raises(Exception): client.get_version()
def __init__(self, host_url, api_key, index_uid, custom_settings): self.meilisearch_client = meilisearch.Client(host_url, api_key) self.meilisearch_index = self.__delete_and_create_index(index_uid) self.add_settings(MeiliSearchHelper.SETTINGS, custom_settings)
def test_meilisearch_api_error_no_master_key(): client = meilisearch.Client(BASE_URL) with pytest.raises(MeiliSearchApiError): client.create_index("some_index")
class TestIndex: """ TESTS: all index routes """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index_uid = 'indexUID' index_uid2 = 'indexUID2' index_uid3 = 'indexUID3' index_uid4 = 'indexUID4' def setup_class(self): clear_all_indexes(self.client) def test_create_index(self): """Tests creating an index""" index = self.client.create_index(uid=self.index_uid) assert isinstance(index, object) assert index.uid == self.index_uid assert index.get_primary_key() is None def test_create_index_with_primary_key(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'}) assert isinstance(index, object) assert index.uid == self.index_uid2 assert index.get_primary_key() == 'book_id' def test_create_index_with_uid_in_options(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid3, options={'uid': 'wrong', 'primaryKey': 'book_id'}) assert isinstance(index, object) assert index.uid == self.index_uid3 assert index.get_primary_key() == 'book_id' def test_get_indexes(self): """Tests getting all indexes""" response = self.client.get_indexes() uids = [index['uid'] for index in response] assert isinstance(response, list) assert self.index_uid in uids assert self.index_uid2 in uids assert self.index_uid3 in uids assert len(response) == 3 def test_get_index_with_uid(self): """Tests getting one index with uid""" response = self.client.get_index(uid=self.index_uid) assert isinstance(response, object) assert response.uid == self.index_uid def test_get_index_with_none_uid(self): """Test raising an exception if the index UID is None""" with pytest.raises(Exception): self.client.get_index(uid=None) def test_get_or_create_index(self): """Test get_or_create_index method""" # self.client.create_index(self.index_uid3) index_1 = self.client.get_or_create_index(self.index_uid4) index_2 = self.client.get_or_create_index(self.index_uid4) index_3 = self.client.get_or_create_index(self.index_uid4) assert index_1.uid == index_2.uid == index_3.uid == self.index_uid4 update = index_1.add_documents([{ 'book_id': 1, 'name': "Some book" }]) index_1.wait_for_pending_update(update['updateId']) documents = index_2.get_documents() assert len(documents) == 1 index_2.delete() with pytest.raises(Exception): self.client.get_index(index_3).info() def test_get_or_create_index_with_primary_key(self): """Test get_or_create_index method with primary key""" index_1 = self.client.get_or_create_index('books', {'primaryKey': self.index_uid4}) index_2 = self.client.get_or_create_index('books', {'primaryKey': 'some_wrong_key'}) assert index_1.get_primary_key() == self.index_uid4 assert index_2.get_primary_key() == self.index_uid4 index_1.delete() def test_index_info(self): """Tests getting an index's info""" index = self.client.get_index(uid=self.index_uid) response = index.info() assert isinstance(response, object) assert response['uid'] == self.index_uid assert response['primaryKey'] is None def test_index_info_with_wrong_uid(self): """Tests getting an index's info in MeiliSearch with a wrong UID""" with pytest.raises(Exception): self.client.get_index(uid='wrongUID').info() def test_get_primary_key(self): """Tests getting the primary-key of an index""" index = self.client.get_index(uid=self.index_uid) response = index.get_primary_key() assert response is None def test_update_index(self): """Tests updating an index""" index = self.client.get_index(uid=self.index_uid) response = index.update(primaryKey='objectID') assert isinstance(response, object) assert index.get_primary_key() == 'objectID' def test_delete_index(self): """Tests deleting an index""" index = self.client.get_index(uid=self.index_uid) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid).info() index = self.client.get_index(uid=self.index_uid2) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid2).info() index = self.client.get_index(uid=self.index_uid3) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid3).info() assert len(self.client.get_indexes()) == 0
class TestUpdate: """ TESTS: wait_for_pending_update method """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index = None dataset_file = None dataset_json = None def setup_class(self): clear_all_indexes(self.client) self.index = self.client.create_index(uid='indexUID') self.dataset_file = open('./datasets/small_movies.json', 'r') self.dataset_json = json.loads(self.dataset_file.read()) self.dataset_file.close() def teardown_class(self): self.index.delete() def test_wait_for_pending_update_default(self): """Tests waiting for an update with default parameters""" response = self.index.add_documents([{'id': 1, 'title': 'Le Petit Prince'}]) assert 'updateId' in response update = self.index.wait_for_pending_update(response['updateId']) assert isinstance(update, object) assert 'status' in update assert update['status'] != 'enqueued' def test_wait_for_pending_update_timeout(self): """Tests timeout risen by waiting for an update""" with pytest.raises(TimeoutError): self.index.wait_for_pending_update(2, timeout_in_ms=0) def test_wait_for_pending_update_interval_custom(self): """Tests call to wait for an update with custom interval""" response = self.index.add_documents(self.dataset_json) assert 'updateId' in response start_time = datetime.now() wait_update = self.index.wait_for_pending_update( response['updateId'], interval_in_ms=1000, timeout_in_ms=6000 ) time_delta = datetime.now() - start_time assert isinstance(wait_update, object) assert 'status' in wait_update assert wait_update['status'] != 'enqueued' assert time_delta.seconds >= 1 def test_wait_for_pending_update_interval_zero(self): """Tests call to wait for an update with custom interval""" response = self.index.add_documents(self.dataset_json) assert 'updateId' in response wait_update = self.index.wait_for_pending_update( response['updateId'], interval_in_ms=0, timeout_in_ms=6000 ) assert isinstance(wait_update, object) assert 'status' in wait_update assert wait_update['status'] != 'enqueued'
import requests import json import meilisearch from bs4 import BeautifulSoup from services.earthdata.earthdata import img_url_prefix, earthdata_endpoint # MeiliSearch Config meilisearch_client = meilisearch.Client("https://localhost:7700", "") index = meilisearch_client.get_or_create_index("earthdata", options={"primaryKey": "id"}) indexing_batch_size = 100 def build_document(result, collection_id, has_granules=None): # Count granules if present granules = 0 try: if has_granules: response = requests.get( earthdata_endpoint + "/search/granules?collection_concept_id=" + collection_id, ) if (response.status_code == 200): from xml.dom import minidom xmldoc = minidom.parseString(response.text) items = xmldoc.getElementsByTagName('hits')[0] granules = int(items.childNodes[0].data) except Exception: print("Couldn't get granules")
def __init__(self, host='http://127.0.0.1:7700', masterkey=None): self.client = meilisearch.Client(host, masterkey)
from flask.signals import message_flashed from flask_restful import Resource from flask import jsonify from flask import abort import meilisearch import json client = meilisearch.Client('http://127.0.0.1:8080') class BookResource(Resource): def get(self, request): res = client.index('books').search(request)['hits'] if res: return jsonify({'books': res}) abort(404, message=f'Book {request} not found') class BookListResource(Resource): def get(self): with open('./db/books.json') as f: data = json.load(f) return jsonify(data) class BookIdResource(Resource): def get(self, book_id): with open('./db/books.json') as f: data = json.load(f) for book in data:
def get_client(): return meilisearch.Client(url, apiKey=master_key)
def get_meili_secure_client(started_cluster): connection_str = "http://localhost:{}".format(started_cluster.meili_secure_port) return meilisearch.Client(connection_str, "password")
def handle(self, *args, **options): if not settings.MEILISEARCH_URL: print("Moteur meilisearch non configuré") return client = meilisearch.Client(settings.MEILISEARCH_URL, settings.MEILISEARCH_KEY) # orgues try: index = client.get_index(uid='orgues') except: index = client.create_index(uid='orgues') index.update_searchable_attributes([ 'commune', 'edifice', 'facteurs', 'departement', 'region', 'ancienne_commune', 'placeholder', 'latitude', 'longitude' ]) index.update_filterable_attributes([ 'departement', 'region', 'resume_composition_clavier', 'facet_facteurs', 'jeux', ]) index.update_displayed_attributes([ 'id', 'designation', 'edifice', 'commune', 'ancienne_commune', 'departement', 'region', 'completion', 'vignette', 'emplacement', 'resume_composition', 'facteurs', 'facet_facteurs', 'url', 'latitude', 'longitude', 'construction', ]) index.update_ranking_rules([ 'typo', 'words', 'proximity', 'attribute', 'sort', 'completion:desc', 'exactness', ]) index.update_sortable_attributes([ 'commune', 'completion', 'jeux_count', 'construction', ]) orgues = OrgueResumeSerializer(Orgue.objects.all(), many=True).data index.delete_all_documents() if orgues: index.add_documents(orgues) # types de jeux try: index = client.get_index(uid='types_jeux') except: index = client.create_index(uid='types_jeux') index.update_searchable_attributes(['nom']) index.delete_all_documents() typesJeu = TypeJeu.objects.all() if typesJeu: index.add_documents([{ 'id': t.id, 'nom': str(t) } for t in typesJeu])
def test_get_client_without_apikey(self): """Tests a call to get a client instance of meilisearch""" client = meilisearch.Client("http://127.0.0.1:7700") assert client.config
def search_client(db_path): if not is_search_server_up(): start_search_server(db_path) while not is_search_server_up(): sleep(0.01) return meilisearch.Client(f'http://{SEARCH_IP}:{SEARCH_PORT}')
def test_get_client_without_master_key(): """Tests a call to get a client instance of MeiliSearch""" client = meilisearch.Client(BASE_URL) with pytest.raises(Exception): client.get_indexes()
def update_type_jeu_in_index(sender, instance, **kwargs): if hasattr(settings, 'MEILISEARCH_URL'): client = meilisearch.Client(settings.MEILISEARCH_URL, settings.MEILISEARCH_KEY) index = client.get_index(uid='types_jeux') index.add_documents([{"id": instance.id, "nom": str(instance)}])
def test_get_client(): """Tests a call to get a client instance of MeiliSearch""" client = meilisearch.Client(BASE_URL, MASTER_KEY) assert client.config response = client.get_indexes() assert isinstance(response, list)
def test_meilisearch_communication_error_host(): client = meilisearch.Client("http://wrongurl:1234", MASTER_KEY) with pytest.raises(MeiliSearchCommunicationError): client.create_index("some_index")
class TestDocument: """ TESTS: all documents routes and optional params """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index = None dataset_file = None dataset_json = None def setup_class(self): clear_all_indexes(self.client) self.index = self.client.create_index(uid='indexUID') self.dataset_file = open('./datasets/small_movies.json', 'r') self.dataset_json = json.loads(self.dataset_file.read()) self.dataset_file.close() def teardown_class(self): self.index.delete() def test_get_documents_default(self): """Tests getting documents on a clean index""" response = self.index.get_documents() assert isinstance(response, list) assert response == [] def test_add_documents(self): """Tests adding new documents to a clean index""" response = self.index.add_documents(self.dataset_json) assert isinstance(response, object) assert 'updateId' in response assert self.index.get_primary_key() == 'id' update = self.index.wait_for_pending_update(response['updateId']) assert update['status'] == 'processed' def test_get_document(self): """Tests getting one document on a populated index""" response = self.index.get_document('500682') assert isinstance(response, object) assert 'title' in response assert response['title'] == 'The Highwaymen' def test_get_document_inexistent(self): """Tests getting one INEXISTENT document on a populated index""" with pytest.raises(Exception): self.index.get_document('123') def test_get_documents_populated(self): """Tests getting documents on a populated index""" response = self.index.get_documents() assert isinstance(response, list) assert len(response) == 20 def test_get_documents_offset_optional_params(self): """Tests getting documents on a populated index with optional parameters""" response = self.index.get_documents() assert isinstance(response, list) assert len(response) == 20 response_offset_limit = self.index.get_documents({ 'limit': 3, 'offset': 1, 'attributesToRetrieve': 'title' }) assert len(response_offset_limit) == 3 assert response_offset_limit[0]['title'] == response[1]['title'] def test_update_documents(self): """Tests updating a single document and a set of documents """ response = self.index.get_documents() response[0]['title'] = 'Some title' update = self.index.update_documents([response[0]]) assert isinstance(update, object) assert 'updateId' in update self.index.wait_for_pending_update(update['updateId']) response = self.index.get_documents() assert response[0]['title'] == 'Some title' update = self.index.update_documents(self.dataset_json) self.index.wait_for_pending_update(update['updateId']) response = self.index.get_documents() assert response[0]['title'] != 'Some title' def test_delete_document(self): """Tests deleting a single document""" response = self.index.delete_document('500682') assert isinstance(response, object) assert 'updateId' in response self.index.wait_for_pending_update(response['updateId']) with pytest.raises(Exception): self.index.get_document('500682') def test_delete_documents(self): """Tests deleting a set of documents """ to_delete = ['522681', '450465', '329996'] response = self.index.delete_documents(to_delete) assert isinstance(response, object) assert 'updateId' in response self.index.wait_for_pending_update(response['updateId']) for document in to_delete: with pytest.raises(Exception): self.index.get_document(document) def test_delete_all_documents(self): """Tests updating all the documents in the index""" response = self.index.delete_all_documents() assert isinstance(response, object) assert 'updateId' in response self.index.wait_for_pending_update(response['updateId']) response = self.index.get_documents() assert isinstance(response, list) assert response == []
class TestSearch: """ TESTS: search route """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index = None dataset_file = None dataset_json = None def setup_class(self): clear_all_indexes(self.client) self.index = self.client.create_index(uid='indexUID') self.dataset_file = open('./datasets/small_movies.json', 'r') self.dataset_json = json.loads(self.dataset_file.read()) self.dataset_file.close() response = self.index.add_documents(self.dataset_json, primary_key='id') self.index.wait_for_pending_update(response['updateId']) def teardown_class(self): self.index.delete() def test_basic_search_empty_query(self): """Tests search with an empty query""" response = self.index.search('') assert len(response['hits']) == 0 def test_basic_search(self): """Tests search with an simple query""" response = self.index.search('How to Train Your Dragon') assert isinstance(response, object) assert response['hits'][0]['id'] == '166428' def test_basic_search_with_empty_params(self): """Tests search with an simple query and empty params""" response = self.index.search('How to Train Your Dragon', {}) assert isinstance(response, object) assert response['hits'][0]['id'] == '166428' assert '_formatted' not in response['hits'][0] def test_custom_search(self): """Tests search with an simple query and custom parameter (attributesToHighlight)""" response = self.index.search('Dragon', {'attributesToHighlight': 'title'}) assert isinstance(response, object) assert response['hits'][0]['id'] == '166428' assert '_formatted' in response['hits'][0] assert 'dragon' in response['hits'][0]['_formatted']['title'].lower() def test_custom_search_params_with_wildcard(self): """Tests search with '*' in query params""" response = self.index.search( 'a', { 'limit': 5, 'attributesToHighlight': '*', 'attributesToRetrieve': '*', 'attributesToCrop': '*', }) assert isinstance(response, object) assert len(response['hits']) == 5 assert '_formatted' in response['hits'][0] assert "title" in response['hits'][0]['_formatted'] def test_custom_search_params_with_simple_string(self): """Tests search with simple string in query params""" response = self.index.search( 'a', { 'limit': 5, 'attributesToHighlight': 'title', 'attributesToRetrieve': 'title', 'attributesToCrop': 'title', }) assert isinstance(response, object) assert len(response['hits']) == 5 assert '_formatted' in response['hits'][0] assert 'title' in response['hits'][0]['_formatted'] assert not 'release_date' in response['hits'][0]['_formatted'] def test_custom_search_params_with_string_list(self): """Tests search with string list in query params""" response = self.index.search( 'a', { 'limit': 5, 'attributesToRetrieve': ['title', 'overview'], 'attributesToHighlight': ['title'], }) assert isinstance(response, object) assert len(response['hits']) == 5 assert 'title' in response['hits'][0] assert 'overview' in response['hits'][0] assert not 'release_date' in response['hits'][0] assert 'title' in response['hits'][0]['_formatted'] assert not 'overview' in response['hits'][0]['_formatted'] def test_custom_search_params_with_facets_distribution(self): update = self.index.update_attributes_for_faceting(['genre']) self.index.wait_for_pending_update(update['updateId']) response = self.index.search('world', {'facetsDistribution': ['genre']}) assert isinstance(response, object) assert len(response['hits']) == 12 assert 'facetsDistribution' in response assert 'exhaustiveFacetsCount' in response assert response['exhaustiveFacetsCount'] assert 'genre' in response['facetsDistribution'] assert response['facetsDistribution']['genre']['cartoon'] == 1 assert response['facetsDistribution']['genre']['action'] == 3 assert response['facetsDistribution']['genre']['fantasy'] == 1 def test_custom_search_params_with_facet_filters(self): update = self.index.update_attributes_for_faceting(['genre']) self.index.wait_for_pending_update(update['updateId']) response = self.index.search('world', {'facetFilters': [['genre:action']]}) assert isinstance(response, object) assert len(response['hits']) == 3 assert 'facetsDistribution' not in response assert 'exhaustiveFacetsCount' not in response def test_custom_search_params_with_multiple_facet_filters(self): update = self.index.update_attributes_for_faceting(['genre']) self.index.wait_for_pending_update(update['updateId']) response = self.index.search('world', { 'facetFilters': ['genre:action', ['genre:action', 'genre:action']] }) assert isinstance(response, object) assert len(response['hits']) == 3 assert 'facetsDistribution' not in response assert 'exhaustiveFacetsCount' not in response def test_custom_search_params_with_many_params(self): update = self.index.update_attributes_for_faceting(['genre']) self.index.wait_for_pending_update(update['updateId']) response = self.index.search( 'world', { 'facetFilters': [['genre:action']], 'attributesToRetrieve': ['title', 'poster'] }) assert isinstance(response, object) assert len(response['hits']) == 3 assert 'facetsDistribution' not in response assert 'exhaustiveFacetsCount' not in response assert 'title' in response['hits'][0] assert 'poster' in response['hits'][0] assert 'overview' not in response['hits'][0] assert 'release_date' not in response['hits'][0] assert response['hits'][0]['title'] == 'Avengers: Infinity War'
class TestIndex: """ TESTS: all index routes """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index_uid = 'indexUID' index_uid2 = 'indexUID2' index_uid3 = 'indexUID3' def setup_class(self): clear_all_indexes(self.client) def test_create_index(self): """Tests creating an index""" index = self.client.create_index(uid=self.index_uid) assert isinstance(index, object) assert index.uid == self.index_uid assert index.get_primary_key() is None def test_create_index_with_primary_key(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'}) assert isinstance(index, object) assert index.uid == self.index_uid2 assert index.get_primary_key() == 'book_id' def test_create_index_with_uid_in_options(self): """Tests creating an index with a primary key""" index = self.client.create_index(uid=self.index_uid3, options={ 'uid': 'wrong', 'primaryKey': 'book_id' }) assert isinstance(index, object) assert index.uid == self.index_uid3 assert index.get_primary_key() == 'book_id' def test_get_indexes(self): """Tests getting all indexes""" response = self.client.get_indexes() uids = [index['uid'] for index in response] assert isinstance(response, list) assert self.index_uid in uids assert self.index_uid2 in uids assert self.index_uid3 in uids assert len(response) == 3 def test_get_index_with_uid(self): """Tests getting one index with uid""" response = self.client.get_index(uid=self.index_uid) assert isinstance(response, object) assert response.uid == self.index_uid def test_get_index_with_none_uid(self): """Test raising an exception if the index UID is None""" with pytest.raises(Exception): self.client.get_index(uid=None) def test_index_info(self): """Tests getting an index's info""" index = self.client.get_index(uid=self.index_uid) response = index.info() assert isinstance(response, object) assert response['uid'] == self.index_uid assert response['primaryKey'] is None def test_index_info_with_wrong_uid(self): """Tests getting an index's info in MeiliSearch with a wrong UID""" with pytest.raises(Exception): self.client.get_index(uid='wrongUID').info() def test_get_primary_key(self): """Tests getting the primary-key of an index""" index = self.client.get_index(uid=self.index_uid) response = index.get_primary_key() assert response is None def test_update_index(self): """Tests updating an index""" index = self.client.get_index(uid=self.index_uid) response = index.update(primaryKey='objectID') assert isinstance(response, object) assert index.get_primary_key() == 'objectID' def test_delete_index(self): """Tests deleting an index""" index = self.client.get_index(uid=self.index_uid) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid).info() index = self.client.get_index(uid=self.index_uid2) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid2).info() index = self.client.get_index(uid=self.index_uid3) response = index.delete() assert isinstance(response, object) assert response.status_code == 204 with pytest.raises(Exception): self.client.get_index(uid=self.index_uid3).info() assert len(self.client.get_indexes()) == 0
def test_meilisearch_api_error_wrong_master_key(): client = meilisearch.Client(BASE_URL, MASTER_KEY + '123') with pytest.raises(MeiliSearchApiError): client.create_index("some_index")
from sqlalchemy.orm import Session import meilisearch from app.core.config import settings client = meilisearch.Client(settings.MEILI_API_URL, apiKey=settings.MEILI_MASTER_KEY, timeout=3600) def init_taxa(db: Session) -> None: index = client.index('taxa') rows = db.execute("SELECT json_agg(t) FROM taxa as t") index.add_documents(rows.first()[0]) def init_indices(db: Session) -> None: init_taxa(db)
import os import time import json import subprocess import meilisearch import xxhash from pathlib import Path from datetime import datetime DELAY = int(os.getenv('DELAY')) input_path = Path('/inputs') client = meilisearch.Client('http://search:7700') index = client.get_or_create_index('screenshots') while True: print(f'\n\n****** Checking for changes: {datetime.now()} ******') inputs = set(i.name for i in input_path.iterdir() if not i.is_dir()) n_docs = index.get_stats()['numberOfDocuments'] outputs = set(x['name'] for x in index.get_documents({ 'limit': n_docs, 'attributesToRetrieve': 'name' })) deleted_files = outputs - inputs print(f'No of docs to be deleted from db = {len(deleted_files)}'.upper()) index.delete_documents( [xxhash.xxh3_64_hexdigest(x) for x in deleted_files]) added_files = inputs - outputs print(f'No of docs to be added to db = {len(added_files)}'.upper())
class TestSetting: """ TESTS: all settings route """ client = meilisearch.Client(BASE_URL, MASTER_KEY) index = None new_settings = { 'rankingRules': ['typo', 'words'], 'searchableAttributes': ['title', 'overview'] } default_ranking_rules = [ 'typo', 'words', 'proximity', 'attribute', 'wordsPosition', 'exactness' ] def setup_class(self): self.index = self.client.create_index(uid='indexUID') def teardown_class(self): self.index.delete() def test_get_settings_default(self): """Tests getting all settings by default""" response = self.index.get_settings() assert isinstance(response, object) for rule in self.default_ranking_rules: assert rule in response['rankingRules'] assert response['distinctAttribute'] is None assert response['searchableAttributes'] == [] assert response['displayedAttributes'] == [] assert response['stopWords'] == [] assert response['synonyms'] == {} assert response['acceptNewFields'] is True def test_update_settings(self): """Tests updating some settings""" response = self.index.update_settings(self.new_settings) assert isinstance(response, object) assert 'updateId' in response update = self.index.wait_for_pending_update(response['updateId']) assert update['status'] == 'processed' response = self.index.get_settings() for rule in self.new_settings['rankingRules']: assert rule in response['rankingRules'] assert response['distinctAttribute'] is None for attribute in self.new_settings['searchableAttributes']: assert attribute in response['searchableAttributes'] assert response['displayedAttributes'] == [] assert response['stopWords'] == [] assert response['synonyms'] == {} assert response['acceptNewFields'] is True def test_reset_settings(self): """Tests resetting default settings""" response = self.index.reset_settings() assert isinstance(response, object) assert 'updateId' in response update = self.index.wait_for_pending_update(response['updateId']) assert update['status'] == 'processed' response = self.index.get_settings() for rule in self.default_ranking_rules: assert rule in response['rankingRules'] assert response['distinctAttribute'] is None for attribute in self.new_settings['searchableAttributes']: assert attribute in response['searchableAttributes'] assert attribute in response['displayedAttributes'] assert response['stopWords'] == [] assert response['synonyms'] == {} assert response['acceptNewFields'] is True
def test_get_client_without_master_key(): """Tests getting a client instance without MASTER KEY""" client = meilisearch.Client(BASE_URL) with pytest.raises(Exception): client.get_version()
import click from collections import defaultdict import meilisearch import ujson as json import logging from pathlib import Path from tinydb import TinyDB, Query from tinydb.storages import MemoryStorage from api.models import Identifiable from api.database import db_session logger = logging.getLogger(__name__) logger.addHandler(logging.StreamHandler()) client = meilisearch.Client("http://127.0.0.1:7700") KB_INDEX_UID = "knowledge_bases" KB_SCHEMA = { "uid": ["displayed", "identifier"], "name": ["displayed", "indexed"], "shortname": ["displayed", "indexed"], "tags": ["displayed", "indexed"], "website": ["displayed"], "description": ["displayed", "indexed"], "sources": ["displayed"], } INDEX_INDEX_UID = "index" INDEX_SCHEMA = { "uid": ["displayed"],
def test_get_client(): """Tests getting a client instance""" client = meilisearch.Client(BASE_URL, MASTER_KEY) assert client.config response = client.health() assert response.status_code >= 200 and response.status_code < 400
def connect(self): self.client = meilisearch.Client( f"{self.protocol}://{self.host}:{self.port}", self.api_key) self.index = self.client.index(self.index_name)