Skip to content

Latest commit

 

History

History
234 lines (145 loc) · 3.88 KB

README.md

File metadata and controls

234 lines (145 loc) · 3.88 KB

Php Elasticsearch Example

You can add random data from home page and can see the added data from list page and search within it.

ElasticSearch Library İnsallation

composer require elasticsearch/elasticsearch

#How To Connect Elasticsearch

require 'vendor/autoload.php';

use Elastic\Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->setHosts(['elasticsearch:9200'])->build(); // elasticsearch:9200 is docker container name

How To Install Data In Elasticsearch

$params = [
    'index' => 'your_index_name',
    'id'    => 'my_id',
    'body'  => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

How To Get Data In Elasticsearch

$params = [
        'index' => 'your_index_name',
        'body' => [
            'query' => [
                'match' => [
                    'color' => 'red'
                ]
            ]
        ]
];

$response = $client->search($params);

if (isset($response['hits']['total']['value']) && $response['hits']['total']['value'] > 0) {
    $result['message'] = "data found!";
    $result['data'] = $response['hits']['hits'];
}
else {
    $result['status'] = false;
    $result['message'] = "data not found.";
}

print_r($result);

How to get multiple data in Elasticsearch

$params = [
    'index' => 'your_index_name',
    'body' => [
        'query' => [
            'multi_match' => [
                'query' => 'red',
                'fields' => ['name','color','brand']
            ]
        ]
    ]
];

$response = $client->search($params);

if (isset($response['hits']['total']['value']) && $response['hits']['total']['value'] > 0) {
    $result['message'] = "data found!";
    $result['data'] = $response['hits']['hits'];
}
else {
    $result['status'] = false;
    $result['message'] = "data not found.";
}

print_r($result);

How To Update Data In Elasticsearch

# Update data

Install Docker Compose And Elasticsearch

This is a simple PHP example of elasticsearch. It includes the following with docker configuration.

  • Php last version
  • Elasticsearch last version
  • Kibana last version
  • Mongo last version

You can run the project with the following command.

docker-compose up -d

You can access the project with the following link.

http://localhost:8001

You can access the elasticsearch with the following link.

http://localhost:9200

You can access the kibana with the following link.

http://localhost:5601

You can access the mongo with the following link.

http://localhost:8003

## Elastic Search Notes

Search Command:

curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
        {
            "query": {
                "match_all": {}
            }
        }

Insert Data Command:

  curl -X POST https://localhost:9200/index_name/_doc/1 -H 'Content-Type: application/json' -d'
 {
     "name": "test",
     "description": "test description"
 }

Update Data Command:

    curl -X POST https://localhost:9200/index_name/_doc/1 -H 'Content-Type: application/json' -d'
           {
               "doc": {
                   "name": "test",
                   "description": "test description"
               }
           }

Create Index Command:

       curl -X POST https://localhost:9200/index_name -H 'Content-Type: application/json' -d'
   {
       "mappings": {
           "properties": {
               "name": {
                   "type": "text"
               },
               "description": {
                   "type": "text"
               }
           }
       }
   }