Exemplo n.º 1
0
def search(q, size, start, slop):

    """
    Search documents.
    """

    results = config.es.search(
        "osp",
        "document",
        body={
            "size": size,
            "from": start,
            "fields": [],
            "query": {"match_phrase": {"body": {"query": q, "slop": slop}}},
            "highlight": {"pre_tags": ["\033[1m"], "post_tags": ["\033[0m"], "fields": {"body": {}}},
        },
    )

    term = Terminal()

    # Total hits.
    hits = str(results["hits"]["total"]) + " docs"
    click.echo(term.standout_cyan(hits))

    # Hit highlights.
    for hit in results["hits"]["hits"]:
        click.echo("\n" + term.underline(hit["_id"]))
        for hl in hit["highlight"]["body"]:
            click.echo(hl)
Exemplo n.º 2
0
    def es_query(cls, toponym):

        """
        Search for a toponym.

        Args:
            toponym (str): The placename.
        """

        results = config.es.search(
            cls.es_index,
            cls.es_doc_type,
            body={
                'size': 10,
                'fields': [],
                'query': {
                    'match_phrase': {
                        'body': {
                            'query': toponym,
                            'slop': 3
                        }
                    }
                },
                # TODO|dev
                'highlight': {
                    'pre_tags': ['\033[1m'],
                    'post_tags': ['\033[0m'],
                    'fields': {
                        'body': {}
                    }
                }
            }
        )

        # TODO|dev
        term = Terminal()

        # Total hits.
        hits = str(results['hits']['total'])+' docs'
        print(term.standout_green(hits))

        # Hit highlights.
        for hit in results['hits']['hits']:
            print('\n'+term.standout_cyan(hit['_id']))
            for hl in hit['highlight']['body']:
                print(hl)