This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to use Elasticsearch Codeigniter Library in your projects

Updated on  13th February

4 Min Read
Reading Time: 4 minutes

Elasticsearch is a popular text search engine that has recently become very popular with PHP developers. It is a popular choice for adding real-time data processing capabilities.

Using Elasticsearch Codeigniter Library
1. Setup the Environment
2. Handle Calls through CURL
3. Create the Index
4. Get the Status
5. Count Existing Indexes
6. Set Mapping for Index
7. Delete an Index
8. Make a Search Query
9. Make a Search Query with Result Sized Set
10. To Get Index Through ID
11. Get the Whole Server
12. Get Similar Indexes for a Index ID
13. Make a Search Query with Results Sized Set
14. Create a Query based on Similar Terms
15. Use Advanced Query
16. Final Words

Adding elasticsearch to CodeIgniter projects is simple enough.

You might also like: How To Host CodeIgniter On Cloud Using SSH

Setup the Environment

The first step of using elasticsearch in CodeIgniter projects is the setting up the environment on hosting for PHP. This involves constructing the right settings and setting up the configuration variable.

For this, use the following elasticsearch class:

Handle Calls through CURL

Once the elasticsearch is up and running, it is time to handle the calls through CURL. For this use a private function named call.

To handle call for every function by using curl

You might also like: How To Setup Elasticsearch With MySQL

Create the Index

Creating the indexes for optimizing the searches is an essential requirements for many projects. To create index with null validation mapping, use the following function:

Get the Status

Getting the status of the calls is a simple matter of using the following function:

 

Count Existing Indexes

Counting the existing indexes is simple. Just use the following code snippet:

Set Mapping for Index

 

Use the following function for setting up mapping for the index:

Delete an Index

Use the following code snippet to delete an index:

Make a Search Query

Setting up a simple search query is quite simple. Just use the following code snippet in the project’s code:

To setup an advanced search query that uses JSON data use the following code snippet:

Make a Search Query with Result Sized Set

Use the following code snippet for setting up a search query with a result sized set:

To Get Index Through ID

Get the index through the ID using the following snippet:

Get the Whole Server

The following code snippet gets the  entire server:

Get Similar Indexes for a Index ID

It is easy to get all similar indexes that matches a particular index ID. USe the following code snippet:

 

Make a Search Query with Results Sized Set

Creating a query  with results sized set is a simple matter of using the following code snippet:

Create a Query based on Similar Terms

Use the following code snippet to set up a query based on similar terms. Put the code in elasticsearch.php under application/libraries/

Use Advanced Query

Use the following code snippet for using advanced query. Put the snippet in the view file:

Final Words

Elasticsearch adds great power to CodeIgniter projects.  Remember that Elasticsearch is a huge library with a number of use cases. It is essential that the developers should study the documentation of the library for more to-the-point integration of Elasticsearch into CodeIgniter projects.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

THERE’S MORE TO READ.