ElasticSearch library

melo lobo Posted in General Discussion 11 months ago

Hello PHP Lions,
Can OSSN support the integration of the ElasticSearch library which allows a more typo-tolerant search? thx

Replies
ma Melo lobo Replied 11 months ago

yes Arslan it's already done, it's by the error_log file that I was able to detect where the problem is. thank you for your help

Indonesian Arsalan Shah Replied 11 months ago

In future when you integerate something login into admin first and you will see actual errors instead of code like

#1703710134|MDER9NBQIB29BFS5E37L8GUK5A

However for such a codes the exact error can be looked in error log for timestamp example 1703710134

ma Melo lobo Replied 11 months ago

Thank you Arsalan :D

Indonesian Arsalan Shah Replied 11 months ago

Glad that you found the issue!

ma Melo lobo Replied 11 months ago

For people who will encounter the same problem as me in the future

Elastic Search recently changed the location of ClientBuilder in their package. You'll have to update your use statement.

Change this:

use Elasticsearch\ClientBuilder;

to this:

use Elastic\Elasticsearch\ClientBuilder;
ma Melo lobo Replied 11 months ago

I try to integrate ElasticSearch but I receive this error

#1703710134|MDER9NBQIB29BFS5E37L8GUK5A

the problem is in

$client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();

here is my test code:

require __DIR__ . '/vendor/autoload.php';
 // Using the namespace for the ClientBuilder class
use Elasticsearch\ClientBuilder;

// Configuring the connection to Elasticsearch
$hosts = [
    'localhost:9200' // Address and port of your Elasticsearch cluster
];

try {
    $client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();
} catch (\Exception $e) {
    echo "Error creating Elasticsearch client: ". $e->getMessage();
    // You can add other error handling actions if necessary
    exit;
}

// Name of the index we are going to create (in this example, "professions")
$index = 'professions';

// Definition of the mapping (structure) of documents in the index
$mapping = [
    'index' => $index,
    'body' => [
        'mappings' => [
            'properties' => [
                'profession' => ['type' => 'text'],
                // Add other properties of your profession here
            ]
        ]
    ]
    ];
...... the rest of the code

In principle this should display the error message

try {
    $client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();
} catch (\Exception $e) {
    echo "Error creating Elasticsearch client: ". $e->getMessage();
    // You can add other error handling actions if necessary
    exit;
}

but it doesn't work.

My ElasticSearch Server is runing correctly

{
  "name" : "DESKTOP-4CFMHET",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qPdEbqZ0SxiAVKDh8yd5pQ",
  "version" : {
    "number" : "8.11.3",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "64cf052f3b56b1fd4449f5454cb88aca7e739d9a",
    "build_date" : "2023-12-08T11:33:53.634979452Z",
    "build_snapshot" : false,
    "lucene_version" : "9.8.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}
Indonesian Arsalan Shah Replied 11 months ago

You need to read their documentation and then try to integrate into OSSN, ElasticSearch is independent library didn't depend on any platform and can be integrated into any platform if you follow their docs.