Build A Stand-Alone Executable Elasticsearch Application Using Python

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This article will explain how to build a stand-alone Elasticsearch query application using Python.

The original Python code used for the app can be found in this ObjectRocket article explaining how to query Elasticsearch documents in a terminal window.

Prerequisites

1
curl -GET "localhost:9200" -v

Screenshot of a terminal window making a cURL request to the Elasticsearch cluster

Building an executable Python script with PyInstaller

Installing PyInstaller and building a Python application on Windows

It’s recommended that you use or switch to Python 3 instead of Python 2.7 since Python 2 is now deprecated and scheduled to lose support by 2020.

Type cmd in the Windows Search bar, or type run and then type cmd in the modal window to open a new instance of Command Prompt to get started.

Install the PyInstaller package for Python 3 on Windows

Depending on your installations of Python you’ll need to use the pip3 or pip command to install PyInstaller for Python 3. Type python --version into a Command Prompt terminal. You should see a response that tells says Python 3.x.

If the response says you have version 2.7 installed, or if it raises an error, then try the following command instead:

1
python3 --version

If Python 3 used the python alias (without the 3), then use the pip command to install PyInstaller:

1
python -m pip install --user pyinstaller

NOTE: Use the --user option if Windows raises a permissions error.

Otherwise use pip3 to installed the package:

1
pip3 install --user pyinstaller

Here’s the command to upgrade your current version of PyInstaller:

1
python -m pip install --upgrade --user pyinstaller

Screenshot of Windows command prompt installing the PyInstaller library with the PIP3 Python package manager

PyInstaller command-not-found error in Windows

Type python -m PyInstaller, pyinstaller, or PyInstaller into your Command Prompt window and press return. If none of these commands work, then it probably means that the pyinstaller.exe executable file is not set in your system’s environmental variables path.

Windows doesn’t know the path to the PyInstaller executable file

Use the echo %PATH in your command line to have it return a list of all the paths for your environmental variables.

Type environment into the Windows Search bar, or just open the System Properties window in your Control Panel. Make sure to click on the Advanced tab in System Properties, and then click the Environment Variables button:

Screenshot of the System Properties window in Windows and the Environmental Variables button in System Properties

Add the path for the ‘pyinstaller.exe’ to your Windows environment variables

Open a instance of File Explorer and navigate to C:\Program Files\Python37\Scripts or C:\Users\USER_NAME\AppData\Roaming\Python\Python37\Scripts and look for the executable. Once you find the file make sure to get its full path.

Click the New.. button in your Environmental Variables to add the system path for the pyinstaller.exe file. Make sure to type PyInstaller in the Variable name input field, and the path to the .exe file in the Variable value field.

Screenshot of the user variables in the Environmental Variables settings for Windows

Verify that the PyInstaller command for Python 3 is now working

Close all your instances of Command Prompt, and re-open it. Type PyInstaller or python PyInstaller into the window’s prompt, and it should now return a response from the executable.

Build a Windows executable using the Pyinstaller package for Python

You should now be able to build an executable from a Python script using PyInstaller using the following command:

1
python -m PyInstaller script_name.py

NOTE: Make sure to first change (cd) into the directory containing the Python script before running the PyInstaller command.

python -m pip install –upgrade –user pip python -m pip install –upgrade –user pyinstaller

pip -v install –user elasticsearch

PyInstaller C:UsersObjectRocketAppDataRoamingPythonPython37Scriptspyinstaller.exe

Python

echo %PATH%

C:UsersObjectRocketAppDataRoamingPythonPython37Scripts

python -m PyInstaller

pyinstaller –version

Screenshot of a Windows Command Prompt application created using PyInstaller for Python

Installing the PyInstaller Package on Ubuntu distros of Linux

pip3 install pyinstaller

sudo apt-get install libc-bin sudo apt-get install binutils

pyinstaller

pyinstaller: error: the following arguments are required: scriptname

pyinstaller cli.py python3 -O -m PyInstaller elastic_query.py

cd dist/elastic_query/

./elastic_query test

sudo rm -rf build/; sudo rm -rf dist/; sudo rm -rf pycache/; sudo rm elastic_query.spec

Change into the PyInstaller’s ‘dist’ directory

Screenshot of the dist directory created by the Python PyInstaller library

Execute the ‘elastic_query’ file created by PyInstaller with a query

pass a query to its parameters

Elasticsearch cluster response returned from the PyInstaller application’s API request

1
./elastic_query some_index "str field" "ObjectRocket"

Screenshot of a terminal printing the results of an Elasticsearch query request made using a PyInstaller application

Conclusion

We hope you enjoyed this tutorial on how to build a stand-alone Elasticsearch query application with Python. You can grab any or all of the code we used below.

Just the Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# use sys for system arguments passed to script
import sys

# import the Elasticsearch client library
from elasticsearch import Elasticsearch

# create a client instance of Elasticsearch
client = Elasticsearch("http://localhost:9200")

# The script name is the first arg [0], but the second arg [1]
# can be used for the Elasticsearch index's name
try:
    INDEX_NAME = sys.argv[1]
except IndexError as err:
    print ("IndexError - Script needs the Elasticsearch index name:", err)
    sys.exit()

def make_query(filter):
    index_exists = client.indices.exists(index=INDEX_NAME)

    # check if the index exists
    if index_exists == True:
        print ("INDEX_NAME:", INDEX_NAME, "exists.")
        print ("FILTER:", filter, "\n")

        try:

            # pass filter query to the client's search() method
            response = client.search(index=INDEX_NAME, body=filter)

            # print the query response`
            print ('response["hits"]:', len(response["hits"]))
            print ('response TYPE:', type(response))

            # iterate the response hits
            print ("\nQueried %d Hits:" % response['hits']['total']['value'])
            for hit in response['hits']['hits']:
                print(hit["_source"])

        except Exception as err:
            print ("search(index) ERROR", err)
            response = {"error": err}
    # return an empty dict if index doesn't exist
    else:
        response = {}

    return response


def main():

    # declare variable for system arguments list
    sys_args = sys.argv

    # remove Python script name from args list
    sys_args.pop(0)

    # quit the script if there are not exactly 3 arguments
    if len(sys_args) != 3:
        print ("Three arguments needed. You provided:", sys_args)
        print ("First argument is index, and next two are the field and query:", sys_args)
        sys.exit()

    else:
        # get the field name and query value from sys args
        field_name = sys_args[1]
        value = sys_args[2]

        # pass the field name and value args to filter dict
        filter = {
            'query': {
                'match': {
                    field_name: value
                }
            }
        }

    # pass the filter dict to the make_query() function
    resp = make_query(filter)

# have interpreter call the main() func
if __name__ == "__main__":
    main()

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.