nodejs实现es调研报告

nodejs实现es调研报告

1.配置文件

js 复制代码
const esConfig = {
    host: 'http://elastic:[email protected]: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);
})();
相关推荐
StarRocks_labs1 小时前
从InfluxDB到StarRocks:Grab实现Spark监控平台10倍性能提升
大数据·数据库·starrocks·分布式·spark·iris·物化视图
若兰幽竹2 小时前
【Spark分析HBase数据】Spark读取并分析HBase数据
大数据·spark·hbase
R²AIN SUITE3 小时前
金融合规革命:R²AIN SUITE 如何重塑银行业务智能
大数据·人工智能
绿算技术4 小时前
“强强联手,智启未来”凯创未来与绿算技术共筑高端智能家居及智能照明领域新生态
大数据·人工智能·智能家居
只因只因爆5 小时前
spark的缓存
大数据·缓存·spark
Leo.yuan6 小时前
3D 数据可视化系统是什么?具体应用在哪方面?
大数据·数据库·3d·信息可视化·数据分析
只因只因爆6 小时前
spark小任务
大数据·分布式·spark
cainiao0806056 小时前
Java 大视界——Java 大数据在智慧交通智能停车诱导系统中的数据融合与实时更新
java·大数据·开发语言
End9289 小时前
Spark之搭建Yarn模式
大数据·分布式·spark
我爱写代码?9 小时前
Spark 集群配置、启动与监控指南
大数据·开发语言·jvm·spark·mapreduce