Python - Sending a Wildcard Query to ElasticSearch using Requests

07 Jun 2017

Here’s an easy wrapper for ElasticSearch using request and json.

import requests
import json

def es_wildcard_query(uri, key, term):
    """Wildcard Elasticsearch Query"""
    query = json.dumps({
        "query": {
            "wildcard": {
                key: '*%s*' % term
            }
        }
    })
    response = requests.get(uri, data=query)
    results = json.loads(response.text)
    return results

# Sample Usage:
# print es_wildcard_query('http://your_es_url/_search?', 'key', 'search_term')