Sample ElasticSearch Queries

1. Query all.

GET http://localhost:9200/index/type/_search

2. Query by single value, using a query string.

GET http://localhost:9200/index/type/_search?q=firstName:John

3. Query by single term, using request body.

GET http://localhost:9200/index/type/_search
    {
        "query": {
            "term": { "firstName" : "John" }
        }
    }

4. Paging query.

GET http://localhost:9200/index/type/_search
    {
        "from" : 0,
        "size" : 10,
        "query" : {
            "term" : { "firstName" : "John" }
        }
    }

5. Pre-filter.

 GET http://localhost:9200/index/type/_search
    {
        "query": {
            "filtered": {
                "filter": {
                    "term": { "firstName" : "John" }
                }
            }
        }
    }

6. Pre-filters.

GET http://localhost:9200/index/type/_search
    {
        "query": {
            "filtered": {
                "filter": {
                    "and": {
                        "filters": [ {
                            "term": { "firstName": "John", "country": "US" }
                        } ]
                    }
                }
            }
        }
    }

7. Pre-filter terms.

GET http://localhost:9200/index/type/_search
    {
        "query": {
            "filtered": {
                "filter": {
                    "and": {
                        "filters": [ {
                            "term": { "firstName": "John" }
                        }, {
                            "terms": { "country": [ "US", "CA" ] }
                        } ]

8. Missing email filter.

GET http://localhost:9200/index/type/_search

    {
        "query": {
            "filtered": {
                "filter": {
                    "missing": { "field" : "emailAddress" }
                }
            }
        }
    }

9. Pre-filtered query.

GET http://localhost:9200/index/type/_search
    {
        "query": {
            "filtered": {
                "query": {
                    "term": { "firstName": "John" }
                },
                "filter": {
                    "term": { "country": "US" }
                }
            }
        }
    }

10. Nested filter.

GET http://localhost:9200/index/type/_search

    {
        "query": {
            "filtered": {
                "filter": {
                    "nested": {
                        "path": "person",
                        "filter": {
                            "term": { "person.country": "US" }
                        }
                    }
                }
            }
        }
    }

11. Nested and non-nested filter.

{
        "query": {
            "filtered": {
                "filter": {
                    "and": {
                        "filters": [ {
                            "term": { "firstName": "John" }
                        }, {
                            "nested": {
                                "path": "person",
                                "filter": {
                                    "term": { "person.country": "US" }
                                }
                            }
                        } ]
                    }
                }
            }
        }
    }