Wildcard and Boolean Search in Elasticsearch
In my last blog, I have explained basic Elasticsearch queries using which we can create basic search queries. Now in this blog, I will explain advanced search queries using which we can construct more complex queries like boolean queries, wildcard queries, etc. So let's start to create the search queries:
Wildcard Query:
Using wildcard queries we can search for items without knowing the exact spelling. Means if someone is not knowing the exact spelling of a word then also he/she can search that word. See the below example:
GET /blogs/technical/_search
{
"query": {
"wildcard": {
"topic": "ki??na"
}
}
}
In the above query, we are looking for a word which starts with 'ki' and ends with '
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "blogs",
"_type": "technical",
"_id": "2",
"_score": 1,
"_source": {
"topic": "introduction to Kibana"
}
}
]
}
}
The result shows the topic "introduction to Kibana" because of the wildcard search 'ki?
GET /blogs/technical/_search
{
"query": {
"wildcard": {
"topic": "k*na"
}
}
}
In the above query, we only know that the word starts with "k" and ends with "
Boolean Query:
The boolean query is used to search the results on the basis of joining them with 'or', 'and', 'not' conditions. Like joining two conditions with any of them for example:
"name": "anurag" and "age": "30"
"name": "anurag" or "name": kapil"
The above examples are just a representation and not the actual Elasticsearch query. Now let's understand how we can achieve the same type of conditions in Elasticsearch. See the below example:
GET /blogs/technical/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"_type": "technical"
}
}
],
"must_not": [
{
"match": {
"topic": "kibana"
}
}
]
}
}
}
In the above query, I am applying the boolean query by passing the "bool" key after the "query" keyword and then under the "bool" block I have provided two blocks "must" and "must_not". These two blocks have a totally different meaning as "must" is there to ensure the existence of the given condition inside the block while "must_not" is there to ensure the non-existence of the given condition inside on the block. Now under "must" block I have added a "match" block to match the "_type" key with value as "technical" and under "must_not" I have added, "match" block to match the "topic" with "
So what will happen? in the above query, Elasticsearch will exclude all documents where topic matches with "Kibana" and will include where "_type" key matches with value "technical". Above query will return the following result:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "blogs",
"_type": "technical",
"_id": "1",
"_score": 1,
"_source": {
"topic": "introduction to Elasticsearch",
"category": "ELK"
}
}
]
}
}
In the above result, we have found the "_type" as "technical" and "topic" as "introduction to Elasticsearch" and there is no "Kibana". Take one more example:
GET /blogs/technical/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"_type": "technical"
}
},
{
"match": {
"topic": "kibana"
}
}
]
}
}
}
In the above query, we are matching the "_type" as "technical" and "topic" as "
GET /blogs/technical/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"_type": "technical"
}
},
{
"match": {
"topic": "kibana"
}
}
]
}
}
}
In the above query, I have replaced the "must" with "should" so now query will list all those documents where the "_type" matches with "technical" or "topic" match with "
In this blog, I have tried to explain the wildcard query and boolean query of Elasticsearch.
Other Blogs on Elastic Stack:
Introduction to Elasticsearch
Elasticsearch Installation and Configuration on Ubuntu 14.04
Log analysis with Elastic stack
Elasticsearch Rest API
Basics of Data Search in Elasticsearch
Elasticsearch Rest API
Wildcard and Boolean Search in Elasticsearch
Configure Logstash to push MySQL data into Elasticsearch
Metrics Aggregation in Elasticsearch
Bucket Aggregation in Elasticsearch
How to create Elasticsearch Cluster
If you found this article interesting, then you can explore “Mastering Kibana 6.0”, “Kibana 7 Quick Start Guide”, “Learning Kibana 7”, and “Elasticsearch 7 Quick Start Guide” books to get more insight about Elastic Stack, how to perform data analysis, and how you can create dashboards for key performance indicators using Kibana.
You can also follow me on:
- LinkedIn: https://www.linkedin.com/in/anubioinfo/
- Twitter: https://twitter.com/anu4udilse
- Medium: https://anubioinfo.medium.com
Comments (0)
Leave a comment
Related Blogs
Introduction to Kibana
Aug 1, 2020, 6:19:45 PM | Anurag Srivastava
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava
Introduction to Elasticsearch Aggregations
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava
Basics of Data Search in Elasticsearch
Aug 4, 2018, 7:02:21 AM | Anurag Srivastava
Elasticsearch REST APIs
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Top Blogs
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Elasticsearch REST APIs
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
How to count number of words in a HTML string and find Read time in Python 3
Jun 30, 2018, 12:07:47 PM | jitender yadav
Create a Chess board in PHP
Mar 9, 2020, 8:45:41 AM | Rocky Paul
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava