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);
})();
相关推荐
TM1Club6 小时前
AI驱动的预测:新的竞争优势
大数据·人工智能·经验分享·金融·数据分析·自动化
zhang133830890756 小时前
CG-09H 超声波风速风向传感器 加热型 ABS材质 重量轻 没有机械部件
大数据·运维·网络·人工智能·自动化
电商API_180079052478 小时前
第三方淘宝商品详情 API 全维度调用指南:从技术对接到生产落地
java·大数据·前端·数据库·人工智能·网络爬虫
龙山云仓8 小时前
No140:AI世间故事-对话康德——先验哲学与AI理性:范畴、道德律与自主性
大数据·人工智能·深度学习·机器学习·全文检索·lucene
躺柒9 小时前
读数字时代的网络风险管理:策略、计划与执行04风险指引体系
大数据·网络·信息安全·数字化·网络管理·网络风险管理
独自归家的兔11 小时前
从 “局部凑活“ 到 “全局最优“:AI 规划能力的技术突破与产业落地实践
大数据·人工智能
海域云-罗鹏11 小时前
国内公司与英国总部数据中心/ERP系统互连,SD-WAN专线实操指南
大数据·数据库·人工智能
策知道12 小时前
依托政府工作报告准备省考【经验贴】
大数据·数据库·人工智能·搜索引擎·政务
Henry-SAP12 小时前
SAP(ERP) 组织结构业务视角解析
大数据·人工智能·sap·erp·sap pp
TracyCoder12314 小时前
ElasticSearch内存管理与操作系统(一):内存分配底层原理
大数据·elasticsearch·搜索引擎