Elasticsearch is an important component of Xola yet very few people know how to work with it.
In this presentation you'll learn:
What it doesn't have:
FOSElasticabundle This is a Symfony bundle for integrating a PHP Elasticsearch library
XolaElasticsearchProxyBundle An authentication and authorization layer for Xola to ensure
no seller can query another seller's ES data.elasticsearch:9200 are made to run search queries
/api/elasticsearchXolaElasticsearchProxyBundle to send it to ElasticsearchFOS = Friends of Symfony. It's the heart and soul of Xola's ES integration. This bundle will do the following:
onKernelTerminate when a order/transaction is
created or updatedfos:elastica:populate which allows us to copy data from MongoDB to
Elasticsearchbin/console fos:elastica:reset --index=order A reset - Wipe out all data from the
order index and copy over mappings from elasticsearch.yml to ES.bin/console xola:elastica:populate --index=order Use smart filters to populate data from
MongoDB to Elasticsearch. This command is written is written by Xola and overrides FOS bundle's
fos:elastica:populate - don't touch that command.bin/console xola:elastica:syncUpdated --index=transaction --hours=2 Sync transactions
that have been updated in last 2 hoursA security focused bundle for Xola. It will:
A simple POST or PUT call
curl -X PUT -H "Content-type: application/json" http://localhost:9200/order -d '
{
"mappings": {
"order": {
"properties": {
"customerName": {
"type": "keyword"
},
"amount": {
"type": "float"
}
}
}
}
}'
A simple POST or PUT call
curl -X PUT -H "Content-type: application/json" http://localhost:9200/order -d '{
"customerName": "Rushi Vishavadia",
"amount": 100.23
}'
FOSElasticaBundle will automatically convert the Order model into JSON and make the HTTP call
to ESelasticsearch.ymlgetFieldName()
curl -X GET localhost:9200/order/order/some_mongo_id
Once you install Elasticsearch here's what you need todo
# Create all four (order, transaction, gift & store_credit) indexes & copy the mappings
bin/console fos:elastica:reset
# Send data from MongoDB
bin/console xola:elastica:populate --seller 4f104661536e86b23d000000
# OR
bin/console xola:elastica:populate --createdAt 2020-10-10
# OR
bin/console xola:elastica:populate --createdAt 2020-10-10 --seller 4f104661536e86b23d000000
Configure once, then leave it alone
parameters:
elasticsearch_protocol: "http"
elasticsearch_host: "localhost"
elasticsearch_port: 9200
elasticsearch_username: ""
elasticsearch_password: ""
elasticsearch_index_suffix: "" # Have indexes like 'transaction_rushi' instead of just 'transaction'
elasticsearch_indexes: [ 'transaction%elasticsearch_index_suffix%', 'order%elasticsearch_index_suffix%', 'gift%elasticsearch_index_suffix%', 'store_credit%elasticsearch_index_suffix%' ]
elasticsearch_api_path: "/api/elasticsearch"
# Silently log Elastica exceptions
fos_elastica.client.class: Xola\CommonBundle\Client\ElasticaClient
# This is the XolaElasticsearchProxyBundle. It serves as a proxy to our ES server
xola_elasticsearch_proxy:
roles_skip_auth_filter: [ 'ROLE_ADMIN' ]
client:
protocol: "%elasticsearch_protocol%"
host: "%elasticsearch_username%:%elasticsearch_password%@%elasticsearch_host%"
port: "%elasticsearch_port%"
indexes: "%elasticsearch_indexes%"
# This configuration is used by the FOSElasticaBundle to sync data betwen MongoDB & Elasticsearch
fos_elastica:
clients:
default:
connections:
- url: "%elasticsearch_protocol%://%elasticsearch_username%:%elasticsearch_password%@%elasticsearch_host%:%elasticsearch_port%"
default_manager: mongodb
indexes:
transaction:
index_name: 'transaction%elasticsearch_index_suffix%'
finder: ~
types:
transaction:
dynamic: strict
persistence:
driver: mongodb
model: Xola\PaymentBundle\Document\Transaction
provider:
service: Xola\CommonBundle\Service\XolaElasticaPopulatePagerProvider
listener:
defer: true
logger: true
finder: ~
properties:
id: { type: keyword }
amount: { type: float }
realizedAt: { type: date }
earning: { type: float }
hits section of the response shows you all the documents that were matched by the filter section abovesize attribute determines how many results are shown in the `hits` sectionLet's walk through a simple real life query
Welcome to a world of pain 😰
Just kidding 😏
size atttribute to see if your filters work properlycurl -i http://localhost:9200Questions?