文本搜索node js--meilisearch

meilisearch 文本搜索处理

meilisearch 安装

建议使用docker 安装,

官网:https://www.meilisearch.com/docs/home

demo 示例安装使用独立的文件进行处理,版本号:V1.35.0

下载url:https://github.com/meilisearch/meilisearch

直接双击下载的文件(meilisearch-windows-amd64.exe),找到master_key 的值,把值复制一下

准备启动

需要配置环境变量

set MEILI_MASTER_KEY 贴贴master_key 的值

再执行:meilisearch-windows-amd64.exe

登录后台管理

打开地址:http://localhost:7700

输入master-key

初始化一笔数据,

1.从网址复制数据JSON,单独保存到当前项目中文件moves.json,用于测试:

网址:https://www.cnblogs.com/zhangyh-blog/p/18261643

截图如下:

2.安装使用sdk

npm install meilisearch@0.55.0,版本:0.55.0

网址:https://github.com/meilisearch/meilisearch-js?tab=readme-ov-file#settings

https://github.com/meilisearch/meilisearch

3.准备代码

javascript 复制代码
const { Meilisearch } = require('meilisearch');

(async () => {
    const client = new Meilisearch({
        host: 'http://localhost:7700',
        apiKey: 'aEd5TX5Iz8cAQUIUECoPhu6Tacx4b3uAhMPXMCA7eT8',//masterKey
    });

    const moviesIndex = await client.index('movies');

    // 用于搜索的字段
    await moviesIndex.updateSearchableAttributes(['name', 'detail']);

    // 自定义排序规则
   // await coursesIndex.updateSortableAttributes(['updatedAt', 'likesCount']);

    //生成10条这样的数据
    //加载json数据从movies.json 文件读取
    const documents = require('./moves.json');
    console.log(documents);

    const documents_n = documents.map((item,index) => {
        item.id = index + 1;
        return item;
    });
    let response = await moviesIndex.addDocuments(documents_n);
    console.log('Documents added successfully');
    console.log(response);
})();
  1. 效果如下

来搜一下,比如输入:里昂

5.查询

代码如下:

javascript 复制代码
const { Meilisearch } = require('meilisearch');

(async () => {
    const client = new Meilisearch({
        host: 'http://localhost:7700',
        apiKey: 'aEd5TX5Iz8cAQUIUECoPhu6Tacx4b3uAhMPXMCA7eT8',//masterKey
    });

    const moviesIndex = await client.index('movies');


    const response = await moviesIndex.search("里昂");
    console.log('Documents search successfully');
    console.log(response);
    //搜索到的结果
    const type = 'movies'
    const data = {};
    data[type] = response.hits;//获取数据结果
})();

6.检查任务:

http://localhost:7700/tasks 需要使用auth bear ,master key

截图如下:

相关推荐
2401_873479403 小时前
如何利用IP查询定位识别电商刷单?4个关键指标+工具配置方案
开发语言·tcp/ip·php
我爱cope3 小时前
【从0开始学设计模式-10| 装饰模式】
java·开发语言·设计模式
菜鸟学Python4 小时前
Python生态在悄悄改变:FastAPI全面反超,Django和Flask还行吗?
开发语言·python·django·flask·fastapi
浪浪小洋5 小时前
c++ qt课设定制
开发语言·c++
charlie1145141915 小时前
嵌入式C++工程实践第16篇:第四次重构 —— LED模板,从通用GPIO到专用抽象
c语言·开发语言·c++·驱动开发·嵌入式硬件·重构
故事和你915 小时前
洛谷-数据结构1-4-图的基本应用1
开发语言·数据结构·算法·深度优先·动态规划·图论
幺风6 小时前
Claude Code 源码分析 — Tool/MCP/Skill 可扩展工具系统
前端·javascript·ai编程
程序猿编码6 小时前
给你的网络流量穿件“隐形衣“:手把手教你用对称加密打造透明安全隧道
linux·开发语言·网络·安全·linux内核
ID_180079054736 小时前
淘宝 API 上货 / 商品搬家 业务场景实现 + JSON 返回示例
前端·javascript·json
M ? A6 小时前
Vue 动态组件在 React 中,VuReact 会如何实现?
前端·javascript·vue.js·经验分享·react.js·面试·vureact