Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FosElasticaBundle: multiple types in one index

I need to build a multiple-entity search.

I don't want every T1, then every T2.

When I use fos:lastica:populate, I get an error:

Rejecting mapping update to [search_dev] as the final mapping would have more than 1 type: [t1, t2]

My mapping:

fos_elastica:
   clients:
       default:
           host: %elastic_host%
           port: %elastic_port%
   indexes:
       search:
           finder: ~
           client: default
           index_name: search_%kernel.environment%
           types:
               t1:
                   indexable_callback: 'getEnabled'
                   properties:
                       id:
                          type: integer
                       name: ~
                   persistence:
                       driver: orm
                       model: AppBundle\Entity\T1
                       finder: ~
                       listener:
                          logger: true
                       elastica_to_model_transformer:
                          ignore_missing: true
               t2:
                   indexable_callback: 'getEnabled'
                   properties:
                       id:
                          type: integer
                       name: ~
                   persistence:
                       driver: orm
                       model: AppBundle\Entity\T2
                       finder: ~
                       listener:
                          logger: true
                       elastica_to_model_transformer:
                          ignore_missing: true

My service:

$search = $this->indexManager->getIndex('search')->createSearch();
$search->addType('t1');
$search->addType('t2');
$resultSet = $search->search($query);

$results = $this->modelTransformer->transform($resultSet->getResults());

Did I miss something? Can I map 2 types in 1 index?

like image 790
Sancho Avatar asked Nov 19 '17 16:11

Sancho


People also ask

Can an index have multiple types?

You can't. One type per index only.

Can we have multiple type in single Elasticsearch index?

From Elasticsearch version 6.0 by default index doesn't allow multiple types per index. The better option is to always have one document type per index.

Can Elasticsearch index have multiple mappings?

No, if you want to use a single index, you would need to define a single mapping that combines the fields of each document type. A better way might be to define separate indices on the same cluster for each document type.

How do you create multiple indices in Elasticsearch?

Elasticsearch provides bulk api but it only supports index , create , delete and update operations over already created indexes. We cannot create multiple new indexes at once.


1 Answers

I was looking for a way to get products and categories and got the same error. It worked when I moved it around and made multiple indexes:

fos_elastica:
  clients:
    default: { host: localhost, port: 9200 }
  indexes:
    products:
      types:
        product:
          properties:
            ...
          persistence:
            ...
    categories:
      types:
        category:
          properties:
            ...
          persistence:
            ...
like image 144
Martijn Avatar answered Sep 22 '22 02:09

Martijn