I am using FosElasticaBundle in a Symfony project. I configured my mappings but I get exception "expected a simple value for field [_id] but found [START_OBJECT]]".
I'd like to see the actual JSON created by FosElasticaBundle so I can directly test it against my ElasticSearch server, and understand more about the exception.
According to FosElastica documentation, everything should be logged when debug mode is enabled (i.e. in DEV environment) but I can't see this happening; I only see Doctrine queries, but no JSON.
How can I dump the JSON created by FosElasticaBundle?
Update: mappings
# FOSElasticaBundle
fos_elastica:
clients:
default: { host: %elasticsearch_host%, port: %elasticsearch_port%, logger: false }
indexes:
app:
types:
user:
mappings:
name: ~
surname: ~
persistence:
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
I think you should only set your logger to true instead of false
fos_elastica:
clients:
default:
host: %elasticsearch_host%
port: %elasticsearch_port%
logger: true <---- set true here
...
Related
I get the following error when populating elastic search:
Root mapping definition has unsupported parameters: [product : {dynamic_date_formats=[], _meta={model=App\Entity\Product}, properties={name={type=text}, description={type=text}}}] [reason: Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [product : {
dynamic_date_formats=[], _meta={model=App\Entity\Product}, properties={name={type=text}, description={type=text}}}]]
Basically, I followed only the documentation ( https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md ) and changed userto product and the corresponding fields in my App\Entity\Product.
fos_elastica.yaml:
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
app:
types:
product:
properties:
name: ~
description: ~
persistence:
driver: orm
model: App\Entity\Product
provider: ~
finder: ~
Are you using Elasticsearch version 7? I had the same problem today and think that it is related to the Elasticsearch version you are using.
If I install Elasticsearch version 6, everything is working fine.
I'm not an Elasticsearch expert, but there are probably some breaking changes in version 7 with which FOSElasticaBundle is not yet compatible.
I have configured two connections to the database. One connection is called user and other is called client. This is the configuration in the config.yml file:
doctrine:
dbal:
default_connection: client
connections:
client:
driver: pdo_mysql
host: '%client_database_host%'
port: '%client_database_port%'
dbname: '%client_database_name%'
user: '%client_database_user%'
password: '%client_database_password%'
charset: UTF8
mapping_types:
enum: string
user:
driver: pdo_mysql
host: '%user_database_host%'
port: '%user_database_port%'
dbname: '%user_database_name%'
user: '%user_database_user%'
password: '%user_database_password%'
charset: UTF8
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: ~
entity_managers:
client:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ProjectModelBundle: ~
connection: client
user:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
BaseModelBundle: ~
ProjectModelBundle: ~
connection: user
But I am always getting the first entity manager no matter what. This is how I am using entity manager's in services:
# BASE
htec.project_model_bundle.repository.database.client_base:
class: Project\BaseModelBundle\Repository\Database\DatabaseRepository
arguments: ['#service_container', '#doctrine.orm.client_entity_manager', '#form.factory']
htec.project_model_bundle.repository.database.user_base:
class: Project\BaseModelBundle\Repository\Database\DatabaseRepository
arguments: ['#service_container', '#doctrine.orm.user_entity_manager', '#form.factory']
But no matter what I do, I always get the first entity manager that I have defined under orm->entity_managers settings. For example if configure orm like this:
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: ~
entity_managers:
client:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ProjectModelBundle: ~
connection: client
user:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
BaseModelBundle: ~
ProjectModelBundle: ~
connection: user
I will always get the client entity manager even if I supply '#doctrine.orm.user_entity_manager' as service argument.
If I configure orm like this:
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: ~
entity_managers:
user:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
BaseModelBundle: ~
ProjectModelBundle: ~
connection: user
client:
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ProjectModelBundle: ~
connection: client
I will always get the user entity manager even if I supply '#doctrine.orm.client_entity_manager' as service argument.
What am I doing wrong here?
Recently I had a similar issue. The comment of #dbrumann gave me a good clue for resolve it. I used the next approach:
If you use the console command debug:autowiring you will see there is a service aliased as doctrine.
Doctrine\Common\Persistence\ManagerRegistry alias to doctrine
You can get access to that service in your target class by type-hinting, put the name of the service class (or interface) as argument in the constructor of your target class.
Now you can access all the methods of the service in your target class, by using the method getManager() you will get any of your managers:
use Doctrine\Common\Persistence\ManagerRegistry;
$protected $managerRegistry;
public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}
public function foo()
{
// asuming your managers names are default and second
$firstManager = $this->managerRegistry->getManager('default');
$secondManager = $this->managerRegistry->getManager('second');
}
So you defined a service named DatabaseRepository that exists in two variations. At one point it receives the 'client' entity manager, and at another point it receives 'user' entity manager?
I think it always returns you the 'client' manager most probably because that is the first time the service has been defined for the service container. It does not matter what you define it afterwards in the same .yml code.
Now, there is a big "why" rhetoric question for you. Why didn't you define two service classes, each for particular manager? If you really had to do it that way, that indicates some serious design problems in the remaining code of your Project.
Another suggestion: Do name your classes somewhat more descriptive. An entity manager and repository class certainly do something about a database. Naming classes, properties, variables in a proper way I find to be one of the most challenging part of the work, but it makes our lives easier.
Btw. Avoid passing entire container as service parameter:
arguments: ['#service_container',
because it's too expensive resources wise.
I have installed FOSElasticaBundle "friendsofsymfony/elastica-bundle": "^3.2"
(with symfony 2.8.8) and tried to define this simple configuration
fos_elastica:
clients:
default: { host: %elastic_host%, port: %elastic_port% }
indexes:
project:
types:
item:
mappings:
id: { type: integer }
itemno: { type: string }
persistence:
driver: propel
model: Model\Item\Item
provider: ~
Unfortunately I'm getting following error
The parent definition "fos_elastica.listener.prototype.propel" defined
for definition "fos_elastica.listener.project.item" does not
exist.
As I read through the documentation, there is no "listener" available for propel - that's why I'm a little bit confused about the error message. I already tried to define the missing definition, but with no result.
Is it necessary to define any class/service as provider for the type? In the documentation I couldn't find anything about that?
Thanks.
I am trying to setup a parent child relationship and I can't seem to get the mapping config right. I'm using Symfony2.5, FOSElastica 3.0.9 and Elasticsearch 1.4.4
Here is the relevent section from my mapping:
# fos elastica
fos_elastica:
clients:
default: { host: 127.0.0.1, port: 9200 }
serializer:
callback_class: FOS\ElasticaBundle\Serializer\Callback
serializer: serializer
indexes:
index:
index_name: index_%kernel.environment%
client: default
types:
company:
mappings:
id:
type: "long"
company_name:
type: "string"
fields:
raw:
type: "string"
index: "analyzed"
index_analyzer: "sortable"
persistence:
elastica_to_model_transformer:
ignore_missing: true
driver: orm # orm, mongodb, propel are available
model: Alpha\RMSBundle\Entity\Company
provider: ~
finder: ~
listener: ~
serializer:
groups: [company]
job:
mappings:
id:
type: "long"
company:
type: "object"
properties:
id:
type: "long"
_parent:
type: company
property: company
identifier: id
#_routing:
# required: true
# path: company
persistence:
elastica_to_model_transformer:
ignore_missing: true
driver: orm # orm, mongodb, propel are available
model: Alpha\RMSBundle\Entity\JobOpening
provider: ~
finder: ~
listener: ~
serializer:
groups: [job]
First I populate company, but when I try to populate job I get the following error:
[Elastica\Exception\ResponseException]
RoutingMissingException[routing is required for [index_v2]/[job]/[1]]
I have tried specifying the routing which is commented out above, but that didn't set the relationship either, I have taken the routing out as it is not mentioned in the docs.
Can anyone see where I am going wrong?
There is no problem with the mapping above, and _routing is not required. However FOSElastica will not populate the mapping with a _parent set.
However, as the docs says you need to to use setParent on the Document. In order to do this I need to setup a custom ModelToElasticaTransformer. This can be done by extending the ModelToElasticaAutoTransformer provided from the bundle in a class Alpha\RMSBundle\Transformer\JobToElasticaTransformer
In the transform() function I inserted a line:
$document->setParent($object->getCompany()->getId());
just above
return $document
Then you declare the service:
alpha.transformers.model.job:
class: Alpha\RMSBundle\Transformer\JobToElasticaTransformer
calls:
- [ setPropertyAccessor, ['#fos_elastica.property_accessor'] ]
Finally add the following in the mapping for the type in the persistence section:
model_to_elastica_transformer:
service: alpha.transformers.model.job
Now you can use populate as normal, as long as you remember to populate the parent class first
The bundle explains this very clearly in it's documentation
Note that to create a document with a parent, you need to call
setParent on the document rather than setting a _parent field. If you
do this wrong, you will see a RoutingMissingException as Elasticsearch
does not know where to store a document that should have a parent but
does not specify it.
I'm using symfony2 snc-redis bundle for caching.
On my server, redis has been installed and working correctly.
My problem is; when i try to clear or flush db with redis, all sites on my server that using redis, crashes. Giving internal server error because of prod env.
I'v tried to change redis configuration ports in my config.yml for every single site on my server but i think didn't work.
My sample snc-redis configuration:
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
logging: %kernel.debug%
cache:
type: predis
alias: cache
dsn: redis://localhost/1
logging: true
cluster:
type: predis
alias: cluster
dsn:
- redis://127.0.0.1/5
- redis://127.0.0.2/6
- redis://pw#/var/run/redis/redis-1.sock/7
- redis://127.0.0.1:6379/8
options:
profile: 2.4
connection_timeout: 10
connection_persistent: true
read_write_timeout: 30
iterable_multibulk: false
throw_errors: true
cluster: Snc\RedisBundle\Client\Predis\Connection\PredisCluster
monolog:
type: predis
alias: monolog
dsn: redis://localhost/1
logging: false
options:
connection_persistent: true
session:
client: default
prefix: foo
use_as_default: true
doctrine:
metadata_cache:
client: cache
entity_manager: default
document_manager: default
result_cache:
client: cache
entity_manager: [default, read]
document_manager: [default, slave1, slave2]
namespace: "dcrc:"
query_cache:
client: cache
entity_manager: default
monolog:
client: monolog
key: monolog
swiftmailer:
client: default
key: swiftmailer
monolog:
handlers:
main:
type: service
id: monolog.handler.redis
level: debug
What i'm doing wrong? How can i get it work correctly and will not cause crashing.
My Redis Bundle for Symfon2:
Snc\RedisBundle\SncRedisBundle()
https://github.com/snc/SncRedisBundle
You can define prefix for each site like this:
snc_redis:
clients:
default:
dsn: "redis://localhost:6379"
options:
prefix : "site_name"
type: phpredis
alias: default
logging: %kernel.debug%
Note: You must to be considered to put this prefix to all clients ;)
Did you try to change client alias for every site ?