{"id":595,"date":"2022-05-26T18:40:19","date_gmt":"2022-05-26T10:40:19","guid":{"rendered":"https:\/\/usei.cn\/?p=595"},"modified":"2022-05-26T18:40:47","modified_gmt":"2022-05-26T10:40:47","slug":"%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2-elasticsearch%e6%95%b4%e7%90%863-php-es%e7%9a%84%e6%9b%be%e6%9f%a5%e6%94%b9%e5%88%a0","status":"publish","type":"post","link":"https:\/\/usei.cn\/index.php\/2022\/05\/26\/%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2-elasticsearch%e6%95%b4%e7%90%863-php-es%e7%9a%84%e6%9b%be%e6%9f%a5%e6%94%b9%e5%88%a0\/","title":{"rendered":"\u5168\u6587\u68c0\u7d22 ElasticSearch\u6574\u74063 PHP-ES\u7684\u66fe\u67e5\u6539\u5220"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ee5TP5.1\u6846\u67b6\u4e3a\u51c6\uff0cElasticSearch\u7684\u4f9d\u8d56\u5b89\u88c5<\/h2>\n\n\n\n<p>\u5728ElasticSearch\u7684\u5b98\u7f51\u4e2d\u8fdb\u884ccomposer\u7684\u547d\u4ee4\u884c\u590d\u5236\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4e0b\u8f7d\u7684ElasticSearch\u7248\u672c\u662f8.2.0\u7248\u672c\uff0cPHP\u7684ElasticSearch\u4f9d\u8d56\u5305\u5bf9\u5e94\u7684\u4e8b7.11\u7248\u672c\u3002\u5982\u679c\u7528\u7684\u4e0d\u662f\u8be5\u7248\u672c\u53ef\u80fd\u4f1a\u51fa\u73b0ElasticSeearch\u65e0\u6cd5\u6b63\u5e38\u4e0ePHP\u94fe\u63a5\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<p>1\u3001\u5728&nbsp;<code>composer.json<\/code>&nbsp;\u6587\u4ef6\u4e2d\u589e\u52a0 elasticsearch-php\u3002\u5982\u679c\u4f60\u662f\u65b0\u5efa\u9879\u76ee\uff0c\u90a3\u4e48\u628a\u4ee5\u4e0b\u7684\u4ee3\u7801\u590d\u5236\u7c98\u8d34\u5230&nbsp;<code>composer.json<\/code>&nbsp;\u5c31\u884c\u4e86\u3002\u5982\u679c\u662f\u5728\u73b0\u6709\u9879\u76ee\u4e2d\u6dfb\u52a0 elasticsearch-php\uff0c\u90a3\u4e48\u628a elasticsearch-php \u6dfb\u52a0\u5230\u5176\u5b83\u7684\u5305\u540d\u540e\u9762\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"require\": {\n        \"elasticsearch\/elasticsearch\": \"^7.11\"\n        \"elasticsearch\/elasticsearch\": \"~6.0\"\n    }\n}<\/code><\/pre>\n\n\n\n<p>2\u3001\u7136\u540e composer install \u5373\u53ef<\/p>\n\n\n\n<p>3\u3001\u6700\u540e\u52a0\u8f7d autoload.php\u3002\u5982\u679c\u4f60\u73b0\u6709\u9879\u76ee\u662f\u7528 Composer \u5b89\u88c5\u7684\uff0c\u90a3\u4e48 autoload.php \u4e5f\u8bb8\u5df2\u7ecf\u5728\u67d0\u5904\u52a0\u8f7d\u4e86\uff0c\u4f60\u5c31\u4e0d\u5fc5\u518d\u52a0\u8f7d\u3002\u6700\u540e\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5ba2\u6237\u7aef\u5bf9\u8c61\uff1a(\u6ce8\u610f Composer\u5b89\u88c5\u7684\u53ef\u4ee5\u5ffd\u7565\u8fd9\u4e00\u6b65)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>require 'vendor\/autoload.php';\n\n$client = Elasticsearch\\ClientBuilder::create()-&gt;build();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PHP \u4f7f\u7528 ElasticSearch<\/h2>\n\n\n\n<p>1\u3001\u521b\u5efa\u4e00\u4e2a\u7c7b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nnamespace join\\utils; # \u547d\u540d\u7a7a\u95f4\n\nuse Elasticsearch\\ClientBuilder; # \u8fd9\u91cc\u5fc5\u987b\u5f15\u5165\u8fd9\u4e2a\u94fe\u63a5ES\u670d\u52a1\u7684\u7c7b\n\nclass ElasticSearchss{}<\/code><\/pre>\n\n\n\n<p>2\u3001\u5236\u4f5c\u94fe\u63a5ElasticSearch\u7684\u81ea\u52a8\u8fd0\u884c\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public $api; # \u7528\u4e8e\u666e\u901a\u8c03\u7528\n    public $index_name; # \u7d22\u5f15\u540d\u79f0\n    public $index_type; # \u64cd\u4f5c\u7684\u7c7b\u578b\uff0c\u9ed8\u8ba4\u5e94\u8be5\u662f '_doc'\n    public static $Client; # \u7528\u4e8e\u9759\u6001\u8c03\u7528 \n\n    public function __construct($index_name, $index_type)\n    {\n        try {\n            \/\/\u6784\u5efa\u5ba2\u6237\u7aef\u5bf9\u8c61\n            $hosts = &#091;'192.168.0.100:9200']; # \u4f60\u7684ElasticSearch\u670d\u52a1\u5728\u672c\u5730\u7684ip\n            self::$Client = $this-&gt;api = ClientBuilder::create()-&gt;setHosts($hosts)-&gt;build(); # \u94fe\u63a5\n            $this-&gt;index_name = $index_name;\n            $this-&gt;index_type = $index_type;\n        } catch (\\Exception $e) {\n            throw $e;\n        }\n    }<\/code><\/pre>\n\n\n\n<p>3\u3001\u83b7\u53d6\u6240\u6709\u7684\u6587\u6863<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static function getAll()\n    {\n        self::open('huaifeng');\n        $client = ClientBuilder::create()-&gt;build();\n        $params = &#091;\n            \"size\" =&gt; 50, # \u663e\u793a\u7684\u6570\u91cf\uff0c\u7406\u8bba\u4e0a\u5c06\uff0c\u53ef\u4ee5\u8f93\u516510000\n\n            \"type\" =&gt; '_doc' # \u5728\u6587\u6863\u4e2d\u641c\u7d22\n            \"index\" =&gt; \"huaifeng\", # \u7d22\u5f15\u540d\u79f0\n            \"body\" =&gt; &#091; # \u641c\u7d22\u6761\u4ef6\n                \"query\" =&gt; &#091; # \u641c\u7d22\n                    \"match_all\" =&gt; new \\stdClass() # \u641c\u7d22\u5168\u90e8\n                ]\n            ]\n        ];\n        $response = $client-&gt;search($params); # \u6267\u884c\n        dump($response); # \u7ed3\u679c\n    }<\/code><\/pre>\n\n\n\n<p>4\u3001 \u6309\u7167id\u8fdb\u884c\u6392\u5e8f\uff0c\u6a21\u7cca\u641c\u7d22\u5e76\u8fdb\u884c\u5206\u9875<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n     * \u6839\u636e\u5173\u952e\u5b57\u67e5\u8be2\u6570\u636e\n     * \u591a\u4e2a\u5b57\u6bb5\u67e5\u8be2\uff1amulti_match\n     * @param $keyword \u641c\u7d22\u7684\u7ba1\u5173\u952e\u5b57\n     * @param $page \u7b2c\u51e0\u9875\uff0c\u9ed8\u8ba4\u4e3a1\u9875\n     * @param $size \u6bcf\u9875\u6709\u51e0\u6761\u6570\u636e\uff0c\u9ed8\u8ba4\u6bcf\u987510\u6761\n     * @return array|bool\n     * @author \u9648\u946b\n     * @date 2022-5-26\n     *\/\n    public static function pageList($keyword, $page = 1, $size = 10)\n    {\n        $from = $page == 1 ? 0 : ($page * $size) - $size;\n        $params = &#091;\n            'from' =&gt; $from, # \u4ece\u7b2c\u51e0\u6761\u5f00\u59cb\n            'size' =&gt; $size, # \u6bcf\u9875\u67e5\u8be2\u51e0\u6761\n            'index' =&gt; 'huaifeng', # \u7d22\u5f15\u540d\u79f0\n            'body' =&gt; &#091; # \u63d0\u4ea4\u4f53\n                'query' =&gt; &#091; # \u67e5\u8be2\n                    'match' =&gt; &#091; # \u5206\u8bcd\u6a21\u7cca\u67e5\u8be2\n                        'title' =&gt; $keyword # \u4ee5title\u5b57\u6bb5\u67e5\u8be2\u5173\u952e\u5b57\n                    ],\n                ],\n                'sort' =&gt; &#091; # \u6392\u5e8f\n                    'id' =&gt; &#091; # \u6839\u636eid\u5b57\u6bb5\u6392\u5e8f\uff08\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684id\u662f\u4f60\u6dfb\u52a0\u6587\u6863\u8fd4\u56de\u7684id\uff09\n                        'order' =&gt; 'desc', # \u5012\u5e8f\u6392\u5e8f\n                    ]\n                ],\n            ]\n        ];\n        $response = self::$Client-&gt;search($params);\n        $aData&#091;'data'] = array();\n        foreach ($response&#091;'hits']&#091;'hits'] as $k =&gt; $v) {\n            $aData&#091;'data']&#091;] = $v&#091;'_source'];\n        }\n        $aData&#091;'total'] = $response&#091;'hits']&#091;'total']&#091;'value'];\n        return $aData;\n    }\n<\/code><\/pre>\n\n\n\n<p>5\u3001\u6dfb\u52a0\u4e00\u4e2a\u6587\u6863<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static function addIndexDoc($id, $title, $table, $img)\n    {\n\n        # \u9700\u8981\u6dfb\u52a0\u7684\u6587\u6863\u5f00\u59cb\n        $aData&#091;'id'] = $id;\n        $aData&#091;'title'] = $title;\n        $aData&#091;'type'] = $table;\n        $aData&#091;'img'] = $img;\n        # \u9700\u8981\u6dfb\u52a0\u7684\u6587\u6863\u7ed3\u675f\n        $params = &#091;\n            'index' =&gt; 'huaifeng', # \u7d22\u5f15\u540d\u79f0\n            'type' =&gt; '_doc', # \u7c7b\u578b\u4e3a\u6dfb\u52a0\u6587\u6863\n            'body' =&gt; $aData # \u8981\u6dfb\u52a0\u7684\u6570\u636e\n        ];\n        $response = self::$Client-&gt;index($params);\n        return $response&#091;'_id']; # \u83b7\u53d6\u5230\u6dfb\u52a0\u6587\u65e6\u540e\u7684id\u503c\n    }<\/code><\/pre>\n\n\n\n<p>6\u3001\u6839\u636e\u6587\u6863id\u66f4\u65b0\u4e00\u4e2a\u6587\u6863<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static function updataIndexDoc($eid, $id, $title, $table, $img)\n    {\n        $params = &#091;\n            'index' =&gt; 'huaifeng',\n            'type' =&gt; '_doc',\n            'id' =&gt; $eid, # \u6dfb\u52a0\u6587\u6863\u540e\u8fd4\u56de\u7684id\n            'body' =&gt; &#091;\n                'params' =&gt; &#091;\n                    'id' =&gt; $id,\n                    'title' =&gt; $title,\n                    'table' =&gt; $table,\n                    'img' =&gt; $img\n                ]\n            ]\n        ];\n\n        $response = self::$Client-&gt;update($params);\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5TP5.1\u6846\u67b6\u4e3a\u51c6\uff0cElasticSearch\u7684\u4f9d\u8d56\u5b89\u88c5 \u5728ElasticSearch\u7684\u5b98\u7f51\u4e2d\u8fdb\u884ccompo [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-595","post","type-post","status-publish","format-standard","hentry","category-elastic-stack"],"_links":{"self":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":2,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/595\/revisions\/608"}],"wp:attachment":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}