Show Menu
Cheatography

ElasticSearch Query DSL Cheat Sheet (DRAFT) by

Cheatsheet with query and filters examples.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

match all query

{
    "match_all" : { }
}

match query

{
    "match" : {
        "message" : "this is a test"
    }
}

filtered query

If no query, default to match_all
{
  "filtered": {
    "query": {
      "match": { "tweet": "full text search" }
    },
    "filter": {
      "range": { "created": { "gte": "now - 1d / d" }}
    }
  }
}

multi_­match query

{
  "multi_match" : {
    "query":    "this is a test", 
    "fields": [ "subject", "message" ] 
  }
}

bool query

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        },
        "must_not" : {
            "range" : {
                "age" : { "from" : 10, "to" : 20 }
            }
        },
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}

range query

{
    "range" : {
        "age" : {
            "gte" : 10,
            "lte" : 20
        }
    }
}

{
    "range" : {
        "born" : {
            "gte": "2012-01-01",
            "lte": "now",
            "time_zone": "+1:00"
        }
    }
}

fuzzy query

Apply +/- margin

{
    "fuzzy" : {
        "price" : {
            "value" : 12,
            "fuzziness" : 2
        }
    }
}

{
    "fuzzy" : {
        "created" : {
            "value" : "2010-02-05T12:05:07",
            "fuzziness" : "1d"
        }
    }
}

nested query

Nested objects with nested mappings
{
    "nested" : {
        "path" : "obj1",
        "query" : {
            "bool" : {
                "must" : [
                    {
                        "match" : {"obj1.name" : "blue"}
                    },
                    {
                        "range" : {"obj1.count" : {"gt" : 5}}
                    }
                ]
            }
        }
    }
}

prefix query

{
    "prefix" : { "user" : "ki" }
}

regexp query

{
    "regexp":{
        "name.first": "s.*y"
    }
}
 

and filter

The result of the filter is not cached by default (_cache:true)
{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "and" : {
                "filters": [
                    {
                        "range" : {
                            "postDate" : {
                                "from" : "2010-03-01",
                                "to" : "2010-04-01"
                            }
                        }
                    },
                    {
                        "prefix" : { "name.second" : "ba" }
                    }
                ],
                "_cache" : true
            }
        }
    }
}

bool filter

{
    "filtered" : {
        "filter" : {
            "bool" : {
                "must" : {
                    "term" : { "tag" : "wow" }
                },
                "must_not" : {
                    "range" : {
                        "age" : { "from" : 10, "to" : 20 }
                    }
                },
                "should" : [
                    {
                        "term" : { "tag" : "sometag" }
                    },
                    {
                        "term" : { "tag" : "sometagtag" }
                    }
                ]
            }
        }
    }
}

geo distance filter

Require a geo point mapping type

{
    "filtered" : {
        "query" : {
            "match_all" : {}
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

nested filter

{
    "filtered" : {
        "filter" : {
            "nested" : {
                "path" : "obj1",
                "filter" : {
                    "bool" : {
                        "must" : [
                            {
                                "term" : {"obj1.name" : "blue"}
                            },
                            {
                                "range" : {"obj1.count" : {"gt" : 5}}
                            }
                        ]
                    }
                }
            }
        }
    }
}

not filter

{
    "filtered" : {
        "filter" : {
            "not" : {
                "range" : {
                    "postDate" : {
                        "from" : "2010-03-01",
                        "to" : "2010-04-01"
                    }
                }
            }
        }
    }
}

not filter

{
    "filtered" : {
        "filter" : {
            "not" : {
                "range" : {
                    "postDate" : {
                        "from" : "2010-03-01",
                        "to" : "2010-04-01"
                    }
                }
            }
        }
    }
}

nested filter

{
    "filtered" : {
        "filter" : {
            "nested" : {
                "path" : "obj1",
                "filter" : {
                    "bool" : {
                        "must" : [
                            {
                                "term" : {"obj1.name" : "blue"}
                            },
                            {
                                "range" : {"obj1.count" : {"gt" : 5}}
                            }
                        ]
                    }
                }
            }
        }
    }
}