Skip to content

Commit

Permalink
Merge pull request #3 from patdaburu/6.5.2
Browse files Browse the repository at this point in the history
6.5.2
  • Loading branch information
patdaburu committed Jul 27, 2019
2 parents eccef99 + 5ca475b commit caf4c35
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ See also the list of
`contributors <https://github.com/patdaburu/elastalk/contributors>`__
who participated in this project.

LicenseMIT License
------------------
MIT License
-----------

Copyright (c) patdaburu

Expand Down
4 changes: 4 additions & 0 deletions elastalk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
Simple Conveniences for Talking to Elasticsearch
"""
from .version import __version__, __release__
from .config import ElastalkConf, ElastalkConfigException
from .connect import ElastalkMixin
from .seed import seed
from .search import extract_hit, extract_hits
3 changes: 2 additions & 1 deletion elastalk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from pathlib import Path
import uuid
from typing import Iterable, Dict, Set
import toml
from dataclasses import dataclass, field
import toml


#: the module logger
__logger__: logging.Logger = logging.getLogger(__name__)
Expand Down
59 changes: 59 additions & 0 deletions elastalk/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created on 7/24/19 by Pat Daburu
"""
.. currentmodule:: elastalk.search
.. moduleauthor:: Pat Daburu <pat@daburu.net>
This module contains functions you can use when dealing with
`Elasticsearch documents <https://bit.ly/2YcMds5>`_ returned by searches.
"""
from typing import Any, Iterable, Mapping, Tuple
import uuid

ID_FIELD = '_id' #: the standard name of the ID field


def extract_hit(
hit: Mapping[str, Any],
includes: Tuple[str] = (ID_FIELD,),
source: str = '_source'
) -> Mapping[str, Any]:
"""
Extract a document from a single search result hit.
:param hit: the search hit document
:param includes: the metadata keys to include in the return document
:param source: the key that contains the source document
:return:
"""
doc = {
**{
k: hit.get(k) for k in includes
},
**hit.get(source)
}
# If the document ID is included...
if ID_FIELD in doc:
# ...convert it to a UUID.
doc[ID_FIELD] = uuid.UUID(doc.get(ID_FIELD))
return doc


def extract_hits(
result: Mapping[str, Any],
includes: Tuple[str] = (ID_FIELD,),
source: str = '_source'
) -> Iterable[Mapping[str, Any]]:
"""
Extract documents from a search result.
:param result: the search result document
:param includes: the metadata keys to include in the return document
:param source: the key that contains the source document
:return: an iteration of search result documents
"""
hits = result.get('hits', {}).get('hits', [])
for hit in hits:
yield extract_hit(hit, includes=includes, source=source)
4 changes: 2 additions & 2 deletions elastalk/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
This module contains project version information.
"""

__version__ = '6.5.2.1' #: the working version
__release__ = '6.5.2.1' #: the release version
__version__ = '6.5.2.2' #: the working version
__release__ = '6.5.2.2' #: the release version
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
[console_scripts]
elastalk=elastalk.cli:cli
""",
python_requires=">=0.0.1",
python_requires=">=3.6",
license='MIT',
author='Pat Daburu',
author_email='pat@daburu.net',
Expand Down

0 comments on commit caf4c35

Please sign in to comment.