PHP code example of lvinkim / elasticsearch-odm

1. Go to this page and download the library: Download lvinkim/elasticsearch-odm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lvinkim / elasticsearch-odm example snippets



use Lvinkim\ElasticSearchODM\Annotations as ODM;

/**
 * Class User
 * @ODM\Entity()
 */
class User
{
    /**
     * @var string
     * @ODM\Id
     */
    private $id;

    /**
     * @var int
     * @ODM\Field(property="integer")
     */
    private $age;
    
    /**
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }

    /**
     * @param string $id
     */
    public function setId(string $id): void
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getAge(): int
    {
        return $this->age;
    }

    /**
     * @param int $age
     */
    public function setAge(int $age): void
    {
        $this->age = $age;
    }
}


use Lvinkim\ElasticSearchODM\Repository;

class UserRepository extends Repository
{

    /**
     * 返回索引名, 例如: db, access-log-*, ... 等
     * @return string
     */
    protected function getIndexName(): string
    {
        return 'unit-test';
    }

    /**
     * 返回文档 type , 例如: user, product, ... 等
     * @return string
     */
    protected function getTypeName(): string
    {
        return 'user';
    }

    /**
     * 返回数据表的对应实体类名
     * @return string
     */
    protected function getEntityClassName(): string
    {
        return User::class;
    }
}