English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Elasticsearch, searching is done by using JSON-based queries. A query consists of two clauses-
Leaf query clauses—these clauses are matches, terms, or ranges, which search for specific values in specific fields.
Composite query clauses—these queries are combinations of leaf query clauses and other composite queries, used to extract the required information.
Elasticsearch supports a large number of queries. Queries start with a query keyword, followed by a JSON object containing conditions and filters. The following describes different types of queries.
This is the most basic query; it returns all content, with each object scoring1.0.
POST /schools/_search { "query": { "match_all":{} } }
When running the above code, we get the following results-
{ "took": : 7, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 2, "relation": "eq" } "max_score": 1.0, "hits": [ { "_index": "schools", "_type": "school", "_id": "5", "_score": 1.0, "_source": { "name": "Central School", "description": "CBSE Affiliation", "street": "Nagan", "city": "paprola", "state": "HP", "zip": "176115", "location": [ 31.8955385, 76.8380405 ], "fees": : 2200, "tags": [ "Senior Secondary", "beautiful campus" ], "rating": "3.3" } } { "_index": "schools", "_type": "school", "_id": "4", "_score": 1.0, "_source": { "name": "City Best School", "description": "ICSE", "street": "West End", "city": "Meerut", "state": "UP", "zip": "250002", "location": [ 28.9926174, 77.692485 ], "fees": : 3500, "tags": [ "fully computerized" ], "rating": "4.5" } } } } }
These queries are used to search full text, such as chapters or news articles. The query works based on the analyzer associated with the specific index or document. In this section, we will discuss different types of full-text queries.
This query matches text or phrases with the values of one or more fields.
POST /schools*/_search { "query": { "match": { "rating":"4.5" } } }
After running the above code, we get the following response:
{ "took": : 44, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 1, "relation": "eq" } "max_score": 0.47000363, "hits": [ { "_index": "schools", "_type": "school", "_id": "4", "_score": 0.47000363, "_source": { "name": "City Best School", "description": "ICSE", "street": "West End", "city": "Meerut", "state": "UP", "zip": "250002", "location": [ 28.9926174, 77.692485 ], "fees": : 3500, "tags": [ "fully computerized" ], "rating": "4.5" } } } } }
This query matches text or phrases that match one or more fields.
POST /schools*/_search { "query": { "multi_match": { "query": "paprola" "fields": [ "city", "state" ] } } }
After running the above code, we get the following response:
{ "took": : 12, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 1, "relation": "eq" } "max_score": 0.9808292, "hits": [ { "_index": "schools", "_type": "school", "_id": "5", "_score": 0.9808292, "_source": { "name": "Central School", "description": "CBSE Affiliation", "street": "Nagan", "city": "paprola", "state": "HP", "zip": "176115", "location": [ 31.8955385, 76.8380405 ], "fees": : 2200, "tags": [ "Senior Secondary", "beautiful campus" ], "rating": "3.3" } } } } }
This query uses a query parser and the query_string keyword.
POST /schools*/_search { "query": { "query_string":{ "query":"beautiful" } } }
After running the above code, we get the following response:
{ "took": : 60, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 1, "relation": "eq" } ………………………………….
These queries mainly handle structured data, such as numbers, dates, and enumerations.
POST /schools*/_search { "query": { "term":{"zip":"176115} } }
After running the above code, we get the following response:
…………………………….. "hits": [ { "_index": "schools", "_type": "school", "_id": "5", "_score": 0.9808292, "_source": { "name": "Central School", "description": "CBSE Affiliation", "street": "Nagan", "city": "paprola", "state": "HP", "zip": "176115", "location": [ 31.8955385, 76.8380405 ], } } } …………………………………………..
This query is used to find objects with values within a given range. To do this, we need to use operators, such as-
gte −greater than or equal to
gt −greater than
lte −less than or equal to
lt −less than
For example, observe the code provided below-
POST /schools*/_search { "query": { "range": { "rating": { "gte":3.5 } } } }
After running the above code, we get the following response:
{ "took": : 24, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 1, "relation": "eq" } "max_score": 1.0, "hits": [ { "_index": "schools", "_type": "school", "_id": "4", "_score": 1.0, "_source": { "name": "City Best School", "description": "ICSE", "street": "West End", "city": "Meerut", "state": "UP", "zip": "250002", "location": [ 28.9926174, 77.692485 ], "fees": : 3500, "tags": [ "fully computerized" ], "rating": "4.5" } } } } }
There are also other types of term-level queries, such as-
Existence Query −If a field has a non-empty value.
Missing Query −This is exactly the opposite of the existence query, which searches for objects without specific fields or empty values.
Wildcard or regexp query −This query uses regular expressions to find patterns in objects.
These queries are a collection of different queries, which are combined using boolean operators (such as and/or, or not) or merged with different indices or those with function calls, etc.
POST /schools/_search { "query": { "bool": { "must": { "state": "UP" } "term": { "filter": {22"term": { } "minimum_should_match": "00" 1, "boost": : 1.0 } } }
After running the above code, we get the following response:
{ "took": : 6, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": 0, "relation": "eq" } "max_score": null, "hits": [ } }
These queries handle geolocation and geolocation. These queries help find geographical objects near schools or any other geographical location. You need to use the geolocation data type.
"mappings": { /geo_example { "properties": { "type": "geo_shape" "location": { "acknowledged": true, } } } }
After running the above code, we get the following response:
{ "shards_acknowledged": true, "index": "geo_example" }
Now, we will publish the data to the index created above.
POST /geo_example/_doc?refresh { "name": "Chapter One, London, UK", "location": { "type": "point", "coordinates": [11.660544, 57.800286} } }
After running the above code, we get the following response:
{ "took": : 1, "timed_out": false, "_shards": { "total": : 1, "successful": : 1, "skipped": 0, "failed": 0 } "hits": { "total": { "value": : 2, "relation": "eq" } "max_score": 1.0, "hits": [ "_index": "geo_example", "_type": "_doc", "_id": "hASWZ2oBbkdGzVfiXHKD "_score": 1.0, "_source": { "name": "Chapter One, London, UK", "location": { "type": "point", "coordinates": [ 11.660544, 57.800286 } } } } }