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);
})();
相关推荐
johnsong40 分钟前
当决策成为免费商品:思维成本崩溃背后的治理真空
大数据·人工智能
数字新视界1 小时前
动环监控系统优化数据中心管理,提升环境监测与安全效率
大数据·人工智能·数据中心·微模块机房·模块化机房
zhenaibo5212 小时前
如何向导师请教问题,才能得到有效建议?
大数据·人工智能·深度学习
生活愉甜2 小时前
森马SEMIR LAB全球首家概念店落地武康路,“WUKANG368”新地标就位
大数据·人工智能
乐迪信息2 小时前
港口船舶逆行怎么管?AI防爆摄像机搭载智能检测算法
大数据·人工智能·深度学习·算法·计算机视觉
和裕3 小时前
全纸结构重型纸箱能否满足 1 吨以上设备出口熏蒸豁免要求?通关合规性全解析
大数据·运维·网络·人工智能·算法
代码方舟4 小时前
零信任架构实战:基于天远运营商三要素简版V即时版查询构建自动化电子投保实名核验网关
大数据·人工智能·架构·自动化
xiaohaiAIgeo5 小时前
【2026年】实验室应急预案中通风系统的关键作用
大数据·人工智能·科普知识
Sayai5 小时前
Elasticsearch 日志检索 DSL 实战:时间范围查询、字段去重、分钟级统计与最新日志获取
大数据·运维·elasticsearch·搜索引擎·日志分析
W***25926 小时前
2026 企业 AI 办公工具选型指南:可完成端到端任务的平台怎么评估
大数据·人工智能