greyhoundforty
9/30/2016 - 8:37 PM

Search Elasticsearch data

Search Elasticsearch data

#!/usr/bin/env python

import json

import click
import elasticsearch


@click.command()
@click.argument('query', required=True)
def search(query):
    es = elasticsearch.Elasticsearch([{'host': 'localhost', 'port': 9200}])
    matches = es.search('products', q=query)
    hits = matches['hits']['hits']
    if not hits:
        click.echo('No matches found')
    else:
        for hit in hits:
            # This next line and the two after it are the only changes
            click.echo((
                hit['_source']['name'],
		hit['_source']['tags'],
            ))

if __name__ == '__main__':
    search()