国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

elasticsearch插件,快速調用elasticsearch

songshu

elasticsearch 客戶端

簡介

     本客戶端旨在降低elasticsearch的上手難度,依賴于官方的客戶端插件elasticsearch/elasticsearch。直接使用官方客戶端需要手動構建復雜的請求體,
稍微有一點錯誤,操作結果就不對。所以單獨構建一個依賴官方客戶端的插件,用戶只需要傳入關鍵字即可,后面增加了類似于關系型數據庫
的鏈式操作方法,用起來更簡單一些。當然,本插件只能滿足一些常用的功能需求,較為復雜的需求仍然需要手動構建請求體,你可以使用本插件
直接調用官方客戶端的方法。

注意

根據elasticsearch安全策略需求,若你安裝的elasticsearch8以下,請使用v1.0.9及以下版本,若你安裝的elasticsearch8及以上版本,請使用v1.1.0及以上版本。

客戶端安裝方法

composer require xiaosongshu/elasticsearch

elasticsearch服務配置

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.17.7

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e "xpack.security.http.ssl.enabled=false" elasticsearch:8.15.0

兼容elasticsearch v8.15.5版本,高版本需要使用賬號密碼,賬號為elastic密碼為123456端口號為9201,可以根據自己的實際需求調整配置。

docker run -d --name my-es -p 9201:9200 -p 9301:9300 -e "discovery.type=single-node" -e "ELASTIC_PASSWORD=123456" -e "xpack.security.enabled=true" elasticsearch:8.15.5

如果是多個服務之間調用,可能需要創(chuàng)建網絡

# 創(chuàng)建默認的網絡橋接
docker network create default_network
# 創(chuàng)建網絡
docker network create shared_network
# 將Elasticsearch容器加入網絡(假設容器名為my-es,根據實際修改)
docker network connect shared_network my-es
# 將PHP容器加入網絡(假設容器名為your_php_container_name,根據實際修改)
docker network connect shared_network your_php_container_name

關于IK分詞器

參考 加入Ik分詞器的方法:https://blog.csdn.net/weixin_44364444/article/details/125758975

基本配置

支持thinkPHP,laravel,webman等常用框架,需要創(chuàng)建配置文件elasticsearch.php ,放到config/目錄下。
配置內容如下所示:

 return [
    /** 節(jié)點列表 */
    'nodes' => ['127.0.0.1:9200'],
    /** 用戶名 */
    'username'=>'',
    /** 密碼 */
    'password'=>'',
];

基本用法

實例化客戶端

$client = new \Xiaosongshu\Elasticsearch\ESClient();

使用方法


<?php
require_once 'vendor/autoload.php';

/** 實例化客戶端 elasticsearch鏈式操作演示 */
$client = new \Xiaosongshu\Elasticsearch\ESClient([
    /** 節(jié)點列表 */
    'nodes' => ['192.168.101.170:9200'],
    /** 用戶名 */
    'username' => '',
    /** 密碼 */
    'password' => '',
]);

/** 獲取表結構 */
$result = $client->getMap(['index']);

/** 刪除索引 */
$client->deleteIndex('index');
/** 如果不存在index索引,則創(chuàng)建index索引 */
if (!$client->IndexExists('index')) {
    /** 創(chuàng)建索引 */
    $client->createIndex('index', '_doc');
}
/** 創(chuàng)建表 */
$result = $client->createMappings('index', '_doc', [
    'id' => ['type' => 'long',],
    'title' => ['type' => 'text', "fielddata" => true,],
    'content' => ['type' => 'text', 'fielddata' => true],
    'create_time' => ['type' => 'text'],
    'test_a' => ["type" => "integer"],
    'test_b' => ["type" => "rank_feature", "positive_score_impact" => false],
    'test_c' => ["type" => "rank_feature"],
    'name' => ['type' => 'text', "fielddata" => true,],
    'age' => ['type' => 'integer'],
    'sex' => ['type' => 'integer'],
]);

/** 批量插入數據鏈式操作 */
$result = $client->table('index', '_doc')->insertAll([
    [
        'id' => rand(1, 99999),
        'title' => '天有不測風云',
        'content' => '月有陰晴圓缺',
        'create_time' => date('Y-m-d H:i:s'),
        'test_a' => rand(1, 10),
        'test_b' => rand(1, 10),
        'test_c' => rand(1, 10),
        'name' => '張三',
        'age' => 27,
        'sex' => 1
    ]
]);
/** 調用elasticsearch原生方法 */
$params = [
    'index' => 'my_index',
    'type' =>"_doc",
    'id' => "demo",
];
/** 使用原生方法統(tǒng)計滿足某條件的數據 */
$result = $client->count($params);
/** 使用原生方法判斷是否存在某一條數據 */
$result = $client->exists($params);

/** 查詢數據鏈式操作 */
$result = $client
    /** 設置表名 */
    ->table('index','_doc')
    /** must限制條件 */
    ->where(['title','=','測試'])
    /** whereIn查詢 */
    ->whereIn('age',[28])
    /** whereNotIn查詢 */
    ->whereNotIn('age',[27,29])
    /** should限制條件 當must和should沖突的時候,must條件優(yōu)先 */
    ->orWhere(['test_a','>',8])
    /** 排序 */
    ->orderBy('test_a','asc')
    /** 分頁 */
    ->limit(0,10)
    /** 篩選查詢字段 */
    ->select(['name','age'])
    /** 按字段分組 */
    ->groupBy(['age','sex'])
    /** 聚合查詢 */
    ->sum(['age'])
    /** 執(zhí)行查詢操作 */
    ->getAll();

/** 聚合查詢鏈式操作 */
$result = $client->table('index','_doc')->max(['age'])->getAll();
/** 獲取所有數據 */
$result = $client->table('index','_doc')->getAll();

/** 根據條件更新所有數據鏈式操作 */
$result = $client->table('index','_doc')->where(['test_a','>',2])->updateAll(['name'=>'陳圓圓']);
/** 根據條件刪除數據鏈式操作 */
$result = $client->table('index','_doc')->where(['test_a','>',2])->deleteAll();
/** 獲取所有的index索引 */
$result = $client->getIndex(['index']);
/** 使用id更新數據 */
$result = $client->table('index','_doc')->updateById('kmXADJEBegXAJ580Qqp6',['content'=>'今天你測試了嗎']);
/** 使用id 刪除數據 */
$result = $client->table('index','_doc')->deleteByIds(['kmXADJEBegXAJ580Qqp6']);
/** 使用id查詢數據 */
$result = $client->table('index','_doc')->findById('kmXADJEBegXAJ580Qqp6');
/** 使用id批量查詢 */
$result = $client->table('index','_doc')->getByIds(['kmXADJEBegXAJ580Qqp6']);
/** 添加腳本 */
$script = <<<eof
if (doc.containsKey('content') && doc['content'].size() != 0) {
        return doc['content.raw'].value + '_' + '誰不說按家鄉(xiāng)好';
      } else {
        return '字段缺失'; // 或者返回其他適當的默認值
      }
eof;
$result = $client->addScript('update_content',$script);
/** 添加腳本 */
$result = $client->addScript('update_content2',"(doc['content'].value)+'_'+'abcdefg'");
/** 獲取腳本內容 */
$result = $client->getScript('update_content');
/** 使用腳本查詢 */
$result = $client->table('index','_doc')->withScript('update_content11')->getAll();
/** 刪除腳本*/
$result = $client->deleteScript('update_content');
/** 原生查詢 ,請根據es版本自行編寫原生查詢語句 */
$result = $client->table('index')->query([
    'index'=>'index',
    'body'=>[
        'query'=>[
            'match_all'=>new \stdClass(),
        ],
        '_source'=>['title','content'],
    ],
    'from'=>0,
    'size'=>10,
]);
/** 打印處理 結果*/
print_r($result);

測試

 php ./vendor/bin/phpunit -c phpunit.xml
1511 4 6
4個評論

Tinywan

感謝分享!

markowner

Problem 1

  • Root composer.json requires xiaosongshu/elasticsearch 1.1.4 -> satisfiable by xiaosongshu/elasticsearch[v1.1.4].
  • xiaosongshu/elasticsearch v1.1.4 requires elasticsearch/elasticsearch ^7.17 -> found elasticsearch/elasticsearch[v7.17.0, v7.17.1, v7.17.2] but it conflicts with your root composer.json require (^8.6).
    這是為啥呢,不是8以上用 v1.1.0 及以上嗎
  • songshu 24天前

    你試試v1.1.5呢,我測試es版本是elasticsearch:8.15.5,測試顯示正常

songshu

elasticsearch 版本8以上測試
安裝es服務:

docker run -d --name my-es -p 9201:9200 -p 9301:9300 -e "discovery.type=single-node" -e "ELASTIC_PASSWORD=123456" -e "xpack.security.enabled=true" elasticsearch:8.15.5

測試demo.php內容如下:

<?php
require_once 'vendor/autoload.php';

/** 實例化客戶端 elasticsearch鏈式操作演示 */
$client = new \Xiaosongshu\Elasticsearch\ESClient(
    [
        'nodes' => [
            "192.168.110.72:9201",
            "127.0.0.1:9201",
        ],
        'username' => 'elastic',
        'password' => '123456',
        'proxy' => [
            'client' => [
                'curl' => [
                    CURLOPT_PROXY => '', // 明確禁用代理
                    CURLOPT_PROXYUSERPWD => '', // 清除代理認證
                    CURLOPT_NOPROXY => '*' // 對所有主機禁用代理
                ]
            ]
        ]
    ]
);

/** 如果不存在index索引,則創(chuàng)建index索引 */
if (!$client->IndexExists('index')) {
    /** 創(chuàng)建索引 */
    $client->createIndex('index', '_doc');
    /** 創(chuàng)建表 */
    $result = $client->createMappings('index', '_doc', [
        'id' => ['type' => 'long',],
        'title' => ['type' => 'text', "fielddata" => true,],
        'content' => ['type' => 'text', 'fielddata' => true],
        'create_time' => ['type' => 'text'],
        'test_a' => ["type" => "integer"],
        'test_b' => ["type" => "rank_feature", "positive_score_impact" => false],
        'test_c' => ["type" => "rank_feature"],
        'name' => ['type' => 'text', "fielddata" => true,],
        'age' => ['type' => 'integer'],
        'sex' => ['type' => 'integer'],
    ]);
}
/** 批量插入數據鏈式操作 */
$result = $client->table('index', '_doc')->insertAll([
    [
        'id' => rand(1, 99999),
        'title' => '天有不測風云',
        'content' => '月有陰晴圓缺',
        'create_time' => date('Y-m-d H:i:s'),
        'test_a' => rand(1, 10),
        'test_b' => rand(1, 10),
        'test_c' => rand(1, 10),
        'name' => '張三',
        'age' => 27,
        'sex' => 1
    ]
]);

$result = $client->table('index', '_doc')->getAll();
print_r($result);

執(zhí)行命令打印測試結果

php demo.php

控制臺打印結果如下

PS D:\php\esLib> php .\demo.php
Array
(
    [took] => 2
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 1
            [successful] => 1
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => Array
                (
                    [value] => 3
                    [relation] => eq
                )

            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => index
                            [_id] => luQvu5gBNSYzMoPMWPh1
                            [_score] => 1
                            [_source] => Array
                                (
                                    [id] => 85746
                                    [title] => 天有不測風云
                                    [content] => 月有陰晴圓缺
                                    [create_time] => 2025-08-18 11:18:18
                                    [test_a] => 10
                                    [test_b] => 9
                                    [test_c] => 10
                                    [name] => 張三
                                    [age] => 27
                                    [sex] => 1
                                )

                        )

                    [1] => Array
                        (
                            [_index] => index
                            [_id] => l-Qvu5gBNSYzMoPMbfiF
                            [_score] => 1
                            [_source] => Array
                                (
                                    [id] => 47805
                                    [title] => 天有不測風云
                                    [content] => 月有陰晴圓缺
                                    [create_time] => 2025-08-18 11:18:24
                                    [test_a] => 8
                                    [test_b] => 5
                                    [test_c] => 8
                                    [name] => 張三
                                    [age] => 27
                                    [sex] => 1
                                )

                        )

                    [2] => Array
                        (
                            [_index] => index
                            [_id] => mOQvu5gBNSYzMoPMxfjX
                            [_score] => 1
                            [_source] => Array
                                (
                                    [id] => 4797
                                    [title] => 天有不測風云
                                    [content] => 月有陰晴圓缺
                                    [test_a] => 6
                                    [test_b] => 2
                                    [test_c] => 8
                                    [name] => 張三
                                    [age] => 27
                                    [sex] => 1
                                )

                        )

                )

        )

)
  • 暫無評論
zikn

$client->query方法報錯
改成截圖這樣就行了

  • songshu 17天前

    已修改

  • zikn 17天前

    大佬,多條件組合的count,需要寫原生實現(xiàn)嗎

  • songshu 17天前
    /** 查詢數據鏈式操作 */
    $result = $client
        /** 設置表名 */
        ->table('index','_doc')
        /** must限制條件 */
        ->where(['title','=','測試'])
        /** whereIn查詢 */
        ->whereIn('age',[28])
        /** whereNotIn查詢 */
        ->whereNotIn('age',[27,29])
        /** should限制條件 當must和should沖突的時候,must條件優(yōu)先 */
        ->orWhere(['test_a','>',8])
        /** 排序 */
        ->orderBy('test_a','asc')
        /** 分頁 */
        ->limit(0,10)
        /** 篩選查詢字段 */
        ->select(['name','age'])
        /** 按字段分組 */
        ->groupBy(['age','sex'])
        /** 聚合查詢 */
        ->sum(['age'])
        /** 執(zhí)行查詢操作 */
        ->getAll();

    如果這種不滿足,那就寫原生吧

songshu

450
積分
0
獲贊數
0
粉絲數
2022-06-23 加入
??