Query Utility for Elasticsearch
Project description
Query Utility for Elasticsearch
Utility library for creating elasticsearch query proxies using FastAPI.
from fastapi_elasticsearch import ElasticsearchAPI
es_api = ElasticsearchAPI(
# The elasticsearech client
es_client=es,
# The index or indices that your query will run.
index_name=index_name)
# Decorate a function as a filter.
# The filter can declare parameters.
@es_api.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.
@es_api.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.
@es_api.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.
@es_api.highlighter()
def highlight(q: Optional[str] = Query(None),
h: bool = Query(False):
return {
"name": {}
} if q is not None and h else None
# Decorate a function as a search_route.
# It creates a new route using the declared filters (and matchers, etc.)
# as the endpoint parameters but combined with the route's parameters.
@es_api.search_route(app, "/search")
async def search(req: Request,
size: Optional[int] = Query(10,
le=100,
alias="s"),
start_from: Optional[int] = Query(0,
alias="f"),
scroll: Optional[str] = Query(None),
) -> JSONResponse:
return es_api.search(
request=req,
size=size,
start_from=start_from,
scroll=scroll,
)
It is possible to customize the generated query body using the decorator @search_builder.
from typing import List, Dict
@es_api.search_builder()
def build_search_body(size: int = 10,
start_from: int = 0,
scroll: str = None,
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.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfa0f204f9da76a6fe74452107188d89042d7d69dd66da0c2b5e410465b8abc1 |
|
MD5 | 71ce5806ad46b0114dcfbed56bb5cd58 |
|
BLAKE2b-256 | 1625bd90a519568564a8431dd4f195a4f289cca20fdecf6c4b9910980c366556 |
Close
Hashes for fastapi_elasticsearch-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c854852a46828d11afe088581571a63b58a57d741b45d1b34f8709b2e8750805 |
|
MD5 | 388523edfa4b28c0686e600b6acc61f0 |
|
BLAKE2b-256 | 4295683adebe2647d05ec9908cdd49ead6be3df4fdf6307eee61630639e90356 |