WPSolr logo
Search
Close this search box.

Table of contents :

Should you Use Elasticsearch PHP Client With OpenSearch? Opensearch PHP guide

integrate your Opensearch server using the PHP client

Table of contents :

Historically, you had to use the Elasticsearch PHP client, but as of november 2021 that is not the case anymore. The official Opensearch PHP client is now available.

 

As an official opensearch partner, we just had to explain the benefits of the Opensearch PHP client and provide a comprehensive guide on setting up a PHP application with it.

 

Opensearch is an open-source search engine designed for scalable and high available applications. It offers features such as full-text search, faceted search, vector search and many more.

 

Why use the Opensearch PHP client

 

Using the OpenSearch PHP client offers several advantages over using cURL directly. First, the PHP client provides a more simple and user-friendly interface for interacting with the OpenSearch API, reducing the amount of boilerplate code needed to perform common tasks.

It also includes more features that do not have to be created manually and is regurarly updated. With a simple installation, their are not many reasons to not use the PHP client to connect your web application to your Opensearch server.

Overall, using the OpenSearch PHP client can simplify development, improve maintainability, and enhance the overall developer experience when working with OpenSearch.

 

Build your search with the Opensearch PHP Client

 

Install Opensearch

 

To install Opensearch locally, you can follow the guide we have written on this subject.

 

Or you could setup an Opensearch cloud instance using AWS.

 

Download and parse the example dataset (if you have your own data you can skip this step)

 

For the purpose of this guide, we will use an existing dataset containing TechCrunch posts.

 

You can download it from this page and it will be stored in your download directory as “archive.zip”.

 

You can then create the directory containing the dataset and unzip the CSV file into it :

mkdir dataset
unzip ~/Downloads/archive.zip -d dataset/

 

In a php file (in the project directory), you can add the following code :

<?php

$dataset_file_path = 'dataset/techcrunch_posts.csv';
$number_of_lines = 20;
$data = parse_csv($dataset_file_path, $number_of_lines);

function parse_csv(string $file_path, int $number_of_lines) {

    
    $parsed_data = [];
    $property_names = [];
    $line = 0;
    $num_properties = 0;
    
    // Open the CSV file
    $file_contents = fopen($file_path, "r");
    
    // Parse the lines of the CSV file
    while (($data = fgetcsv($file_contents, null, ",")) !== FALSE) {
        
        /**
         * If first line, get the property names and store them in an array
         */
        if (0 === $line){

            $num_properties = count($data);
            
            for ($i=0; $i < $num_properties; $i++) {

                // If property name is 'Id', get it's position.
                if ('id' === strtolower($data[$i])){
                    $id_position = $i;
                }
                // Else add the property name to the array
                $property_names[] = $data[$i];   
            }
        }
        /**
         * Else get the property values and store them in the $parsed_data array
         */
        else {
    
            // If an id property exists in the CSV file, set it's value. 
            if (isset($id_position)){
                $id = $data[$id_position];
            }
            // If not set the id value is the current line
            else {
                $id = $line;
            }
            
            // Store all the values in the parsed_data array
            for ($i=0; $i < $num_properties; $i++) {
                // If is not the id
                if (!isset($id_position) || $i !== $id_position){
                    $parsed_data[$id][$property_names[$i]] = $data[$i];
                }
            }

            /**
             * If reached line limit, end the parsing
             */
            if ($line >= $number_of_lines){
                break;
            }
    
            $id = null;
        }
    
        $line++;
    }

    return $parsed_data;
}

This program will parse the ‘techcrunch_posts.csv’ file and return an array containing the first 20 items to import into to Recombee with all their respective properties. If you wish to change which dataset to import, you can modify the value of $dataset_file_path; if you whish to change the number of lines to import, you can modify the value $number_of_lines.

 

You can view the parsed data by adding the following lines :

$number_of_lines = 1;

$data = parse_csv($dataset_file_path, $number_of_lines);
echo json_encode($data);

 

It will return the following JSON :

{
   
   "1401293":{
      "authors":"Dale Chang",
      "category":"Startups",
      "content":"\n\nTech investing isn\u2019t what it used to be \u2014 even compared to six months ago.\nInvestors are applying greater scrutiny to deals.",
      "date":"2016-10-15",
      "img_src":"https:\/\/tctechcrunch2011.files.wordpress.com\/2015\/04\/shutterstock_94007068.jpg?w=738",
      "section":"startups\/",
      "tags":"data",
      "title":"How startups can use data to grow smarter",
      "topics":"",
      "url":"https:\/\/techcrunch.com\/2016\/10\/15\/how-startups-can-use-data-to-grow-smarter\/"
   }
}

 

Install the Opensearch PHP client

 

First, you need to ensure that composer (PHP package manager) is installed correctly. Learn more about it here.

 

In a composer.json file in the project directory :

{
  "require": {
    "opensearch-project/opensearch-php": "2.2.*"
  }
}

 

Then you can install the Opensearch PHP client using the following command :

composer install

 

Connect to your Opensearch installation using the PHP client

 

To load the Opensearch PHP client, you should add the following file to your PHP script :

require __DIR__ . '/vendor/autoload.php';

 

Now you can connect to the Opensearch server using the newly loaded PHP client :

 

Connect to your Opensearch server using HTTP

 

If you do not wish to establish a secure connection, set the “setSSLVerification” value to false :

$client = (new \OpenSearch\ClientBuilder())->setHosts(['https://localhost:9200'])
    ->setBasicAuthentication('admin', 'password')
    ->setSSLVerification(false)
    ->build();

 

Connect to your Opensearch server using HTTPS (with self-signed certificate)

 

If you wish to use HTTPS, you can set the “setSSLVerification” value to your certificate path :

$client = (new \OpenSearch\ClientBuilder())->setHosts(['https://localhost:9200'])
    ->setBasicAuthentication('admin', 'password')
    ->setSSLVerification('opensearch_certs/opensearch-server-1.pem')
    ->build();

 

You can check that a connection was established by adding the following line to your PHP script :

echo json_encode($client->info());

 

If you have not yet created a certificate, it is detailled in our Opensearch installation guide.

 

Create an index

 

To create a new “techcrunch” index :

create_index($client, 'techcrunch');

function create_index(object $client, string $index_name){
    $client->indices()->create([
        'index' => $index_name,
        'body' => [
            'settings' => [
                'index' => [
                    'number_of_shards' => 4
                ]
            ]
        ]
    ]);
}

 

In the opensearch dashboard, “Index management” > “Indices”, new index is now visible : 

Index has been created in Opensearch dashboard

 

Index the dataset documents

 

To index the dataset documents, add the following lines to your PHP file :

index_dataset_documents($client, 'techcrunch', $data);

function index_dataset_documents(object $client, string $index_name, array $data){

    foreach ($data as $id => $document_data){
        $client->create([
            'index' => $index_name,
            'id' => $id,
            'body' => $document_data
        ]);
    }
}

 

The documents have now been added to your index.

To view them, in the opensearch dashboard “Dev Tools”:

In opensearch dev tools : "GET techcrunch/_search { "query": { "match_all": {} } }"

 

Send queries

 

Search for the documents in the ‘techcrunch’ index containing the word ‘Steve’ : 

echo json_encode(search_for_documents($client, 'techcrunch', 'steve'));

function search_for_documents(object $client, string $index_name, string $query){
    return $client->search([
        'index' => $index_name,
        '_source' => ['id', 'title'],
        'body' => [
            'size' => 10,
            'query' => [
                'multi_match' => [
                    'query' => $query,
                    'fields' => ['title^1.5', 'content', 'category^2', 'authors^2']
                ]
            ]
        ]
    ]);
}

 

This will return the ids and titles of the 10 most relevant (high scoring) documents containing the word “steve” in their title, content, categories and authors fields. As you can see, some boosters have been applied for certain field values.

 

Integrate Opensearch easily into WordPress & Woocommerce using WPSolr

 

WPSolr is a search & recommendations WordPress plugin. It can work with blog websites, Woocommerce and even buddypress. WPSolr is compatible with many search and recommendations engines (view our comparison) which ensures fast and relevant results.

At WPSOLR, we’re using the latest Opesnearch PHP client with the OpenSearch server. Our tests show no issue till now. And the PHP OpenSearch php client is a bit young.

Elasticsearch PHP client:

OpenSearch PHP client:

Related posts ... not powered by WPSOLR 😊