{"id":13530429,"url":"https://github.com/certego/AtlasQ","last_synced_at":"2025-04-01T18:31:47.721Z","repository":{"id":37093948,"uuid":"496576315","full_name":"certego/AtlasQ","owner":"certego","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-27T14:30:42.000Z","size":195,"stargazers_count":7,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T16:23:09.463Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/certego.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-05-26T10:29:51.000Z","updated_at":"2024-09-27T14:30:11.000Z","dependencies_parsed_at":"2024-04-13T04:40:16.659Z","dependency_job_id":null,"html_url":"https://github.com/certego/AtlasQ","commit_stats":null,"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/certego%2FAtlasQ","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/certego%2FAtlasQ/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/certego%2FAtlasQ/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/certego%2FAtlasQ/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/certego","download_url":"https://codeload.github.com/certego/AtlasQ/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246691589,"owners_count":20818540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T07:00:49.702Z","updated_at":"2025-04-01T18:31:47.389Z","avatar_url":"https://github.com/certego.png","language":"Python","funding_links":[],"categories":["Libraries"],"sub_categories":["Python"],"readme":"# AtlasQ\nAtlasQ allows the usage of [AtlasSearch](https://www.mongodb.com/docs/atlas/atlas-search/) keeping the [MongoEngine](https://github.com/MongoEngine/mongoengine) syntax.\n\n## Structure\nThe package tries to follow the MongoEngine structure;\nthe major differences reside in the `transform.py` and `queryset.py` files. \n\n### Transform\nLike in MongoEngine, a step in the pipeline is the creation of a query from a `Q` object: \nwe have to find a correspondence between the MongoEngine common syntax and what AtlasSearch allows.\nFor doing this, we had to find some compromises.\n\nNot every keyword is supported at the moment: if you have an actual use case that you would like to support,\nplease be free to open an issue or a PR at any moment.\n\n### QuerySet\nThere are probably a thousand of better implementation, if you actually knew MongoEngine and above all [PyMongo](https://pymongo.readthedocs.io/en/stable/).\nUnfortunately, our knowledge is limited, so here we go. If you find a solution that works better, again, feel free to open an issue or a PR.\n\nThe main idea, is that the `filter` should work like an `aggregation`. \nFor doing so, and with keeping the compatibility on how MongoEngine works (i.e. the filter should return a queryset of `Document`) we had to do some work.  \nCalling `.aggregate` instead has to work as MongoEngine expect, meaning a list of dictionaries. \n\n\n\n## Usage\nNow the most important part: how do you use this package?\n\n\n```python3\nfrom mongoengine import Document, fields\n\nfrom atlasq import AtlasManager, AtlasQ, AtlasQuerySet\n\nindex_name = str(\"my_index\")\n\nclass MyDocument(Document):\n    name = fields.StringField(required=True)\n    surname = fields.StringField(required=True)\n    atlas = AtlasManager(index_name)\n\nobj = MyDocument.objects.create(name=\"value\", surname=\"value2\")\n\nqs = MyDocument.atlas.filter(name=\"value\")\nassert isinstance(qs, AtlasQuerySet)\nobj_from_atlas = qs.first()\nassert obj == obj_from_atlas\n\nobj2_from_atlas = MyDocument.atlas.get(AtlasQ(name=\"value\") \u0026 AtlasQ(surname=\"value2\"))\nassert obj == obj2_from_atlas\n\n\nobj3_from_atlas = MyDocument.atlas.get(AtlasQ(wrong_field=\"value\"))\nassert obj3_from_atlas is None\n```\n\n##  Extended Features\n\n### Validation\nWe also decided to have, optionally, a validation of the index.\nTwo things are checked:\n- The index actually exists (If you query a non-existing index, Atlas as default behaviour will not raise any error).\n- The fields that you are querying are actually indexed(If you query a field that is not indexed, Atlas as default behaviour will not raise any error, and will return an empty list).\nTo make these check, you need to call the function `ensure_index` on the queryset.\n\n```python3\nfrom mongoengine import Document, fields\n\nfrom atlasq import AtlasManager, AtlasQ\n\nindex_name = str(\"my_index\")\n\nclass MyDocument(Document):\n    name = fields.StringField(required=True)\n    surname = fields.StringField(required=True)\n    atlas = AtlasManager(index_name)\n\nresult = MyDocument.atlas.ensure_index(\"user\", \"pwd\", \"group\", \"cluster\")\nassert result is True\nobj1_from_atlas = MyDocument.atlas.get(AtlasQ(name=\"value\")) \nobj2_from_atlas = MyDocument.atlas.get(AtlasQ(wrong_field=\"value\")) # raises AtlasIndexFieldError\n```\n\n### EmbeddedDocuments\nEmbedded documents are queried in two different ways, depending on how you created your Search Index.\nRemember to ensure the index so that AtlasQ can know how your index is defined\nIf you used the [embeddedDocuments](https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#std-label-bson-data-types-embedded-documents) type, AtlasQ will use the [embeddedDocument](https://www.mongodb.com/docs/atlas/atlas-search/embedded-document/) query syntax.\nOtherwise, if you used the [document](https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#document) type, or you did not ensure the index, a normal `text` search with the `.` syntax will be used.\n\n### Upload index\nIt is possible to upload directly the Search index using AtlasQ, calling the function `upload_index` on the queryset. Syntax checks on the index itself are performed.\nIf the `_id` is not present but `pk` or `id` was specified, it will be automatically added, allowing valid text query on the\nprimary key.\n\n```python3\nfrom mongoengine import Document, fields\n\nfrom atlasq import AtlasManager\n\nindex_name = str(\"my_index\")\nindex = {\n  \"analyzer\": \"lucene.keyword\",\n  \"mappings\": {\n    \"dynamic\": False,\n    \"fields\": {\n      \"_id\": {\n        \"type\": \"objectId\"\n      },\n        \"name\": {\n        \"type\": \"string\"\n      },\n      \"surname\": {\n        \"type\": \"string\"\n      },\n    }\n  }\n}\nclass MyDocument(Document):\n    name = fields.StringField(required=True)\n    surname = fields.StringField(required=True)\n    atlas = AtlasManager(index_name)\n\nresult = MyDocument.atlas.ensure_index(\"user\", \"pwd\", \"group\", \"cluster\")\nassert result is False\nMyDocument.atlas.upload_index(index, \"user\", \"pwd\", \"group\", \"cluster\")\nresult = MyDocument.atlas.ensure_index(\"user\", \"pwd\", \"group\", \"cluster\")\nassert result is True\n\n```\n\n\n### Sort\nOn the [10th of July 2023](https://www.mongodb.com/docs/atlas/atlas-search/changelog/#10-july-2023-release), the `Sort` functionality was released for Atlas search.\n\nAtlasQ, from version 0.12.0, will support this feature inside the `order_by` function.\nTo have the old behaviour of the order_by (useful if you want to sort _after_ aggregations and not after the search stage), you can set the kwarg `as_aggregation` as `True`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcertego%2FAtlasQ","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcertego%2FAtlasQ","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcertego%2FAtlasQ/lists"}