# Elasticsearch in Action - Roy Russo, Radu Gheorghe, Matthew Lee Hinman Published: 2015-11-17 by Simon and Schuster ![Elasticsearch in Action cover](http://books.google.com/books/content?id=6zkzEAAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api) ## Queries There are different types of queries, which are sent as JSON to the `/_search` endpoint. ### `match` query Match all documents. Query string analyzed. Input tokens are OR by default. ### `multi_match` Query across multiple fields ### `prefix` query Well, prefix search. ### `fuzzy` search Multi letter changes (set by `fuzziness`) ### Term-level queries This is for querying structured data. Structured fields are stored as is (not run through an analyzer) - `term` query to match a single term - `range` query to match a range ### Compound queries Create more sophisticated queries. - `bool` query - `must` - must match, contributes to score - `must_not` - must not match, no score - `should` - not mandatory, but contributes to score - `filter` - must match, no score - `constant_score` query - `function_score` query - `boosting` query - `dismax_query` (disjunction max) query ### Aggregations - metric aggregations - sum, min, max, average - bucket aggregations - pipeline aggregations - work on the output from other aggregations ## Query examples ### `match` query ```json { "query": { "match": { "author": { #A The author field is now having inner properties defined "query": "Joshua Schildt", #B provide your query here "operator": "AND" #C The AND operator (default is OR) } } } } ``` ### `multi_match` query ```json { "query": { "multi_match": { #A Multi match query that searches across multiple fields "query": "Java", #B The search words "fields": ["title","synopsis"] #C Searching across two fields } } } ``` ### `fuzzy` query ```json { "query": { "fuzzy": {#A Fuzzy query to support spelling mistakes "title": { "value": "kava",#B The incorrectly spelt criteria "fuzziness": 1 #C Fuzziness 1 indicates one letter forgiveness } } } } ``` ### Term-level queries ```json GET books/_search { "query": { "range": { #A Range query declaration "amazon_rating": {#B Mention the range to match "gte": 4.5,#C gte - greater than or equal to "lte": 5 #D lte - less than or equal to } } } } ``` ```json { "query": { "range": { #A Range query declaration "amazon_rating": {#B Mention the range to match "gte": 4.5,#C gte - greater than or equal to "lte": 5 #D lte - less than or equal to } } } } ``` ### Compound-queries ```json { "query": { "bool": { #A A boolean query "must": [{# A must clause - the documents must match to the criteria "match": { #A One of the queries - a match query "author": "Joshua Bloch" } }] } } } ``` ```json { "query": { "bool": { "must": [{ #A Must query with two leaf queries "match": {#B A match query finding books authored by Joshua "author": "Joshua Bloch" } }, { "match_phrase": {#C A second query searching for a phrase "synopsis": "best Java programming books" } }] } } } ``` ### Aggregations ```json { "aggs": { #A Writing an aggregation query "critical_patients": { #B User defined query output name "sum": {#C The sum metric - sum of all the critical patients "field": "critical" #D The field on which the aggregation is applied } } } } ``` Buckets: ```json { "size": 0, "aggs": { "critical_patients_as_histogram": {#A The user-defined name of the report "histogram": {#B The type of the bucketing aggregation - histogram "field": "critical",#C The field the aggregation applied on "interval": 2500#D The bucket interval } } } } ```