PHP symfony FOSElastica

 

Visit project on github: https://github.com/FriendsOfSymfony/FOSElasticaBundle

FOSElastica works fine by default but it has a problem because it works asynchronous.

Try to imagine:

  • PHP symfony server (backend)
  • React (frontend)
  • MySql
  • ElasticSearch

For example (backend):

You made a endpoint like /api/samples [POST] – it adds new entity of type Sample

You made a endpoint like /api/samples [GET] – it lists all entities of type Sample

example ElasticSearch configuration:

fos_elastica:
2    default_manager: orm
3    clients:
4        default: { host: 'localhost', port: '9200' }
5    indexes:
6        sample:
7            properties:
8                id:
9                    type: integer
10                name:
11                    type: keyword
12            persistence:
13                model: ....\Sample
14                repository: ....\SampleElasticSearchRepository
15                finder: ~
16                provider:

See the problem:

  1. You send POST request to the php server
  2. When you receive response then you need to fetch all entities
  3. Your added entity isn’t in list

Problem description: If you add a entity in database this plugin send asynchronous request to ElasticSearch server and your php server doesn’t wait a response from ElasticSearch server.

If you need to make ElasticSearch server work synchronous that is not possible, but you can make that your PHP server waits response from ElasticSearch server and it works fine and it works synchronous.

Solution:

You only need to change FOSElastica configuration like this (Please to see line 13 and line 14):

fos_elastica:
2    default_manager: orm
3    clients:
4        default: { host: 'localhost', port: '9200' }
5    indexes:
6        sample:
7            properties:
8                id:
9                    type: integer
10                name:
11                    type: keyword
12            persistence:
13                persister:
14                    refresh: wait_for
15                model: ....\Sample
16                repository: ....\SampleElasticSearchRepository
17                finder: ~
18                provider: ~

For the end:

Visit ElasticSearch documentation for refresh option: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

Visit Github issue for FOSElastica: https://github.com/FriendsOfSymfony/FOSElasticaBundle/issues/1516

Visit source code for FOSElastica (Please to see lines 363 to 373) https://github.com/jvasseur/FOSElasticaBundle/blob/4075aa439febb0280262c7e76fe33bbd4379cf86/src/DependencyInjection/Configuration.php

ElasticSearch Commands

Visit site for all examples: https://www.bmc.com/blogs/elasticsearch-commands/

You need to install tool

https://curl.se/

delete index

curl -X DELETE 'http://localhost:9200/samples

list all indexes

curl -X GET 'http://localhost:9200/_cat/indices?v'

list all docs in index

curl -X GET 'http://localhost:9200/sample/_search'

query using URL parameters

Here we use Lucene query format to write q=school:Harvard.
curl -X GET http://localhost:9200/samples/_search?q=school:Harvar

list index mapping

All Elasticsearch fields are indexes. So this lists all fields and their types in an index.

curl -X GET http://localhost:9200/samples

Add Data

curl -XPUT --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/1 -d '{
  "school" : "Harvard"			
}'

Update Doc

Here is how to add fields to an existing document. First we create a new one. Then we update it.

curl -XPUT --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/2 -d '
  {
  "school": "Clemson"
  }'
curl -XPOST --header 'Content-Type: application/json' http://localhost:9200/samples/_doc/2/_update -d '{
  "doc" : {
    "students": 50000}
  }'

backup index

curl -XPOST --header 'Content-Type: application/json' http://localhost:9200/_reindex -d '{
  "source": {
    "index": "samples"
  },
  "dest": {
    "index": "samples_backup"
  }
}'

Bulk load data in JSON format

export pwd="elastic:"
curl --user $pwd  -H 'Content-Type: application/x-ndjson' -XPOST 'https://58571402f5464923883e7be42a037917.eu-central-1.aws.cloud.es.io:9243/0/_bulk?pretty' --data-binary @<file>