This is a cache of https://www.elastic.co/search-labs/blog/llm-functions-elasticsearch-intelligent-query. It is a snapshot of the page at 2025-03-16T00:38:30.309+0000.
Unifyin<strong>g</strong> Elastic vector database and LLM functions for intelli<strong>g</strong>ent query - Elasticsearch Labs

Unifying Elastic vector database and LLM functions for intelligent query

Leverage LLM functions for query parsing and Elasticsearch search templates to translate complex user requests into structured, schema-based searches for highly accurate results.

Imagine searching for “recently renovated accommodations 250m from Belongil Beach with at least 4 stars and a pool and gym” and having your search engine return exactly what you needed. Intelligent search understands intent and reasons with your queries. Reasoning and intention discovery require sophistication beyond a heuristics-only approach. This is where large language model functions and Elasticsearch search templates come together to deliver a truly intelligent search experience.

Try it yourself

If you're not a believer, no worries. This entire end-to-end example is available within a Python notebook here . The notebook includes data, index mapping, inferencing endpoints, search templates, and LLM functions. You'll need an Elastic Cloud instance, Azure OpenAI instance, and google Maps API key.

The Problem: Capturing complex constraints

Search approaches such as keyword or even vector-based methods are challenged when users pack multiple nuances into a single request. Consider a hotel finder scenario. A person wants:

  1. A hotel near Belongil Beach, within 250 meters.
  2. A minimum rating of 4 stars.
  3. A recently renovated property.
  4. Specific amenities like a pool and gym.

Trying to encode all of these requirements using naive keyword matches or basic similarity scores might return incomplete or irrelevant results, reducing user trust and experience.

Schema-Based Searches with LLMs Elasticsearch is built on an index schema architecture. When you index data, you define fields like “rating,” “geopoint,” or “amenities,” making it much easier to filter and rank results accurately. However, this structure demands that queries be equally structured. That’s where LLMs (like gPT-based or generation models) become the linchpin.

An LLM can interpret a user’s natural language query, extract key attributes (“distance = 250m,” “rating &gt;= 4,” “amenities = pool, gym,” “near Belongil Beach,” etc.), and even call a geocoder service when a geo component has been detected. It then outputs a JSON payload ready to be slotted into an Elasticsearch search template—a parameterized query that cleanly separates the query logic from the dynamic values. With this approach, we capitalize on both the semantic understanding of LLMs and the schema-based filtering and faceting of Elasticsearch.

Example in action

Suppose your user’s query is:

“recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym.”

  1. LLM processing: The LLM breaks down the text, recognizes that it needs a distance-based filter (250m), a minimum rating (4 stars), relevant amenities (pool, gym), and even a contextual hint about being “recently renovated.” It also calls a geocoding service for “Belongil Beach,” returning precise latitude and longitude coordinates.
  2. Search template: You create an Elasticsearch search template that expects parameters like rating, distance, latitude, longitude, and possibly a text query for any free-form conditions. Once the LLM provides these parameters, your application simply fills in the placeholders and calls Elasticsearch. Here, not only is filtering leveraged, but also hybrid queries using vectors, ELSER, and lexical search.
  3. Results: The response precisely matches accommodations within 250 meters of Belongil Beach, has at least 4 stars, is flagged as recently renovated, and includes a pool and gym. An example result might be:
    • Hotel name: Belongil Beach Apartment
    • Rating: 4 stars
    • City: Byron Bay, New South Wales
    • Country: Australia

Rather than depending solely on a vector space or hybrid search, you can input precise filters, which will make the recall more comprehensive and the precision more accurate.

Why this approach works

Precision and recall: By structuring the query according to the index schema, you remove ambiguity, ensuring you don’t miss valid results (high recall) and keep out irrelevant ones (high precision). This is often observed when relying solely on a vector space, which doesn’t naturally offer distillation features.

Scalability: Elasticsearch is designed for massive data volumes. Once the parameters are extracted, the query itself remains blazing fast, even on huge indexes.

Flexibility: If new attributes appear (e.g., “EV charging station”), the LLM functions should capture the attribute as a hotel amenity and inject it into the Elasticsearch search template.

Resilience to complexity: No matter how complex a user’s query, the LLM’s semantic parsing ensures every relevant detail is captured: distance constraints, star ratings, location-based conditions, and more.

Why search templates

Elasticsearch templates enable the creation of parameterized queries, separating the query logic from dynamic values. This is particularly valuable when building dynamic queries based on user input or other variable data.

For example, consider the hotel index with fields

  • Description
  • Attractions
  • Rating
  • Facilities
  • Location
    • Latitude
    • Longitude

Users might include any combination of these attributes in their search query. As the number of fields increases, manually constructing a query for every potential input combination becomes impractical. Search templates provide a solution by dynamically generating the appropriate query based on the user's input. If the user specifies a rating and attractions, the corresponding query is generated. Similarly, if the user provides a location and rating, the search template generates a query that reflects those inputs.

Search templates are defined using a JSON format that includes placeholders for dynamic values. When you execute a search template, Elasticsearch replaces the placeholders with the actual values and then executes the query.

Search templates can be used to perform a variety of tasks, such as:

  • Filtering results based on user input
  • Boosting the relevance of certain results
  • Adding custom scoring functions
  • Aggregating results

Below is an example of the search template that dynamically creates a query based on input parameters.

LLM functions

Large Language Model functions utilize strong reasoning capabilities to determine the optimal subsequent action, such as parsing data, calling an API, or requesting additional information. When combined with search templates, LLMs can determine if a user query contains an attribute supported by a search template. If a supported attribute is identified, the LLM will execute the corresponding user-defined method call.

Within the notebook, there are a few LLM functions. Each function is defined with the tools list.

Let’s briefly review each one. The role of the `extract_hotel_search_parameters` LLM function is to extract the parameters from the user query that the search template supports.

The `geocode_location` LLM function would be invoked if a location attribute such as "500 meters from Belongil Beach" is identified.

The LLM function `query_elasticsearch` will be called using the `geocode_location` (if it was found within the user query) and the parameters from the LLM function `extract_hotel_search_parameters`.

The completions API registers each LLM function as a tool. This list of tools was detailed earlier in the article.

Azure OpenAI

The notebook uses an Azure OpenAI completions model and to run it, you will need the Azure OpenAI Key (either Key 1 or Key 2), Endpoint, Deployment Name, and Version number. All this information can be found under Azure OpenAI → Keys and Endpoint.

Deploy a completion model. That is the deployment name used within the notebook.

Under Chat playground, click on View code to find the api version.

google Maps API

The notebook uses the google Maps API to geocode locations identified within user queries. This functionality requires a google account and an API key, which can be generated here.

Putting LLM functions and search templates into Action

The LLM uses reasoning to determine the necessary functions and their order of execution based on the given query. Once a query is executed such as "recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym", the LLM reasoning layer is exposed:

Extract parameters

The initial LLM function call is designed to pull parameters from the query.

geocoding

The LLM then determined that the query contained a 'from' location and that a geocoder function should be called next.

Intelligent query

The reasoning layer of the LLM uses the parameters from previous function calls to execute an Elasticsearch query with search templates.

Precise results

Using LLM functions along with a search template to execute an intelligent query, a perfect match has been found.

Conclusion

Combining the power of Large Language Models functions with Elasticsearch search templates ushers in capabilities for query intent and reasoning. Rather than treating a query as an unstructured blob of text, we methodically break it down, match it against a known schema, and let Elasticsearch handle the heavy lifting of search, filtering and scoring. The result is a highly accurate, user-friendly search experience that feels almost magical—users simply speak (or type) their minds, and the system understands precisely what they mean.

Try out vector search for yourself using this self-paced hands-on learning for Search AI. You can start a free cloud trial or try Elastic on your local machine now.

Related content

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as your are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself