Query Utility for Elasticsearch
Project description
Query Utility for Elasticsearch
Utility library for creating elasticsearch query proxies using FastAPI.
from fastapi_elasticsearch import ElasticsearchAPIQueryBuilder
query_builder = ElasticsearchAPIQueryBuilder()
# Decorate a function as a filter.
# The filter can declare parameters.
@query_builder.filter()
def filter_category(c: Optional[str] = Query(None)):
return {
"term": {
"category": c
}
} if c is not None else None
# Decorate a function as a matcher
# (will contribute to the query scoring).
# Parameters can also be used.
@query_builder.matcher()
def match_fields(q: Optional[str] = Query(None)):
return {
"multi_match": {
"query": q,
"fuzziness": "AUTO",
"fields": [
"name^2",
"description"
]
}
} if q is not None else None
# Decorate a function as a sorter.
# Parameters can be declared.
@query_builder.sorter()
def sort_by(direction: Optional[str] = Query(None)):
return {
"name": direction
} if direction is not None else None
# Decorate a function as a highlighter.
# Parameters can also be declared.
@query_builder.highlighter()
def highlight(q: Optional[str] = Query(None),
h: bool = Query(False):
return {
"name": {}
} if q is not None and h else None
app = FastAPI()
# Create a route using the query builder as dependency.
@app.get("/search")
async def search(query_body: Dict = Depends(query_builder.build())) -> JSONResponse:
# Search using the Elasticsearch client.
return es.search(
body=query_body,
index=index_name
)
It is possible to customize the generated query body using the decorator @search_builder.
from typing import List, Dict
@query_builder.search_builder()
def build_search_body(size: int = 10,
start_from: int = 0,
source: Union[List, Dict, str] = None,
minimum_should_match: int = 1,
filters: List[Dict] = [],
matchers: List[Dict] = [],
highlighters: List[Dict] = [],
sorters: List[Dict] = []) -> Dict:
return {
"query": {
...
},
...
}
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
Close
Hashes for fastapi-elasticsearch-0.5.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 553085b0300a1fb50c7eab3c3478c4b5ffe475cc6e5b8e0d6ae47476acb9b9e4 |
|
MD5 | 9c16819d50036d4cda499dd807656a19 |
|
BLAKE2b-256 | 006be77667827b538c1625ffe715e47712b271e0f6e4d54b31af679d4c80e162 |
Close
Hashes for fastapi_elasticsearch-0.5.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa78bcdcb4c6984992bcd77618c5f15f4ed6b6a0a10f6b5bfcaa43e533e1ed18 |
|
MD5 | a3a8206b3521307ed19415b105ac8ce0 |
|
BLAKE2b-256 | 65d056c4ad4d2a638a9e613978b9ef57a5c777f2d5ffcd2bf94e1ad569a0dfe4 |