When a wildcard query is executed in Elasticsearch, it searches for documents that contain terms that match a specified pattern using wildcards. The wildcard query searches the inverted index for terms that match the specified pattern and returns any documents that contain a matching term.
The wildcard character `*` matches zero or more characters, while the wildcard character `?` matches exactly one character. The pattern can include any number of wildcard characters and can be used to match a wide range of terms.
Here’s an example of a wildcard query in Elasticsearch:
GET /my_index/_search { "query": { "wildcard": { "title": { "value": "elasticsearch*" } } } }
In this example, we are searching the `title` field in the `my_index` index for terms that match the pattern “elasticsearch*”, using the wildcard character `*`. The wildcard query will return any documents that contain a term in the `title` field that matches the specified pattern, such as “elasticsearch”, “elasticsearch2” or “elasticsearch-engine”.
The wildcard query can also be used in combination with other query types, such as the bool query, to construct more complex search queries. By allowing for searching for terms that match a specified pattern using wildcards, the wildcard query provides a powerful and flexible way to search for text in Elasticsearch.
It’s important to note that wildcard queries can be computationally expensive and may not be suitable for large datasets or high-traffic applications. Additionally, wildcard queries can be less precise than other query types, such as the term or match query, because they match a broader range of terms.