
Elasticsearch Query Builder is a powerful tool that allows developers to construct complex queries with ease. It provides a flexible and intuitive way to search and filter data in Elasticsearch indexes.
With Query Builder, you can create queries using a variety of methods, including JSON-based queries, query DSL, and even native Elasticsearch queries. This flexibility makes it an ideal choice for developers who need to work with large datasets.
As we'll explore in this guide, Query Builder is not just a tool for simple searches, but also for complex filtering and aggregation operations.
Getting Started
The Elasticsearch Query Builder is a powerful tool that requires minimal classes to interact with, and the main class you'll need to work with is the Spatie\ElasticsearchQueryBuilder\Builder class.
This class requires an \Elastic\Elasticsearch\Client passed in the constructor, which you can learn more about by checking out the ElasticSearch SDK docs.
To start building your query, you'll need to add queries, aggregations, sorts, fields, and pagination extras using the methods available in the Builder class.
The Builder class contains methods for adding queries, aggregations, sorts, fields, and pagination extras, which you can read more about below.
You can execute your fully built query using $builder->search() or get the raw payload for ElasticSearch with $builder->getPayload().
Expand your knowledge: Elasticsearch Fields
Query Types
Elasticsearch query builder supports a variety of query types, including match queries and range queries.
A match query can be used to search for documents that contain a specific term in a field, as seen in the example of searching for cars with a name of "Ford". This type of query is useful for searching for exact matches in a field.
Range queries, on the other hand, can be used to filter out documents that don't fall within a specific range of values in a field. This is demonstrated in the example of searching for cars with a weight greater than or equal to a particular number.
Bool queries are also supported, allowing you to combine multiple queries in a logical manner. This is shown in the example of building a bool query that searches for documents containing a specific term in one field and a range of values in another field.
Here are some common query types supported by Elasticsearch query builder:
- Match queries: search for exact matches in a field
- Range queries: filter out documents that don't fall within a specific range of values in a field
- Bool queries: combine multiple queries in a logical manner
Match Phrase
The match phrase query is a powerful tool in Elasticsearch that allows you to search for a specific phrase in a document. It's similar to the match query, but instead of searching for a single term, it searches for a phrase.
To use the match phrase query, you can call the QueryBuilders.matchPhraseQuery method, passing in the field name and the phrase you're looking for. For example, in the Elasticsearch Query Builder, you can use the matchPhraseQuery method to search for a specific phrase in a field.
The match phrase query is useful when you need to search for a specific phrase in a document, such as a quote or a title. It's also useful when you need to search for a phrase that contains multiple words, such as a product description.
Here are some examples of how you can use the match phrase query:
As you can see, the match phrase query is a flexible and powerful tool that allows you to search for specific phrases in your documents. By using the match phrase query, you can improve the accuracy of your search results and provide better user experiences for your users.
For another approach, see: Elasticsearch Search Template
Geoshape
Geoshape is a type of spatial query that returns a polygon that encloses all the points in a set of results. This type of query is commonly used in geographic information systems (GIS) to identify the area where a certain condition is met.
A geoshape query can be used to find the area where a certain value is within a certain range, such as finding all the areas where the temperature is between 20 and 30 degrees Celsius. This can be useful for identifying regions with specific climate conditions.
Geoshape queries are often used in conjunction with other spatial queries, such as spatial joins, to retrieve more detailed information about the areas that meet a certain condition. For example, a geoshape query could be used to identify the areas where a certain disease is prevalent, and then a spatial join could be used to retrieve information about the population density in those areas.
Advanced Queries
Advanced Queries can be complex, but they're a powerful tool in Elasticsearch Query Builder. You can use bool queries to return documents that match boolean combinations of other queries.
Bool queries are built using one or more boolean clauses, each with a typed occurrence like must, filter, should, or mustNot. This allows you to specify the exact conditions for a document to be included in the search results.
To make things more specific, you can use the must clause to ensure a query is present in matching documents, while the should clause indicates that a query should be present but isn't required. The mustNot clause, on the other hand, ensures a query is not present in matching documents.
You can also use constant score queries to return documents that have a specific relevance score. This can be useful when you want to prioritize certain documents over others.
Nested queries are another type of advanced query. They allow you to search within nested objects, giving you more flexibility in your searches. This can be particularly useful when dealing with complex data structures.
Here are some common boolean clauses used in bool queries:
Testing Our Implementation

To test our implementation, we can start our server by running npm start. Then we can visit a specific URL to run our query with the provided filters: name, origin, and weight_in_lbs.
The URL is http://localhost:3000/search-by-multiple?Name=ford&Origin=USA&Weight_in_lbs=3000. This is a GET request, and the parameters after the URL are the query parameters required to give us our desired filtered results.
We can copy the link to the collections on POSTMAN and import it to test the query as well. The entire result of the API call is shown below, though it's been truncated for brevity.
The queries in the services.js file are quite readable and easy to grasp. We've made use of the matchQuery, rangeQuery, termQuery, matchPhraseQuery, boolQuery, and matchAllQuery queries provided by the builder library.
As we can see, for each query, we're doing a map on the returned data and outputting the id and the _source fields alone.
Multi-Search
You can execute multiple search queries at once using the Multi-Search API in Elasticsearch. This is made possible by the MultiBuilder class, which allows you to add multiple query bodies to a single request.
Check this out: Elasticsearch Search after
To use the MultiBuilder class, you create an instance of it and add builders to it, which will be executed in the same order they were added. The response will include a responses array with the query results.
The MultiBuilder class provides two main methods: search() and getPayload(). The search() method executes the queries and returns the results, while the getPayload() method returns the raw request payload.
The response from the MultiBuilder class will have a JSON shape that includes a responses array, which contains the query results in the same order they were added.
Range
The range operator is a powerful tool in advanced queries. It allows you to filter results based on a specific range of values.
To use the range operator, you can specify a start and end value, such as "price > 10 and price < 20", which would return all records where the price is between 10 and 20.
You can also use the range operator with dates, such as "created_at > '2020-01-01' and created_at < '2020-12-31'", which would return all records created between January 1st, 2020 and December 31st, 2020.
The range operator can be combined with other operators to create more complex queries.
Suggestion: Elasticsearch Operator
Percolate
Percolate is a type of query that allows you to filter and refine your results.
It uses a combination of AND and OR operators to specify conditions that must be met.
Percolate queries can include multiple filters, such as text, date, and number filters.
You can use Percolate to narrow down a large dataset to a specific subset of results.
For example, a Percolate query might look like this: (text: "example" OR text: "test") AND date: "2022-01-01".
This query would return all documents that contain either the word "example" or the word "test" in the text field, and also have a date of January 1, 2022.
Discover more: Elasticsearch Percolate
Compound
Compound queries are a powerful tool in advanced searching. They allow you to combine multiple queries to create more complex searches.
You can use a bool query to return documents that match boolean combinations of other queries. For example, you can use the must clause to specify a query that must be present in matching documents.
The bool query also includes a filter clause, which is similar to must but ignores the score. This can be useful when you want to include certain documents in your results without affecting their score.
You can also use a constant score query to return documents that have a specific relevance score. This is done using the constantScoreQuery, which takes a provided value as an argument.
Here's a summary of the bool query clauses:
You can also use a nested query to return documents that have a field matching a provided value. This is useful when dealing with nested objects.
Additional reading: Elasticsearch Nested Query
Filtering and Sorting
Filtering and sorting are two powerful tools in the Elasticsearch Query Builder that help you refine your search results.
The Builder has a filter() method that allows you to add filters to your queries. You can read more about how filtering works in the ElasticSearch docs.
You can use the filter() method to exclude specific documents from your search results.
The Builder (and some aggregations) has a addSort() method that takes a Sort instance to sort the results.
You can use the addSort() method to sort your search results in ascending or descending order.
Sorting and Retrieval

The ElasticSearch query builder has a powerful feature that allows you to sort your search results with ease.
You can add a sort to your query using the addSort() method, which takes a Sort instance as an argument.
This method is also available in some aggregations, giving you even more flexibility in how you retrieve and display your data.
For your interest: Sort by Elasticsearch
Adding Sorts
You can add sorts to your Builder using the addSort() method, which takes a Sort instance to sort the results.
The Builder is capable of handling sorts, so you can use this method to customize your results.
To use the addSort() method, you'll need to create a Sort instance, which can be done by reading the ElasticSearch docs for more information.
The addSort() method is a powerful tool for refining your search results, and it's definitely worth exploring further.
By adding a Sort instance to your Builder, you can ensure that your results are displayed in the most useful order possible.
Retrieve Specific Fields
Retrieving specific fields can be a game-changer for performance and efficiency.
You can use the fields() method to request specific fields from the resulting documents without returning the entire _source entry.
This is particularly useful when dealing with large documents or complex queries, as it can significantly reduce the amount of data being transferred.
According to the ElasticSearch docs, the fields parameter can be used to specify which fields to include in the response.
By being selective about which fields you retrieve, you can improve the overall performance of your queries and reduce the load on your system.
You might like: Elasticsearch Performance
Highlighting and Pagination
The highlight() method can be used to add a highlight section to your query along the rules in the ElasticSearch docs.
You can use the highlight() method to add emphasis to specific parts of your query, making it easier to scan and understand the results.
For instance, you can use the highlight() method to highlight specific keywords or phrases in your query, making it more engaging and user-friendly.
Intriguing read: Elasticsearch Highlight
Highlighting

Highlighting is a powerful feature in ElasticSearch that allows you to add a highlight section to your query. The highlight() method can be used to achieve this.
You can use the highlight() method to add a highlight section to your query along the rules in the ElasticSearch docs. This method is a key part of making your search results more user-friendly and visually appealing.
The highlight() method is a simple yet effective way to make your search results stand out. By using it, you can draw attention to the most relevant information in your search results.
Pagination
Pagination is a crucial feature in search functionality. It allows users to navigate through large amounts of data by displaying only a portion of the results at a time.
The Builder features a size() and from() method for the corresponding ElasticSearch search parameters. These can be used to build a paginated search.
To give you a better idea, the size() method can be used to specify the number of results to display per page. This can be especially useful when dealing with large datasets.
A paginated search can be built by combining the size() and from() methods. This approach enables users to navigate through the results by clicking on page numbers or using navigation arrows.
Java API and Integration

In Java API, the SearchRequest and SearchSourceBuilder objects are used for any searching operation, and to add specific queries, a query builder implementing the QueryBuilder interface is added to the request.
To create a QueryBuilder, you can call the constructor or use the helper methods of QueryBuilders.
The basic search request with a match query against index drivers looks like this:
Java API
The Java API is a powerful tool for searching and querying data. It's built around two main objects: SearchRequest and SearchSourceBuilder.
To add specific queries to a search request, you need to use a QueryBuilder that implements the QueryBuilder interface. There are query builders for each type of query supported by the Query DSL.
You can create a QueryBuilder by calling its constructor or using the helper methods of QueryBuilders. This is how you add more conditions to a search request.
BoolQueryBuilder is a useful tool for combining different queries with conditions. You can put different queries as parameters to the filter method to create a more complex query.
For example, you can create a query that looks for all drivers who are active, from Germany, have no titles, have at least one win, and were born after January 1, 1985. This query is equivalent to a sentence that specifies these conditions.
Native Integration

Native Integration is a powerful feature that allows seamless interaction between systems. Knowi provides a native connection to Elasticsearch, allowing users to query Elasticsearch indices directly.
This native integration is made possible through Elasticsearch DSL queries or Looker's SQL-like interface, known as LookML. Users can leverage these interfaces to connect with Elasticsearch data sources and query them using the visual builder or c9QL.
With this integration, users can easily access and analyze their Elasticsearch data without needing to worry about complex data transformations or mappings. The native integration streamlines the process, making it efficient and user-friendly.
Full Text Search
Full Text Search is a powerful tool in Elasticsearch Query Builder that allows you to search for specific keywords within your documents.
You can use the match query, which is the standard query for performing full-text search, to return documents that match the provided keyword.
The match query is flexible and can handle different types of data, including text, numbers, dates, and booleans.
On a similar theme: Match Query in Elasticsearch

If you need to search multiple fields, you can use the multi match query, which allows you to run the same match query on multiple fields.
Here are the different types of full-text queries you can use:
- match query – QueryBuilders.matchQuery
- multi match query – QueryBuilders.multiMatchQuery
- match phrase query – QueryBuilders.matchPhraseQuery
These queries can be used to create complex search queries that return relevant results from your indexed data.
Featured Images: pexels.com


