国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索

這篇具有很好參考價(jià)值的文章主要介紹了thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

提示:文章寫(xiě)完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔


前言

提示:thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索:

thinkphp數(shù)據(jù)庫(kù)配置文件

 // Elasticsearch數(shù)據(jù)庫(kù)配置信息
        'elasticsearch' => [
            'scheme' =>'http',
            'host' => '127.0.0.1',
            'port' => '9200',
            'user' => '',
            'pass' => '',
            'timeout' => 2,
        ],

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索

示例:thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索

二、使用步驟

1.引入庫(kù)

直接上代碼如下(示例):

composer require "elasticsearch/elasticsearch": "7.0.*"

2.讀入數(shù)據(jù)

代碼如下(示例):


namespace app\common\model;
use think\facade\Db;
use think\Model;
use Elasticsearch\ClientBuilder;
class Article extends Model
{
    protected $client;

    public function __construct($data = [])
    {
        parent::__construct($data);

        try {	
            $this->client = ClientBuilder::create()
                ->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])
                ->build();
        } catch (\Exception $e) {
            // 輸出連接錯(cuò)誤信息
            echo $e->getMessage();
            exit;
        }
    }

    // 添加文檔到Elasticsearch
    public function addDocument()
    {
        $articles = Db::name('article')->select();
        foreach ($articles as $article) {
            $data = [
                'id' => $article['id'], // 文章表的 ID
                'title' => $article['title'],
                'content' => $article['content'],
                'category_id' => $article['category_id'], // 文章表的 ID
            ];

            $params = [
                'index' => 'articles', // 索引名稱
                'id' => $data['id'], // 文章 ID 作為文檔的唯一標(biāo)識(shí)
                'body' => $data,
            ];

            $response = $this->client->index($params);
        }
        return $response;
    }

    // 搜索文檔
    public function searchDocuments($index,$query)
    {
        $params = [
            'index' => $index,
            'body' => [
                'query' => [
                    'multi_match' => [
                        'query' => $query,
                        'fields' => ['title', 'content'],
                    ],
                ],
            ],
        ];

        $response = $this->client->search($params);
        return $response;
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2023/9/2
 * Time: 17:55
 */

namespace app\common\model;
use think\facade\Db;
use think\Model;
use Elasticsearch\ClientBuilder;

class Book extends Model
{
    protected $client;

    public function __construct($data = [])
    {
        parent::__construct($data);

        try {
            // $host = config('database.connections.elasticsearch.host');
            // $port = config('database.connections.elasticsearch.port');
            $this->client = ClientBuilder::create()
                ->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])
                ->build();
        } catch (\Exception $e) {
            // 輸出連接錯(cuò)誤信息
            echo $e->getMessage();
            exit;
        }
    }

    // 添加文檔到Elasticsearch
    public function addDocument()
    {
        $books = Db::name('book')->select();
        foreach ($books as $book) {
            $data = [
                'id' => $book['id'], // 書(shū)籍表的 ID
                'user_id' => $book['user_id'], // 書(shū)籍表作者ID
                'book' => $book['book'],
            ];

            $params = [
                'index' => 'books', // 索引名稱
                'id' => $data['id'], // 文章 ID 作為文檔的唯一標(biāo)識(shí)
                'body' => $data,
            ];

            $response = $this->client->index($params);
        }
        return $response;
    }

    // 搜索文檔
    public function searchDocuments($index,$query)
    {
        $params = [
            'index' => $index,
            'body' => [
                'query' => [
                    'multi_match' => [
                        'query' => $query,
                        'fields' => ['book'],
                    ],
                ],
            ],
        ];

        $response = $this->client->search($params);
        return $response;
    }
}
<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2023/9/2
 * Time: 15:27
 * 全局搜素模型
 */

namespace app\common\model;

use think\Model;
use Elasticsearch\ClientBuilder;

class ElasticsearchModel extends Model
{
    protected $client;

    public function __construct($data = [])
    {
        parent::__construct($data);

        try {
            $this->client = ClientBuilder::create()
                ->setHosts([config('database.connections.elasticsearch.host') . ':' . config('database.connections.elasticsearch.port')])
                ->build();
        } catch (\Exception $e) {
            // 輸出連接錯(cuò)誤信息
            echo $e->getMessage();
            exit;
        }
    }

    public function globalSearch($keyword)
    {
        // 搜索articles索引
        $articles = $this->searchIndex('articles', $keyword);

        // 搜索books索引
        $books = $this->searchIndex('books', $keyword);

        // 合并搜索結(jié)果
        $result = array_merge($articles, $books);

        return $result;
    }

    protected function searchIndex($index, $keyword)
    {
        $params = [
            'index' => $index,
            'body' => [
                'query' => [
                    'multi_match' => [
                        'query' => $keyword,
                        'fields' => ['title', 'content','book'],
                    ],
                ],
            ],
        ];

        // 執(zhí)行搜索請(qǐng)求
        $response = $this->client->search($params);

        // 解析結(jié)果
        $result = [];
        if (isset($response['hits']['hits'])) {
            foreach ($response['hits']['hits'] as $hit) {
                $result[] = $hit['_source'];
                $result['index'] = $index;
            }
        }

        return $result;
    }


}
<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2023/9/2
 * Time: 18:53
 */

namespace app\index\controller;

use app\common\model\ElasticsearchModel;

class SearchController
{
	//全局搜索個(gè)表間的數(shù)據(jù)
    public function search($keyword)
    {
        $searchModel = new ElasticsearchModel();
        $result = $searchModel->globalSearch($keyword);
        return json($result);
    }
}


namespace app\index\controller;
use app\BaseController;
use app\common\model\Article as ElasticArticle;
use app\common\model\Book as ElasticBook;
use app\Request;
use Elasticsearch\ClientBuilder;


class Demo1 extends BaseController
{
//新增索引,建議在模型中新增 ,刪除, 修改 或者使用觀察者模式更新ES索引
 public function addDocument()
    {
        $elasticsearchArticle = new ElasticArticle();
        $response = $elasticsearchArticle->addDocument();
        $elasticsearchBook = new ElasticBook();
        $response1 = $elasticsearchBook->addDocument();
        return json($response);
        // print_r(json($response));
        // print_r(json($response1));
    }


 /**
     * 單獨(dú)搜索文章表例子
     */
    public function search(Request $request)
    {
        $keyword = $request->param('keyword');
        $elasticsearchModel = new ElasticArticle();
        $index = 'articles';
        $query = '你';

        $response = $elasticsearchModel->searchDocuments($index, $query);
        return json($response);
    }

	//單獨(dú)搜搜書(shū)籍表
    public function searchBook(Request $request)
    {
        $keyword = $request->param('keyword');
        $elasticsearchModel = new ElasticBook();
        $index = 'books';
        $query = '巴黎';

        $response = $elasticsearchModel->searchDocuments($index, $query);
        return json($response);
    }




    public function deleteIndex()
    {
        $client = ClientBuilder::create()->build();

        $params = [
            'index' => 'my_index', // 索引名稱
        ];

        $response = $client->indices()->delete($params);

        if ($response['acknowledged']) {
            return '索引刪除成功';
        } else {
            return '索引刪除失敗';
        }
    }

}

使用的表

CREATE TABLE `article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_id` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `content` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=107 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `book` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `elasticsearch_model` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `model_name` varchar(255) NOT NULL DEFAULT '' COMMENT '模型名稱',
  `index_name` varchar(255) NOT NULL DEFAULT '' COMMENT '索引名稱',
  `created_time` int(11) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
  `updated_time` int(11) NOT NULL DEFAULT '0' COMMENT '更新時(shí)間',
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_name_unique` (`index_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='Elasticsearch 模型配置表';


結(jié)果
thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)
thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)

windwos 上記住 安裝 Elasticsearch 7.0 庫(kù), 和 kibana-7.0.0-windows-x86_64 圖像管理工具
thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)
thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)

thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)

thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索,php,elasticsearch,jenkins,大數(shù)據(jù)

總結(jié)

提示:這是簡(jiǎn)單例子, 注意’fields’ => [‘title’, ‘content’], 嘗試使用搜索number型字段,索引報(bào)錯(cuò), 貌似只支持txt類型字段搜索
例如:以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了Elasticsearch的使用文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-691430.html

到了這里,關(guān)于thinkphp中使用Elasticsearch 7.0進(jìn)行多表的搜索的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Elasticsearch:使用 fuzziness 來(lái)進(jìn)行搜索

    在我之前的文章 “Elasticsearch:fuzzy 搜索 (模糊搜索)”,我詳細(xì)描述了模糊搜索。盡管那篇文章已經(jīng)很詳盡了,但是還是有 auto 這個(gè)配置沒(méi)有完全覆蓋到。在今天的文章中,我們來(lái)進(jìn)一步對(duì)這個(gè)進(jìn)行講解一下。 Fuzziness 參數(shù)存在于某些查詢中,使用它時(shí),你將受益于根據(jù)術(shù)

    2024年02月08日
    瀏覽(23)
  • Elasticsearch:使用 ELSER v2 進(jìn)行語(yǔ)義搜索

    Elasticsearch:使用 ELSER v2 進(jìn)行語(yǔ)義搜索

    在我之前的文章 “Elasticsearch:使用 ELSER 進(jìn)行語(yǔ)義搜索”,我們展示了如何使用 ELESR v1 來(lái)進(jìn)行語(yǔ)義搜索。在使用 ELSER 之前,我們必須注意的是: 重要 :雖然 ELSER V2 已正式發(fā)布,但 ELSER V1 仍處于 [預(yù)覽] 狀態(tài)。此功能處于技術(shù)預(yù)覽階段,可能會(huì)在未來(lái)版本中更改或刪除。 E

    2024年02月22日
    瀏覽(25)
  • Elasticsearch:使用 ELSER 文本擴(kuò)展進(jìn)行語(yǔ)義搜索

    Elasticsearch:使用 ELSER 文本擴(kuò)展進(jìn)行語(yǔ)義搜索

    在今天的文章里,我來(lái)詳細(xì)地介紹如何使用 ELSER??進(jìn)行文本擴(kuò)展驅(qū)動(dòng)的語(yǔ)義搜索。 如果你還沒(méi)有安裝好自己的 Elasticsearch 及 Kibana,請(qǐng)參考如下的鏈接來(lái)進(jìn)行安裝: 如何在 Linux,MacOS 及 Windows 上進(jìn)行安裝 Elasticsearch Kibana:如何在 Linux,MacOS 及 Windows 上安裝 Elastic 棧中的 Kiba

    2024年02月07日
    瀏覽(25)
  • Elasticsearch:使用查詢規(guī)則(query rules)進(jìn)行搜索

    Elasticsearch:使用查詢規(guī)則(query rules)進(jìn)行搜索

    在之前的文章 “Elasticsearch 8.10 中引入查詢規(guī)則 - query rules”,我們?cè)斒隽巳绾问褂?query rules 來(lái)進(jìn)行搜索。這個(gè)交互式筆記本將向你介紹如何使用官方 Elasticsearch Python 客戶端來(lái)使用查詢規(guī)則。 你將使用 query rules API 將查詢規(guī)則存儲(chǔ)在 Elasticsearch 中,并使用 rule_query 查詢它們。

    2024年02月21日
    瀏覽(25)
  • 使用 Elasticsearch、OpenAI 和 LangChain 進(jìn)行語(yǔ)義搜索

    使用 Elasticsearch、OpenAI 和 LangChain 進(jìn)行語(yǔ)義搜索

    在本教程中,我將引導(dǎo)您使用 Elasticsearch、OpenAI、LangChain 和 FastAPI 構(gòu)建語(yǔ)義搜索服務(wù)。 LangChain 是這個(gè)領(lǐng)域的新酷孩子。 它是一個(gè)旨在幫助你與大型語(yǔ)言模型 (LLM) 交互的庫(kù)。 LangChain 簡(jiǎn)化了與 LLMs 相關(guān)的許多日常任務(wù),例如從文檔中提取文本或在向量數(shù)據(jù)庫(kù)中對(duì)它們建立索引

    2024年02月08日
    瀏覽(21)
  • Elasticsearch:Search tutorial - 使用 Python 進(jìn)行搜索 (二)

    Elasticsearch:Search tutorial - 使用 Python 進(jìn)行搜索 (二)

    這個(gè)是繼上一篇文章 “Elasticsearch:Serarch tutorial - 使用 Python 進(jìn)行搜索 (一)” 的續(xù)篇。在今天的文章中,我們接著來(lái)完成如何進(jìn)行分頁(yè)及過(guò)濾。 應(yīng)用程序處理大量結(jié)果通常是不切實(shí)際的。 因此,API 和 Web 服務(wù)使用分頁(yè)控件來(lái)允許應(yīng)用程序請(qǐng)求小塊或頁(yè)面的結(jié)果。 你可能已

    2024年02月01日
    瀏覽(26)
  • Elasticsearch:Search tutorial - 使用 Python 進(jìn)行搜索 (三)

    Elasticsearch:Search tutorial - 使用 Python 進(jìn)行搜索 (三)

    這個(gè)是繼上一篇文章 “Elasticsearch:Serarch tutorial - 使用 Python 進(jìn)行搜索 (二)” 的續(xù)篇。在今天的文章中,本節(jié)將向你介紹一種不同的搜索方式,利用機(jī)器學(xué)習(xí) (ML) 技術(shù)來(lái)解釋含義和上下文。 在機(jī)器學(xué)習(xí)中,嵌入是表示現(xiàn)實(shí)世界對(duì)象(例如單詞、句子、圖像或視頻)的向量

    2024年02月02日
    瀏覽(27)
  • 通過(guò) Elasticsearch 和 Go 使用混合搜索進(jìn)行地鼠狩獵

    通過(guò) Elasticsearch 和 Go 使用混合搜索進(jìn)行地鼠狩獵

    作者:CARLY RICHMOND,LAURENT SAINT-FéLIX 就像動(dòng)物和編程語(yǔ)言一樣,搜索也經(jīng)歷了不同實(shí)踐的演變,很難在其中做出選擇。 在本系列的最后一篇博客中,Carly Richmond 和 Laurent Saint-Félix 將搜索和向量搜索結(jié)合起來(lái),使用 Go 客戶端在 Elasticsearch 中尋找地鼠(gopher)。 今天構(gòu)建

    2024年02月05日
    瀏覽(19)
  • 快速入門(mén):使用 Gemini Embeddings 和 Elasticsearch 進(jìn)行向量搜索

    快速入門(mén):使用 Gemini Embeddings 和 Elasticsearch 進(jìn)行向量搜索

    Gemini 是 Google DeepMind 開(kāi)發(fā)的多模態(tài)大語(yǔ)言模型家族,作為 LaMDA 和 PaLM 2 的后繼者。由 Gemini Ultra、Gemini Pro 和 Gemini Nano 組成,于 2023 年 12 月 6 日發(fā)布,定位為 OpenAI 的競(jìng)爭(zhēng)者 GPT-4。 本教程演示如何使用 Gemini API 創(chuàng)建嵌入并將其存儲(chǔ)在 Elasticsearch 中。 Elasticsearch 將使我們能夠執(zhí)

    2024年01月21日
    瀏覽(25)
  • 使用 LangChain 和 Elasticsearch 對(duì)私人數(shù)據(jù)進(jìn)行人工智能搜索

    使用 LangChain 和 Elasticsearch 對(duì)私人數(shù)據(jù)進(jìn)行人工智能搜索

    關(guān)于本博文的所有代碼可以在地址下載:GitHub - liu-xiao-guo/python-vector-private 我將在本博文中其中深入研究人工智能和向量嵌入的深水區(qū)。 ChatGPT 令人大開(kāi)眼界,但有一個(gè)主要問(wèn)題。 這是一個(gè)封閉的托管系統(tǒng)。 在一個(gè)被大型網(wǎng)絡(luò)公司改變的世界里生活了二十年之后,我們作為人

    2024年02月07日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包