尊龙凯时人生就是博

ThinkPHP6ÖÐÔõÑù¾ÙÐÐElasticsearchÈ«ÎÄËÑË÷²Ù×÷£¿

Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤ºÍÊý¾ÝÁ¿µÄÔöÌí£¬ÔõÑù¸ßЧµØ¾ÙÐÐÈ«ÎÄËÑË÷ÒѾ­³ÉΪÁËÔ½À´Ô½¶à¿ª·¢ÕßÃæÁÙµÄÎÊÌâ¡£elasticsearchÊÇÒ»ÖÖÊ¢ÐеÄÈ«ÎÄËÑË÷ÒýÇ棬ËüÄܹ»¿ìËÙ´¦Àí´ó×ÚµÄÎı¾Êý¾Ý£¬²¢¶ÔÆä¾ÙÐмìË÷ºÍÆÊÎö£¬ÕâʹµÃËü³ÉΪÁËÐí¶àwebÓ¦ÓóÌÐòµÄÊ×Ñ¡¹¤¾ß¡£ÏÖÔÚ£¬thinkphp6Ò²ÒѾ­×îÏÈÖ§³ÖelasticsearchÈ«ÎÄËÑË÷²Ù×÷£¬Îª¿ª·¢Õß´øÀ´Ô½·¢¸ßЧµÄËÑË÷¼Æ»®¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚThinkPHP6ÖÐ×°ÖÃElasticsearchÖ§³Ö¿â£¬Õâ¿ÉÒÔͨ¹ýÔÚcomposer.jsonÎļþÖÐÌí¼ÓÒÔÏ´úÂëÀ´Íê³É£º

“require”: {

"elasticsearch/elasticsearch": "^7.0"

µÇ¼ºó¸´ÖÆ

}

È»ºóÔÚÏîÄ¿¸ùĿ¼ÏÂÖ´ÐÐcomposer updateÏÂÁ¼´¿ÉÍê³ÉElasticsearchÖ§³Ö¿âµÄ×°Öá£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

½ÓÏÂÀ´£¬ÎÒÃǽ«½¨ÉèÒ»¸öElasticsearchЧÀÍÌṩÕߣ¬½«Elasticsearch¿Í»§¶ËʵÀý°ó¶¨µ½ÈÝÆ÷ÖУ¬ÒÔ±ãÎÒÃÇËæʱ¿ÉÒÔͨ¹ýÒÀÀµ×¢ÈëÔÚÓ¦ÓóÌÐòÖÐʹÓÃËü¡£ÔÚApp/ProviderĿ¼Ï£¬½¨ÉèElasticsearchServiceProvider.phpÎļþ£¬´úÂëÈçÏ£º

namespace appprovider;

use ElasticsearchClientBuilder;

use thinkService;

class ElasticsearchServiceProvider extends Service

{

public function register()
{
    // »ñÈ¡config/elasticsearch.phpÉèÖÃ
    $config = $this->app->config->get('elasticsearch');

    // ½¨ÉèElasticsearch¿Í»§¶ËʵÀý
    $client = ClientBuilder::create()->setHosts($config['hosts'])->build();

    // ½«Elasticsearch¿Í»§¶ËʵÀý°ó¶¨µ½ÈÝÆ÷ÖÐ
    $this->app->bind('elasticsearch', $client);
}

µÇ¼ºó¸´ÖÆ

}

ÔÚ±¾ÀýÖУ¬ÎÒÃÇÊ×ÏÈ´Óconfig/elasticsearch.phpÉèÖÃÎļþÖлñÈ¡ElasticsearchµÄÖ÷»úµØµã£¬È»ºóʹÓÃClientBuilderÀཨÉèÒ»¸öElasticsearch¿Í»§¶ËʵÀý£¬²¢½«Æä°ó¶¨µ½Ó¦ÓóÌÐòµÄÈÝÆ÷ÖУ¬ÕâÑùÎÒÃǾͿÉÒÔËæʱʹÓÃËüÀ´Ö´ÐÐÈ«ÎÄËÑË÷²Ù×÷ÁË¡£

½ÓÏÂÀ´£¬ÎÒÃǽ«ÑÝʾÔõÑùÔÚThinkPHP6Ó¦ÓóÌÐòÖоÙÐÐÈ«ÎÄËÑË÷²Ù×÷¡£ÔÚÕâÀÎÒÃǽ¨ÉèÒ»¸öElasticsearchServiceЧÀÍÀ࣬¸ÃÀà°üÀ¨Á˼¸¸ö¼òÆÓµÄÒªÁìÀ´Ö´ÐÐËÑË÷²Ù×÷¡£´úÂëÈçÏ£º

namespace appservice;

use ElasticsearchClient;

use ElasticsearchCommonExceptionsMissing404Exception;

use Throwable;

class ElasticsearchService

{

protected $client;

public function __construct(Client $client)
{
    $this->client = $client;
}

/**
 * ½¨ÉèË÷Òý
 *
 * @param string $indexName Ë÷ÒýÃû³Æ
 * @return bool
 */
public function createIndex(string $indexName)
{
    $params = [
        'index' => $indexName,
        'body' => [
            'mappings' => [
                'properties' => [
                    'title' => [
                        'type' => 'text'
                    ],
                    'content' => [
                        'type' => 'text'
                    ]
                ]
            ]
        ]
    ];

    try {
        $response = $this->client->indices()->create($params);
        return true;
    } catch (Throwable $e) {
        throw new Exception('½¨ÉèË÷Òýʧ°Ü£º' . $e->getMessage());
    }
}

/**
 * ɾ³ýË÷Òý
 *
 * @param string $indexName Ë÷ÒýÃû³Æ
 * @return bool
 */
public function deleteIndex(string $indexName)
{
    try {
        $response = $this->client->indices()->delete(['index' => $indexName]);
        return true;
    } catch (Missing404Exception $e) {
        return false;
    } catch (Throwable $e) {
        throw new Exception('ɾ³ýË÷Òýʧ°Ü£º' . $e->getMessage());
    }
}

/**
 * Ìí¼ÓÎĵµ
 *
 * @param string $indexName Ë÷ÒýÃû³Æ
 * @param string $id ÎĵµID
 * @param array $data ÎĵµÊý¾Ý
 * @return bool
 */
public function indexDocument(string $indexName, string $id, array $data)
{
    $params = [
        'index' => $indexName,
        'id' => $id,
        'body' => $data
    ];

    try {
        $response = $this->client->index($params);
        return true;
    } catch (Throwable $e) {
        throw new Exception('Ìí¼ÓÎĵµÊ§°Ü£º' . $e->getMessage());
    }
}

/**
 * ËÑË÷Îĵµ
 *
 * @param string $indexName Ë÷ÒýÃû³Æ
 * @param string $query ÅÌÎÊ×Ö·û´®
 * @return array
 */
public function searchDocuments(string $indexName, string $query)
{
    $params = [
        'index' => $indexName,
        'body' => [
            'query' => [
                'match' => [
                    '_all' => $query
                ]
            ]
        ]
    ];

    try {
        $response = $this->client->search($params);
        return $response['hits']['hits'];
    } catch (Throwable $e) {
        throw new Exception('ËÑË÷ÎĵµÊ§°Ü£º' . $e->getMessage());
    }
}

µÇ¼ºó¸´ÖÆ

}

ÔÚ´ËЧÀÍÀàÖУ¬ÎÒÃǽç˵ÁËËĸöÒªÁ죺createIndex¡¢deleteIndex¡¢indexDocumentºÍsearchDocuments¡£ÕâЩҪÁì·â×°ÁËElasticsearch APIµÄŲÓ㬿ÉÒÔÇáËɵؽ¨É衢ɾ³ýË÷Òý£¬Ìí¼ÓºÍËÑË÷Îĵµ¡£

ÏÖÔÚÎÒÃǽ«ÑÝʾÔõÑùʹÓÃÕâЩҪÁì¡£ÔÚÕâÀÎÒÃǽ«½¨ÉèÒ»¸ö²âÊÔÒ³Ã棬¿ÉÒÔ½¨ÉèÒ»¸öÃûΪ¡°articles¡±µÄË÷Òý£¬²¢Ìí¼ÓһЩÎĵµ£¬È»ºóʹÓÃËÑË÷¿òËÑË÷Îĵµ¡£ÔÚApp/controllerĿ¼Ï£¬½¨ÉèÒ»¸öElasticsearchTestController.phpÎļþ£¬´úÂëÈçÏ£º

namespace appcontroller;

use appServiceElasticsearchService;

use thinkRequest;

class ElasticsearchTestController extends BaseController

{

protected $elasticsearchService;

public function __construct(ElasticsearchService $elasticsearchService)
{
    $this->elasticsearchService = $elasticsearchService;
}

public function index()
{
    $this->elasticsearchService->createIndex('articles');

    // Ìí¼Ó²âÊÔÎĵµ
    $this->elasticsearchService->indexDocument('articles', '1', [
        'title' => 'ThinkPHP',
        'content' => 'ThinkPHPÊÇÒ»¿îÓÅÒìµÄPHP¿ª·¢¿ò¼Ü'
    ]);
    $this->elasticsearchService->indexDocument('articles', '2', [
        'title' => 'Laravel',
        'content' => 'LaravelÊÇÒ»¿îÊ¢ÐеÄPHP¿ª·¢¿ò¼Ü'
    ]);
    $this->elasticsearchService->indexDocument('articles', '3', [
        'title' => 'Symfony',
        'content' => 'SymfonyÊÇÒ»¿îPHP¿ª·¢¿ò¼Ü'
    ]);

    // ËÑË÷¿ò
    $search = Request::instance()->get('search', '');

    // ËÑË÷Ч¹û
    $results = $this->elasticsearchService->searchDocuments('articles', $search);

    // ·µ»ØËÑË÷Ч¹û
    return $this->fetch('index', [
        'results' => $results
    ]);
}

µÇ¼ºó¸´ÖÆ

}

ÔÚ´Ë¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ×¢ÈëÁËElasticsearchServiceЧÀÍ£¬²¢ÔÚindexÒªÁìÖÐŲÓÃÁËcreateIndex¡¢indexDocumentºÍsearchDocumentsÒªÁìÀ´½¨ÉèË÷Òý¡¢Ìí¼ÓÎĵµºÍÖ´ÐÐËÑË÷²Ù×÷¡£ËÑË÷¿òºÍËÑË÷Ч¹ûÒ²±»°üÀ¨ÔÚÁËindexÒªÁìÖС£

ÖÁ´Ë£¬ÎÒÃÇÒѾ­Íê³ÉÁËÔÚThinkPHP6Ó¦ÓóÌÐòÖÐʹÓÃElasticsearch¾ÙÐÐÈ«ÎÄËÑË÷²Ù×÷µÄÑÝʾ¡£ÖµµÃ×¢ÖصÄÊÇ£¬±¾Àý½ö½öÊÇÒ»¸ö¼òÆÓµÄÑÝʾÓÃÀý£¬ÏÖʵÏîÄ¿ÖÐÐèÒªÔ½·¢Ï꾡µØ¾ÙÐÐË÷ÒýÉè¼ÆºÍÎĵµÖÎÀí£¬ÒÔÈ·±£ËÑË÷ЧÂʺÍËÑË÷Ч¹ûµÄ׼ȷÐÔ¡£

×ܵÄÀ´Ëµ£¬Ëæ×ÅElasticsearchµÄÆÕ±éÓ¦Óã¬ËüÒѾ­³ÉΪÁËWebÓ¦ÓóÌÐòÖÐÒ»ÖÖºÜÊÇÊ¢Ðк͸ßЧµÄÈ«ÎÄËÑË÷ÒýÇæ¡£ÔÚThinkPHP6ÖУ¬Í¨¹ýʹÓÃElasticsearchÖ§³Ö¿âºÍElasticsearch API£¬ÎÒÃÇ¿ÉÒÔÇáËɵؾÙÐÐÈ«ÎÄËÑË÷²Ù×÷¡£

ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐElasticsearchÈ«ÎÄËÑË÷²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ