idapgroup/elasticsearch-its-easy

使用 Elasticsearch 的简单搜索

1.1.1 2023-05-08 09:57 UTC

This package is auto-updated.

Last update: 2024-09-08 12:58:57 UTC


README

Elasticsearch it`s easy SDK,让您像使用构造函数一样与 Elasticsearch 交互

文档

有关 Elasticsearch REST API 的文档可以在这里找到。

安装

安装此扩展的首选方式是通过composer

运行以下命令:

composer require idapgroup/elasticsearch-its-easy

或者在您的composer.json文件的要求部分添加以下内容:

{
  "require": {
    "idapgroup/elasticsearch-its-easy": "^1.1.1"
  }
}

快速入门

准备您想要存储在 Elasticsearch 中的数据

$data = [
    [
        'user' => [
            'id' => 100,
            'email' => 'stepan21@gmail.com',
            'name' => 'Stepan',
            'age' => 21,
            'birthday' => '2001-06-15',
        ],
        'work' => [
            'position' => [
                'id' => 25,
                'name' => 'php developer',
            ],
            'skills' => [
                [
                    'id' => 36,
                    'name' => 'php'
                ],
                [
                    'id' => 40,
                    'name' => 'mysql'
                ],
                [
                    'id' => 56,
                    'name' => 'js'
                ],
            ],
            'salary' => 4000
        ],
        'location' => [
            'lat' => 50.445077,
            'lon' => 30.521215
        ],
    ],
    [
        'user' => [
            'id' => 101,
            'email' => 'luigi@gmail.com',
            'name' => 'Luigi',
            'age' => 29,
            'birthday' => '2005-03-20',
        ],
        'work' => [
            'position' => [
                'id' => 12,
                'name' => 'js developer',
            ],
            'skills' => [
                [
                    'id' => 56,
                    'name' => 'js'
                ],
                [
                    'id' => 70,
                    'name' => 'mongodb'
                ],
                [
                    'id' => 1,
                    'name' => 'html'
                ],
                [
                    'id' => 2,
                    'name' => 'css'
                ],
            ],
            'salary' => 2700
        ],
        'location' => [
            'lat' => 47.454589,
            'lon' => 32.915673
        ],
    ],
];

创建一个可以被基本搜索扩展的类

use IdapGroup\ElasticsearchItsEasy\ModelSearchBase;

class StaffModelSearch extends ModelSearchBase
{   
    public function setRules() : void
    {
        $this->rules = [
            self::GROUP_MUST => [
                self::RULE_EQUAL => [
                    'userId' => 'user.id',
                ],
            ],
            self::GROUP_SHOULD => [
                self::RULE_LIKE => [
                    'userEmail' => 'user.email',
                    'userName' => 'user.name',
                ],
                self::RULE_IN => [
                    'workSkillsId' => 'work.skills.id',
                ],
            ],
            self::GROUP_FILTER => [
                self::RULE_EQUAL => [
                    'workPositionId' => 'work.position.id'
                ],
                self::RULE_RANGE_NUMBER => [
                    'userAge' => 'user.age',
                    'workSalary' => 'work.salary',
                ],
                self::RULE_RANGE_DATE => [
                    'birthday' => 'user.birthday',
                ],
            ],
            self::GROUP_LOCATION => [
                'location' => self::SORT_DESC,
            ],
        ];
    }
   
    public function setSort() : void
    {
        $this->sort = [
            'user.id' => self::SORT_DESC,
        ];
    }

}

根据 Elasticsearch 配置创建模型

$staffModelSearch = new StaffModelSearch('es01', '9200', 'staff_search');

在 Elasticsearch 中索引文档

$staffModelSearch->reCreateIndex();

foreach ($data as $item) {
    $staffModelSearch->addDocument($item, 'user.id', $item['user']['id']);
}

示例 #1:带有分页的列表搜索

指定搜索键及其值

$params = [
    'userId' => 100,
    'workPositionId' => 25,
    'userEmail' => 'stepan21@gmail.com',
    'userName' => 'Stepan',
    'workSkillsId' => [36, 40],
    'userAge' => ['min' => 18, 'max' => 65],
    'workSalary' => ['min' => 500, 'max' => 5000],
    'location' => [
        'point' => [
            'lat' => 48.454589,
            'lon' => 33.915673,
            'distance' => 100000
        ],
        'rectangle' => [
            'topLeftLat' => 55.710929,
            'topLeftLng' => 14.090451,
            'bottomRightLat' => 41.830140,
            'bottomRightLng' => 41.802791
        ],
    ],
    'page' => 1,
    'limit' => 20
];

如有需要,设置数据输出限制并搜索

//$staffModelSearch->enableFixLimitResult(50);
$result = $staffModelSearch->searchList($params);

响应的结果将以以下格式呈现

[
    'result' => [
        [
            'user' => [
                'id' => 100,
                'email' => 'stepan21@gmail.com',
                'name' => 'Stepan',
                'age' => 21
            ],
            'work' => [
                'position' => [
                    'id' => 25,
                    'name' => 'php developer',
                ],
                'skills' => [
                    [
                        'id' => 36,
                        'name' => 'php'
                    ],
                    [
                        'id' => 40,
                        'name' => 'mysql'
                    ],
                    [
                        'id' => 56,
                        'name' => 'js'
                    ],
                ],
                'salary' => 4000
            ],
            'location' => [
                'lat' => 50.445077,
                'lon' => 30.521215
            ],
        ],
        //... etc.
    ],
    'pagination' => [
        'totalCount' => (int),
        'pageCount' => (int),
        'currentPage' => (int)
    ]
]

示例 #2:搜索映射

指定搜索键及其值

$params = [
    'userId' => 100,
    'workPositionId' => 25,
    'userEmail' => 'stepan21@gmail.com',
    'userName' => 'Stepan',
    'workSkillsId' => [36, 40],
    'userAge' => ['min' => 18, 'max' => 65],
    'workSalary' => ['min' => 500, 'max' => 5000],
    'location' => [
        'point' => [
            'lat' => 48.454589,
            'lon' => 33.915673,
            'distance' => 100000
        ],
        'rectangle' => [
            'topLeftLat' => 55.710929,
            'topLeftLng' => 14.090451,
            'bottomRightLat' => 41.830140,
            'bottomRightLng' => 41.802791
        ],       
    ],
];

执行搜索

$result = $staffModelSearch->searchMap($params);

响应的结果将以以下格式呈现

[
    [
        'user' => [
            'id' => 100,
            'email' => 'stepan21@gmail.com',
            'name' => 'Stepan',
            'age' => 21
        ],
        'work' => [
            'position' => [
                'id' => 25,
                'name' => 'php developer',
            ],
            'skills' => [
                [
                    'id' => 36,
                    'name' => 'php'
                ],
                [
                    'id' => 40,
                    'name' => 'mysql'
                ],
                [
                    'id' => 56,
                    'name' => 'js'
                ],
            ],
            'salary' => 4000
        ],
        'location' => [
            'lat' => 50.445077,
            'lon' => 30.521215
        ],
    ],
    //... etc.
]

附加设置

自定义聚类(当地图上的标记重叠且最大缩放无法解决问题时非常有用)

$params = [
    //...
    'location' => [
        'point' => [
            'lat' => 48.454589,
            'lon' => 33.915673,
            'distance' => 100000
        ],
        'rectangle' => [
            'topLeftLat' => 55.710929,
            'topLeftLng' => 14.090451,
            'bottomRightLat' => 41.830140,
            'bottomRightLng' => 41.802791
        ],
        'clustering' => true,
        'zoom' => 1,
    ],
    //...
];

响应结构示例

[
    // Cluster 1
    [
        [
            // data
        ],
        [
            // data
        ],
        //...
    ],
    // Cluster 2
    [
        [
            // data
        ],
        [
            // data
        ],
        //...
    ],
    //... etc.
]

覆盖规则

// Describe the rules in the associative array as required by the ES documentation
$overWriteRules = [
    'must' => [
        [
            'term' => [
                'user.name.keyword' => 'Stepan',
            ],
        ],
        [
            'term' => [
                'user.age' => 21,
            ],
        ],
        //...
    ],
    //...
];

// Use a Method to Set Your Rules
$staffModelSearch->setOverWriteRules($overWriteRules);