A tool for forming a single query body for further search in Elasticsearch
Project description
Elasticsearch Query Builder
A tool for forming a single query body for further search in Elasticsearch
Features
- A single entry point for receiving a ready-made request
- Support for the most frequently used fields
- Basic normalization of input data with the ability to specify the type
- No dependencies
Installation
Requirements:
- Installed python
3.8
or higher
pip install elasticsearch-query-builder
Usage
To use Elasticsearch query builder, you need to describe the inheritor class from the base builder class, and then describe the expected fields as class attributes
from elasticsearch_query_builder import ElasticsearchQueryBuilder, fields
class BookQueryBuilder(ElasticsearchQueryBuilder):
search = fields.MultiMatchElasticField(
query_type="best_fields",
fields=[
"field_1",
"field_2",
"field_3",
"obj.field_4",
]
)
with_reviews = fields.TermElasticField(
input_type=bool,
field_name="has_reviews"
)
author = fields.NestedElasticField(
path="authors",
child=fields.MatchElasticField(
input_type=str,
field_name="name"
)
)
The next step is to call the described class, passing it an object with request parameters in the constructor
...
builder = BookQueryBuilder({"search": "World", "with_reviews": "true", "author": "John Doe"})
query = builder.query
print(query)
As a result of execution, a query object will be constructed, which can be sent to Elasticsearch for search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "World",
"type": "best_fields",
"fields": [
"field_1",
"field_2",
"field_3",
"obj.field_4"
]
}
},
{
"term": {
"has_reviews": true
}
},
{
"nested": {
"path": "authors",
"query": {
"match": {
"authors.name": {
"query": "John Doe"
}
}
}
}
}
]
}
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file elasticsearch_query_builder-0.1.tar.gz
.
File metadata
- Download URL: elasticsearch_query_builder-0.1.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ed8ee472d19923dc984e2d3405a3947cf88525c16ddc50849f7084c2588a19e |
|
MD5 | 02159d7fb963d31ed2ec6a8fb84a1beb |
|
BLAKE2b-256 | 3402b29f7aa5dc32d6addf966e29fcf55fe87709d10fe835d00322e52f6e2477 |
File details
Details for the file elasticsearch_query_builder-0.1-py3-none-any.whl
.
File metadata
- Download URL: elasticsearch_query_builder-0.1-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03e721d4e114b1e6984fe5b123e48574d6252719ebfabe0ca08232d50ae3423d |
|
MD5 | eb95f16bea1621ecdaba9c89b0c94f9b |
|
BLAKE2b-256 | 89f03324ea422c3a29bc98cf3217fa601a2143dd25b561398a020f55c1b9c005 |