How to Install and Use Elasticsearch for Full-Text Search
How to Install and Use Elasticsearch for Full-Text Search
Elasticsearch is a powerful, distributed search engine built on top of Apache Lucene. It’s designed for handling large amounts of data and provides a scalable, real-time search and analytics engine. In this article, we will explore how to install and use Elasticsearch for full-text search, covering everything from installation to basic query execution.
Prerequisites
Before installing Elasticsearch, ensure that you have the following prerequisites:
- A server running a recent version of Ubuntu or CentOS.
- At least 4 GB of RAM.
- Java Development Kit (JDK) version 11 or higher, as Elasticsearch requires Java.
Step 1: Installing Java
If you don’t have Java installed, you can install it by executing the following commands:
For Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk
For CentOS:
sudo yum install java-11-openjdk-devel
Verify the installation by checking the Java version:
java -version
Step 2: Installing Elasticsearch
Download Elasticsearch: Visit the official Elasticsearch downloads page to get the latest version. You can also use the command line to download it.
For Ubuntu:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.x.x-amd64.deb
For CentOS:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.x.x-x86_64.rpm
For Ubuntu:
sudo dpkg -i elasticsearch-8.x.x-amd64.deb
For CentOS:
sudo rpm -ivh elasticsearch-8.x.x-x86_64.rpm
- Configure Elasticsearch: After installation, you need to configure Elasticsearch. The main configuration file is located at /etc/elasticsearch/elasticsearch.yml. You can edit this file to set parameters like the cluster name, node name, network settings, and paths for data and logs.
sudo nano /etc/elasticsearch/elasticsearch.yml
Some essential settings to consider:
cluster.name: my-cluster
node.name: my-node
network.host: localhost
http.port: 9200
Enable and Start Elasticsearch: After configuring Elasticsearch, enable and start the service using the following commands:
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
Check the status to ensure it’s running:
sudo systemctl status elasticsearch
Test Elasticsearch Installation: You can test if Elasticsearch is running correctly by sending a GET request to the server:
curl -X GET “localhost:9200/”
If everything is set up correctly, you should see a JSON response with details about your Elasticsearch instance.
Step 3: Indexing Data
Once Elasticsearch is up and running, you can begin indexing data for full-text search. The first step is to create an index.
- Create an Index: Use the following command to create an index called my_index:
curl -X PUT “localhost:9200/my_index”
- Index Documents: You can index documents using the following command. Here, we will index a sample document.
curl -X POST “localhost:9200/my_index/_doc/1” -H ‘Content-Type: application/json’ -d’
{
“title”: “Elasticsearch Tutorial”,
“content”: “Elasticsearch is a powerful search engine.”
}
Step 4: Performing Full-Text Search
With documents indexed, you can now perform full-text searches. Elasticsearch provides a powerful query DSL (Domain Specific Language) to construct your queries.
Basic Full-Text Search: Use the following command to search for documents containing the word “powerful”:
curl -X GET “localhost:9200/my_index/_search” -H ‘Content-Type: application/json’ -d’
{
“query”: {
“match”: {
“content”: “powerful”
}
}
}
‘
- Advanced Querying: You can also use more advanced queries, such as bool queries, to combine multiple conditions.
curl -X GET “localhost:9200/my_index/_search” -H ‘Content-Type: application/json’ -d’
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “title”: “Elasticsearch” }},
{ “match”: { “content”: “search engine” }}
]
}
}
}
‘
Step 5: Monitoring and Managing Elasticsearch
To monitor your Elasticsearch cluster, you can use Kibana, a visualization tool from Elastic. Kibana allows you to analyze your data, create visualizations, and monitor the health of your Elasticsearch cluster. You can install Kibana by following the instructions on the Kibana installation page.
We appreciate your visit to our page! If you’re interested in exploring more articles about Linux systems and setting up MySQL replication, feel free to check out the links below.