nodejs实现es调研报告

nodejs实现es调研报告

1.配置文件

js 复制代码
const esConfig = {
    host: 'http://elastic:syzx123@10.0.30.30:9200',
    index: "eal_data_",
    connectionRequestTimeout: 300000,
    // ES默认支持的浅查询最大分页限制
    maxResultWindow: 9999
};

module.exports = esConfig;

2.方法

  1. 判断某索引是否存在

  2. 查询:超过9999进行深查询,否则进入浅查询

    ​ 深查询时先使用match_phrase进行稍精准的匹配,取前五个数据与match的结果进行合并去重

js 复制代码
var { Client } = require('@elastic/elasticsearch');
const esConfig = require('./es-config');

const client = new Client({
    node: esConfig.host,
    requestTimeout: esConfig.connectionRequestTimeout,
});

/**
 * 判断某索引是否存在
 * @param {*} index
 * @returns
 */
async function isExists(index) {
    let resp;
    try {
        resp = await client.indices.exists({
            index: index
        });
    } catch (e) {
        resp = null;
    }
    return resp;
}

/**
 * 查询
 * @param {*} index
 * @param {*} corpName
 * @param {*} from
 * @param {*} size
 * @param {*} id
 * @returns
 */
async function search(index, corpName, from, size, id) {
    from = from ?? 0;
    size = size ?? 20;
    let result = [];
    try {
        //超过9999 深查询
        if (id && id > esConfig.maxResultWindow) {
            let newResult = [];
            // match_phrase查询出较为精准的结果 取前五 与match结果集去重
            // 使用search_after时,from值必须设置为0或者-1
            let matchPhrase = await client.search({
                index: index,
                body: {
                    query: {
                        match_phrase: {
                            corpName: corpName
                        }
                    }
                },
                from: 0,
                size: 5,
                search_after: [id],
                sort: [{ id: "asc" }]
            });
            if (matchPhrase && matchPhrase.hits.hits) {
                newResult = newResult.concat(matchPhrase.hits.hits)
            }
            let match = await client.search({
                index: index,
                body: {
                    query: {
                        match: {
                            corpName: corpName
                        }
                    }
                },
                from: 0,
                size: size > 5 ? size - 5 : size,
                search_after: [id],
                sort: [{ id: "asc" }]
            });
            if (match && match.hits.hits) {
                newResult = newResult.concat(match.hits.hits)
            }
            if (newResult && newResult.length > 0) {
                result = [...new Set(newResult)];
            }
        } else {
            //浅查询
            let match = await client.search({
                index: index,
                body: {
                    query: {
                        match: {
                            corpName: corpName
                        }
                    }
                },
                from: from,
                size: size,
            });
            result = match && match.hits.hits ? match.hits.hits : [];
        }
    } catch (e) {
        return [];
    }
    return result;
}

(async function () {
    let index = esConfig.index + '20221019';
    let exist = await isExists(index);
    console.log('exists: ', exist);
    let result = await search(index, "沈阳一鸣", 0, 10, 10000);
    console.log('查询结果为:-------------------------------');
    console.log(result);
})();
相关推荐
知见漫记5 分钟前
AI 桌面 Agent 本地执行能力技术对照:沙箱机制与权限模式拆解
大数据·人工智能
xcl09251 小时前
幼儿托育系统开发实战:从需求分析到上线全流程指南
java·大数据·需求分析
cspttty2 小时前
国际经济与贸易专业考什么证对就业有帮助
大数据
足球魅力2 小时前
哪款足球软件能分析凯利指数大数据?
大数据·业界资讯
LaughingZhu2 小时前
Product Hunt 每日热榜 | 2026-09-05
人工智能·深度学习·神经网络·搜索引擎·百度
xwz小王子3 小时前
机器人的“最后一毫米”: 新加坡南洋理工大学Facet-0如何教会基础模型“感受”自己的动作?
大数据·人工智能·机器人
腾视科技-AI3 小时前
腾视科技AIBOX双版本重磅发布!本地安全与全球适配,解锁视频智能新可能
大数据·人工智能·科技·安全·大模型·腾视科技·ai算力盒
姜穆澜3 小时前
OneID 从 0 到 1 完整生产案例(五)
大数据·notepad++
geneculture4 小时前
《融智学:终结信息定义纷争的时代范式》 拓展分析述评
大数据·人工智能·信息科学·融智学的重要应用·哲学与科学统一性·融智时代(杂志)·比较分析
刘同学的 AI学习手记4 小时前
每日 AI PM 情报 · 2026-08-31
大数据·人工智能