ElasticSearch – query with cURL

This will be a very short post. Actually, its purpose is to remind myself of the ways to query ES from terminal. Why do it from terminal when there’s a nice ES web console (elasticsearch-head plugin)? Well suppose you don’t have an ES web console. Or maybe you don’t trust it, because it so happens that sometimes results differ when you query from ES-head and from terminal. Anyway, there are several ways to do it using the terminal.

  1. Using a query string. This is a GET request.

        # query for documents / rows with title field containing 'jones'
        # added pretty=true to get the json results pretty printed
        curl http://localhost:9200/myindex/_search?q=title:jones&size=5&pretty=true
  2. Using a JSON query. This is a POST request and you are passing the query with the -d parameter.

        curl -X POST  http://localhost:9200/myindex/_search -d '{
          "title": "jones",
          "amount": 5.7
  3. If a JSON query is big, you can save it as a file and use like this:

        curl -X POST  http://localhost:9200/myindex/_search -d '@queryfile.json'

    You can use a full query syntax in a JSON query, for example, add the sort and fields parameters:

        "from": 0, 
        "size": 10,
        "query": {
            "bool": {
                "must": {
                    "term": {
                        "title": "children"
        "sort": [
                "pubDate": {
                    "order": "desc"
        "fields": ["id", "title", "pubDate"]

    If you want to inspect the result at your leisure, you can save it as a file just as easily:

        curl -X POST  http://localhost:9200/myindex/_search -d '@queryfile.json' > result.json

More about querying ES in the tutorial here.


About Maryna Cherniavska

I have productively spent 10+ years in IT industry, designing, developing, building and deploying desktop and web applications, designing database structures and otherwise proving that females have a place among software developers. And this is a good place.
This entry was posted in ElasticSearch, linux and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s