PHP ElasticSearch如何在索引记录之前设置映射? [英] PHP ElasticSearch how to set mapping before indexing records?

查看:178
本文介绍了PHP ElasticSearch如何在索引记录之前设置映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel和 elasticsearch-php 来索引和存储数据到弹性,我的问题是弹性支持使用动态映射,但我需要设置我的自定义映射。如何从我的映射中使用?

I'm using laravel and elasticsearch-php to index and store data to elastic, my problem is that elastisearch uses from dynamic mapping but I need to set my custom mapping. How can I use from my mapping?

Bellow是我的代码:

Bellow is my code:

$client = \Elasticsearch\ClientBuilder::create()->build();

        $mappingData = array(
            'index' => 'promote_kmp',
            'body' => array(
                'mappings' => $resource->getMappingProperties()
            )
        );
        $client->indices()->create($mappingData);

        $params = [
            'type' => 'resources',
            'id' => uniqid(),
            'body' => [
                'id' => $resource->id,
                'name' => $resource->name,
                'display_name_en' => $resource->display_name_en,
                'display_name_pr' => $resource->display_name_pr,
                'display_name_pa' => $resource->display_name_pa,
                'table_name'      => $resource->table_name,
                'model_name'      => $resource->model_name,
                'in_sidemenu'     => $resource->in_sidemenu,
                'icon_class'      => $resource->icon_class,
                'created_at'      => $resource->created_at,
                'created_by'      => $user,
            ]
        ];

        //$response = $client->indices()->create($resource->getMappingProperties());

        $client->index($params);

$ resource-> getMappingProperties()获取我在模型中设置的映射数组。
但是当我想索引一条记录时,它说 IndexAlreadyExistsException [[promote_kmp]已经存在] 。当我想搜索日期字段搜索没有正常工作时出现这个问题,我猜这种映射不正确。

$resource->getMappingProperties() get the mapping array I have set in model. but when I want to index a record it says IndexAlreadyExistsException[[promote_kmp] already exists]. This question arise when I want to search for date field searching is not properly working and I guess that mapping is not true.

推荐答案

正如我在评论中所说的那样。

As I was saying in comments.

每次要查询时,代码都会执行索引的创建。
但索引必须只创建一次。

The code is executing the creation of index every time you want to query. But the index must be created only once.

所以它应该像数据库的迁移一样工作。

So it should work like the migration for the DB's.

我能给你的唯一想法是创建一个命令来生成索引。
这样你就可以

The only idea I can give you is to make a command to generate the index. So that you could just

$ artisan elasticsearch:generate <index>






关于代码,我做了什么我们的情况下,使索引具有注入类型的方式,以及将它们创建为elasticsearch的方法:


About the code, what I've done for our case, made the index with a way to inject the types, plus a way to create them into elasticsearch:

interface Index {

  /**
   * @param Type[] $types Index types (resources)
   */
  function setTypes(array $types);

  /**
   * Generate the index and the types into the elasticsearch
   */
  function create();

}

然后类型应该生成映射和类型名称( as /< index> /< type> ,如:

Then the types should generate the mappings and the type name (as /<index>/<type>, like:

interface Type {

  /**
   * @return string The type name
   */
   function getName();

  /**
   * @return array The type mapping 
   */
  function getMapping();

}

所以(某处),你会创建这个类(这可能更好):

So (somewhere), you would create the class (this could be better):

$myIndex = new MyIndex();
$myIndex->setTypes([
  new MyFirstType(),
  new MySecondType(),
  //...
]);
$myIndex->create();

我希望这会有所帮助。

这篇关于PHP ElasticSearch如何在索引记录之前设置映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆