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);
})();
相关推荐
Yusei_052344 分钟前
迅速掌握Git通用指令
大数据·git·elasticsearch
一只栖枝7 小时前
华为 HCIE 大数据认证中 Linux 命令行的运用及价值
大数据·linux·运维·华为·华为认证·hcie·it
喂完待续11 小时前
Apache Hudi:数据湖的实时革命
大数据·数据仓库·分布式·架构·apache·数据库架构
青云交11 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
还是大剑师兰特17 小时前
Flink面试题及详细答案100道(1-20)- 基础概念与架构
大数据·flink·大剑师·flink面试题
水无痕simon19 小时前
5 索引的操作
数据库·elasticsearch
SEO_juper19 小时前
AI 搜索时代:引领变革,重塑您的 SEO 战略
人工智能·搜索引擎·seo·数字营销·seo优化
1892280486121 小时前
NY243NY253美光固态闪存NY257NY260
大数据·网络·人工智能·缓存
Blossom.11821 小时前
把 AI 推理塞进「 8 位 MCU 」——0.5 KB RAM 跑通关键词唤醒的魔幻之旅
人工智能·笔记·单片机·嵌入式硬件·深度学习·机器学习·搜索引擎
武子康21 小时前
大数据-70 Kafka 日志清理:删除、压缩及混合模式最佳实践
大数据·后端·kafka