In today’s data-driven world, search functionality is a critical part of most applications. Whether you’re building a product catalog, a blog search, or an enterprise document search system, full-text search capabilities can make or break the user experience. Elasticsearch is one of the most powerful and widely-used search engines, enabling developers to build sophisticated search applications with ease.
In this blog post, we’ll walk through the process of building a full-text search application using Elasticsearch 8.17. We’ll cover the key concepts, how to set up your Elasticsearch instance, how to index and search documents, and how to integrate Elasticsearch with a web application.
What is Full-Text Search?
Full-text search refers to the process of searching for words, phrases, or terms in large collections of text documents. Unlike simple keyword-based searches, full-text search engines like Elasticsearch are designed to understand and analyze the content of the text, enabling advanced features like:
Elasticsearch provides powerful features for full-text search, including advanced query capabilities, filtering, and scoring to ensure relevant results.
Prerequisites
Before we dive into building the application, let’s quickly go over the prerequisites:
Step 1: Setting Up Elasticsearch 8.17
Elasticsearch is designed for scalability and distributed search. The first thing you need to do is install and configure it.
You can download Elasticsearch 8.17 from the official website:
Once installed, start Elasticsearch by running the following command:
./bin/elasticsearch
By default, Elasticsearch runs on port 9200. You can test if it’s running by navigating to http://localhost:9200 in your browser.
Kibana is the UI for Elasticsearch and is highly recommended for easier management, visualization, and troubleshooting.
Step 2: Indexing Documents in Elasticsearch
The first step in creating a full-text search application is indexing your data into Elasticsearch. Elasticsearch stores data in indices, and you can create these indices to hold your documents. Let’s start by creating an index and adding a few sample documents.
In this example, let’s create an index called “books” for storing information about books. You can use Kibana Dev Tools or the Elasticsearch Python client to interact with Elasticsearch.
If you’re using Kibana Dev Tools, the following command will create an index called “books”:
PUT /books
{
“mappings”: {
“properties”: {
“title”: { “type”: “text” },
“author”: { “type”: “text” },
“description”: { “type”: “text” },
“published_year”: { “type”: “integer” }
}
}
}
This defines the mapping for the index, where the fields like title, author, and description are indexed as text, which is suitable for full-text search.
You can add some sample book documents to your index. Here’s how you can do it using the Elasticsearch Python client:
from elasticsearch import Elasticsearch
# Connect to Elasticsearch
es = Elasticsearch()
# Index some sample books
books = [
{
“title”: “The Great Gatsby”,
“author”: “F. Scott Fitzgerald”,
“description”: “A novel set in the Jazz Age that examines the American Dream.”,
“published_year”: 1925
},
{
“title”: “To Kill a Mockingbird”,
“author”: “Harper Lee”,
“description”: “A young girl witnesses the racial struggles in the southern United States.”,
“published_year”: 1960
},
{
“title”: “1984”,
“author”: “George Orwell”,
“description”: “A dystopian novel that explores surveillance, totalitarianism, and censorship.”,
“published_year”: 1949
}
]
# Index documents into Elasticsearch
for i, book in enumerate(books):
es.index(index=”books”, id=i+1, document=book)
This Python script will index three books into the “books” index.
Step 3: Performing Full-Text Search
Once your documents are indexed in Elasticsearch, it’s time to search through them. Elasticsearch provides powerful query types for full-text search, such as match queries and bool queries.
The most common query for full-text search is the match query. It analyzes the query text and searches for documents that match the terms in the indexed fields.
Here’s how you can search for a book by its title using the match query:
response = es.search(
index=”books”,
query={
“match”: {
“title”: “gatsby”
}
}
)
# Display the results
for hit in response[‘hits’][‘hits’]:
print(f”Title: {hit[‘_source’][‘title’]}”)
print(f”Author: {hit[‘_source’][‘author’]}”)
print(f”Description: {hit[‘_source’][‘description’]}”)
This will search for any document in the “books” index where the title contains the word “gatsby”.
If you want to create more complex queries, you can use a bool query, which allows you to combine multiple conditions (e.g., must have a certain word in the title, or match a phrase in the description).
response = es.search(
index=”books”,
query={
“bool”: {
“must”: [
{“match”: {“title”: “1984”}},
{“match”: {“description”: “dystopian”}}
]
}
}
)
for hit in response[‘hits’][‘hits’]:
print(f”Title: {hit[‘_source’][‘title’]}”)
This query will return books with the title “1984” and a description containing the word “dystopian”.
Step 4: Integrating with a Web Application
Now that you have a working Elasticsearch index and search functionality, you can integrate it into a web application. Here, we’ll give a basic example using Flask, a lightweight Python web framework.
Below is an example Flask application that allows users to search for books:
from flask import Flask, request, render_template
from elasticsearch import Elasticsearch
app = Flask(__name__)
es = Elasticsearch()
@app.route(“/”, methods=[“GET”, “POST”])
def search():
query = request.form.get(“query”, “”) if request.method == “POST” else “”
books = []
if query:
response = es.search(
index=”books”,
query={“match”: {“title”: query}}
)
books = [hit[“_source”] for hit in response[‘hits’][‘hits’]]
return render_template(“search.html”, books=books, query=query)
if __name__ == “__main__”:
app.run(debug=True)
Create a simple HTML form (in templates/search.html) where users can enter a search term:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Book Search</title>
</head>
<body>
<h1>Search Books</h1>
<form method=”POST”>
<input type=”text” name=”query” value=”{{ query }}” placeholder=”Enter book title”>
<button type=”submit”>Search</button>
</form>
<ul>
{% for book in books %}
<li>{{ book.title }} by {{ book.author }}</li>
{% endfor %}
</ul>
</body>
</html>
This app will allow users to search for books by title and display the search results dynamically.
Conclusion
Building a full-text search application with Elasticsearch 8.17 is a powerful way to leverage the full potential of your data. In this guide, we covered the process of setting up Elasticsearch, indexing documents, performing advanced searches, and integrating it into a web application using Python and Flask.
By combining Elasticsearch’s powerful search capabilities with a simple web interface, you can build fast, scalable, and intuitive search applications that deliver relevant results in real time.