FOSElastica + JMs serializer malformed data - php

I installed ElasticSearch 6 with their Docker image and everything work well.
Then I installed and configured FOSElastica like the doc said.
This is the following config of fos_elastica
fos_elastica:
clients:
default: { host: '%env(ELASTICSEARCH_HOST)%', port: '%env(ELASTICSEARCH_PORT)%' }
serializer:
serializer: jms_serializer
indexes:
app:
client: default
types:
user:
serializer:
groups: [elastica]
persistence:
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
And the model of my User entity to give the elastica serializer group to some field
AppBundle\Entity\User:
exclusion_policy: ALL
properties:
firstname:
expose: true
groups: [elastica, list, details]
lastname:
expose: true
groups: [elastica, list, details]
locale:
expose: true
groups: [elastica, details]
The serializer is working well for my API and is well configurated
jms_serializer:
metadata:
auto_detection: true
directories:
AppBundle:
namespace_prefix: 'AppBundle'
path: '%kernel.project_dir%/config/serializer'
When I'm trying to populate ElasticSearch I get, I think a bad serialization error
In Http.php line 181:
[Elastica\Exception\ResponseException]
Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]
I tried to set field typing using properties.type config in the fos_elastica configuration without success.
I tried with another entity and I get the same error.
I have already work with FOSElasticaBundle ~1year ago on the same project and population was working...
I didn't success to find where is the problem, if there is a JMSSerializer/FOSElastica bug or a misconfig
Did I miss something in the configuration ? Do you already had this issue ?

I had a similar problem. Here are the original elastic configs (which gave this error).
fos_elastica:
clients:
default: { host: '%env(ELASTICSEARCH_HOST)%', port: '%env(ELASTICSEARCH_PORT)%' }
serializer:
serializer: jms_serializer
indexes:
app:
finder: ~
types:
goods:
properties:
name: { type: text }
brand: { type: text }
cost: { type: float }
category:
type: "object"
properties:
id: ~
name: ~
goodsDescriptions:
type: "object"
properties:
id: ~
name: ~
description: ~
persistence:
driver: orm
model: App\Entity\Goods\Goods
provider: ~
finder: ~
repository: App\SearchRepository\GoodsRepository
However, I climbed inside the vendor foselastic for a long time and noticed that the data for adding it took based on jms_serializer, which was obvious, but at that time I thought otherwise. By removing this serialization and it was worked.
We have different settings, but maybe it will give you an idea.

To avoid this error just remove the
JSON_PRETTY_PRINT
from your JMS/Symfony Serializer config.

Related

make:entity command: [ERROR] Only annotation mapping is supported by make:entity

When I am trying to create a new doctrine entity in Symfony 5.2 (maker-bundle v1.30.0) I get:
$ php bin/console make:entity
Class name of the entity to create or update (e.g. GrumpyChef):
> test
created: src/Entity/Test.php
created: src/Repository/TestRepository.php
[ERROR] Only annotation mapping is supported by make:entity, but the
<info>App\Entity\Test</info> class uses a different format. If you
would like this command to generate the properties & getter/setter
methods, add your mapping configuration, and then re-run this command
with the <info>--regenerate</info> flag.
The files are generated and they look OK to me. I get the same for existing entities, except for the "created" lines. I have read about similar problems from years ago, but they were related to different namespaces which is not the case here, I am using the default namespace.
I believe the problem is related to an update, because of course it previously (not sure if it was 5.0 or 5.1) worked.
Is there any way to debug the MakerBundle or any idea how to solve this problem?
Thanks.
../config/packages/doctrine.yaml (I do not think I ever touched this):
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '5.7'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
$ php bin/console debug:config doctrine
Current configuration for extension with alias "doctrine"
=========================================================
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
override_url: false
driver: pdo_mysql
logging: true
profiling: true
profiling_collect_backtrace: false
profiling_collect_schema_errors: true
options: { }
mapping_types: { }
default_table_options: { }
slaves: { }
replicas: { }
shards: { }
types: { }
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: <<root_path>>/src/Entity
prefix: App\Entity
alias: App
mapping: true
query_cache_driver:
type: null
metadata_cache_driver:
type: null
result_cache_driver:
type: null
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
default_repository_class: Doctrine\ORM\EntityRepository
quote_strategy: doctrine.orm.quote_strategy.default
entity_listener_resolver: null
repository_factory: doctrine.orm.container_repository_factory
hydrators: { }
filters: { }
proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
proxy_namespace: Proxies
resolve_target_entities: { }
https://github.com/symfony/maker-bundle/issues/841
symfony/maker-bundle v1.30.1 fixes it.

Symfony Elasticsearch Integration (FOSElasticaBundle)

I am busy implementing elasticsearch for a customer project. Elasticsearch seems pretty powerful and has a neat API.
Integrated with FOSElasticaBundle i have the following configuration:
fos_elastica:
clients:
default:
host: localhost
port: 9200
indexes:
app:
client: default
settings:
index:
analysis:
filter:
synonym:
type: synonym
synonyms: ["katze, katz"]
analyzer:
synonyms:
tokenizer: standard
filter: [ lowercase, synonym ]
types:
article:
mappings:
name: { analyzer: synonyms }
shortDescription: ~
description: ~
categories:
type: "nested"
properties:
name: ~
persistence:
driver: orm
model: ShopBundle\Entity\Catalog\Article
provider: ~
finder: ~
listener: ~
When i search for "Katze" (German for cat), i get the expected results (Articles having the string "Katze" in its name. When i search for "Katz", i get 0 results.
This is a simpler configuration then i really use. I tried it with nGram tokenizer and other custom analyzers... I don't get rid of the feeling that elastic isn't indexing the field with analyzed content.
When i use the "_analyze" API endpoint, and i send a string and the analyzer name (one of my custom analyzers), the result (list of tokens) looks right!
Do i miss something?
Version of FOSElastica is 4.0.1 and elasticsearch server 5.6.3.

Symfony3 FOSElasticaBundle doc example not working

I am trying to set up FOSElasticaBundle.
Composer json entry:
"friendsofsymfony/elastica-bundle": "^3.2"
I was following official docs tutorial, but this line doesn't work:
$finder = $this->container->get('fos_elastica.finder.app.user');
error i get:
You have requested a non-existent service "fos_elastica.index.app.user".
Do you have any idea why ?
Things i checked:
AppKernel.php contains 'new FOS\ElasticaBundle\FOSElasticaBundle(),'
Profiler under 'Configuration' tab does not show 'FOSElastica' as enabled :(
config.yml:
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
indexes:
app:
types:
user:
mappings:
email: ~
persistence:
# the driver can be orm, mongodb, phpcr or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
Thanks in advance for any insights, guys :)
Turns out bundle didn't install properly.
"minimum-stability": "dev",
in composer.json on fresh installation worked

Symfony2 elasticsearch in production returns 500

In development everything works. When I switch to production and try to search I simply get 500 error. There are no errors written in logs so I dont understand what can be wrong?
When going to the http://localhost:9200/_plugin/head/. I see that the products are saved in index app_dev.
However there is not index related to app. Why is that? Why is it not creating indexes in prod?
config.yml:
fos_elastica:
default_manager: orm
clients:
default: { host: localhost, port: 9200 }
indexes:
app:
index_name: app_%kernel.environment%
types:
product:
mappings:
id:
type: integer
adminTitle:
type: string
base:
type: double
created_at:
type: date
persistence:
# the driver can be orm, mongodb, phpcr or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
model: Mp\ShopBundle\Entity\Product
provider: ~
listener:
immediate: ~
finder: ~
repository: Mp\ShopBundle\Repository\ProductRepository
elastica_to_model_transformer:
query_builder_method: findProductsBySearch
ignore_missing: true

Symfony2 - Fatal error with Bundles

I've got the error [Solution at the end of the question]
Fatal error: Class 'symblog\Blogger\BlogBundle\SymblogBundle' not found in
/var/www/Symfony/app/AppKernel.php on line 20
I founded the question How to install or integrate bundles in Symfony2, but the solutions given didn't help me, because I already did what is suggested there. I'm following the tutorial symblog.co.uk except that I created at
app/config/routing.yml
a
*.php resource
Thanks in advance!
I have to add that while registering the bundle by console I've got the error
The command was not able to configure everything automatically.
You must do the following changes manually.
And the instructions:
- Edit the app/autoload.php file and register the bundle
namespace at the top of the registerNamespaces() call:
'symblog\Blogger\BlogBundle' => '/var/www/Symfony/blog',
which I followed.
AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
//..
new symblog\Blogger\BlogBundle\SymblogBundle(),
);
/app/config/routing.yml
SymblogBundle:
resource: "#SymblogBundle/Resources/config/routing.php"
prefix: /
As requested: /app/config/config.yml
imports:
- { resource: parameters.ini }
- { resource: security.yml }
framework:
#esi: ~
#translator: { fallback: %locale% }
secret: %secret%
charset: UTF-8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enable_annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: %locale%
auto_start: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
# java: /usr/bin/java
filters:
cssrewrite: ~
# closure:
# jar: %kernel.root_dir%/java/compiler.jar
# yui_css:
# jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
jms_security_extra:
secure_controllers: true
secure_all_services: false
[Edit] The solution was very easy after the answer from #Clamidity that the bundles usually are located at src/Blogger/SymBlogBundle/BloggerSymBlogBundle.php
While the configuration using the console it saked about the location of the bundle and the default was /../src but I changed to /../blog. And of course it won't work, Symfony was looking into the wrong location. What I did was to move the folders inside /blog to /src and everything went fine.
There are a few things that it could be. I'll just cover anything I can think of.
Generally bundles are placed in the src folder. So the path to your bundle should look like this.
src/Blogger/SymBlogBundle/BloggerSymBlogBundle.php
(Notice that the bundle name follows the file name convention)
Inside of the BloggerSymBlogBundle.php make sure you have something similar to the following:
<?php
namespace Blogger\SymBlogBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerSymBlogBundle extends Bundle
{
}
(Notice that the same name convention is also followed here)
In your autoload.php the namespace that should be registered is the "Blogger" part of the name/path to your bundle. This is because the bundle itself resides in the Blogger folder:
'Blogger' => __DIR__.'/../src',
(Notice that the folder listed is the parent of the Blogger folder)
Now in the AppKernel.php register the bundle according the namespace your set up and registered:
new Blogger\SymBlogBundle\BloggerSymBlogBundle(),
*Note - Your resources and references to this bundle with the above configuration would be
BloggerSymBlogBundle
so your php routing would be called by using:
#BloggerSymBlogBundle/Resources/config/routing.php
There are different solution.
Clear the app/cache/(prod|dev) folder. When you have edited the Appkernel and autoload.
Fatal error: Class 'symblog\Blogger\BlogBundle\SymblogBundle' not found in
/var/www/Symfony/app/AppKernel.php on line 20
Here are now different Problems:
Look to the SymblogBundle.php in the Bundle Folder and look which namespace they use. Perhaps its really an uppercase ("S") like #kuba said.
The Class file is not in the folder or you have no rights to the folder that the interpreter can load the file.
I think your autoload.php is not correct.
'Avalanche' => __DIR__.'/../vendor/bundles',
This is the common way to register an Namespace. In your case it have to be
'Symlog' => '/var/www/Symfony/blog',
Here are the instructions from the "BloggerBundle" I hope its the correct one
There you can see the your autoload its not correct the Namespace is "Blogger".

Categories