How To Use Elasticsearch Codeigniter Library In Your Projects

How To Use Elasticsearch Codeigniter Library In Your Projects

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.
codeigniter elasticsearch integration
Adding elasticsearch to CodeIgniter projects is simple enough.

Setup the Environment

The first step of using elasticsearch in CodeIgniter projects is the setting up the environment. 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

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.

Comments