二、Es索引

Elasticearch索引

php创建索引语法:
\Elasticsearch\ClientBuilder::create()->build()-indices()->create($params);

1、创建索引

/**
 * 创建常规索引配置参数
 * @return array
 */
public static function create()
{
    return [
        'index' => 'books',
    ];
}

** 2、创建索引、指定类型和映射**

 /**
 * 创建索引、指定类型和映射
 * 建议一个索引对应一个类型
 * @return array
 */
public static function CreateIndexTypeMapping()
{
    return [
        'index' => self::$index,
        'body' => [
            'mappings' => [
                self::$type => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'goods_id' => [
                            'type' => 'integer'
                        ],
                        'goods_name' => [
                            'type' => 'keyword'
                        ],
                    ],
                ],
            ],
        ],
    ];
}

** 3、删除索引**

/**
 * 删除索引
 * @return array
 */
public static function delete()
{
    return [
        'index' => 'books'
    ];
}

** 4、更改索引的配置参数**

 public static function putSettings()
{
    return [
        'index' => self::$index,
        'body' => [
            'settings' => [
                'number_of_replicas' => '0',
                'refresh_interval' => '-1',
            ],
        ],
    ];
}

5、获取索引配置参数

/**
 * 获取索引配置参数
 * @return array
 */
public static function getSettings()
{
    return [
        'index' => self::$index,//获取单个
        'index' => [
            self::$index,'order' //获取多个
        ],
    ];
}

** 6、获取映射信息**

public static function getMappings()
{
    //获取所有的索引和类型映射
    return [];
    //获取某个索引里面的所有类型映射
    return [
        'index' => self::$index
    ];
    //获取所有类型的 eg:goods 的映射,而不考虑索引
    return [
        'type' => self::$type,
    ];
    //获取某个索引的某个类型的映射
    return [
        'index' => self::$index,
        'type' => self::$type
    ];
    //获取多个索引的映射
    return [
        'index' => [
            self::$index,'index2'
        ],
    ];
}

7、更改或增加一个索引的映射

/**
 * 更改或增加一个索引的映射
 * @return array
 */
public static function putMappings()
{
    return [
        'index' => 'books',
        'type' => 'IT',
        'body' => [
            self::$type => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'id' => [
                        'type' => 'integer'
                    ],
                    'author' => [
                        'type' => 'keyword'
                    ],
                    'description' => [
                        'type' => 'text',
                        'fielddata' => true,
                    ],
                    'language' => [
                        'type' => 'keyword'
                    ],
                    'title' => [
                        'type' => 'keyword'
                    ],
                ],
            ],
        ],
    ];
}

猜你喜欢

转载自blog.csdn.net/weixin_34390996/article/details/87832639