【问题标题】:Manage emails with ElasticSearch in Node.js在 Node.js 中使用 ElasticSearch 管理电子邮件
【发布时间】:2019-12-06 16:47:42
【问题描述】:

我使用 ElasticSearch v7。我需要配置 ElasticSearch 来搜索电子邮件。我阅读了很多关于不同类型分析器的信息,我需要在 ElasticSearch 中使用这些分析器来解决这个问题。但是没有任何与 Node.js 相关的文章。我使用 NPM 模块elasticsearch 16.3.0。 这是我创建映射的代码

async function createUserMapping () {
    const textType = {type: 'text'};
    const keywordType = {type: 'keyword'};
    const mapping = {
      properties: {
        lastName: {...textType, boost: FIELD_PRIORITIES.LASTNAME},
        firstName: {...textType, boost: FIELD_PRIORITIES.FIRSTNAME},
        username: {...keywordType, boost: FIELD_PRIORITIES.USERNAME},
        email: {
          ...keywordType,
          boost: FIELD_PRIORITIES.EMAIL,
        },
      },
    };
    await client.indices.putMapping({
      ...ES_CONFIG.INDEX,
      ...ES_CONFIG.USER.TYPE,
      body: mapping,
    });
}

请帮助我创建自定义分析器的代码。可以的话,举个例子吧。

【问题讨论】:

    标签: node.js email elasticsearch elasticsearch-plugin


    【解决方案1】:
    const CustomEmailAnalyzer = 'email_analyzer';
    
    async function addEmailAnalyzer () {
        await client.indices.putSettings({
          ...ES_CONFIG.INDEX,
          body: {
            analysis: {
              analyzer: {
                [CustomEmailAnalyzer]: {
                  type: 'custom',
                  tokenizer: 'uax_url_email',
                  filter: ['lowercase', 'stop'],
                },
              },
            },
          },
        });
    }
    
    async function createUserMapping () {
        const textType = {type: 'text'};
        const keywordType = {type: 'keyword'};
        const mapping = {
          properties: {
            lastName: {...textType, boost: FIELD_PRIORITIES.LASTNAME},
            firstName: {...textType, boost: FIELD_PRIORITIES.FIRSTNAME},
            username: {...keywordType, boost: FIELD_PRIORITIES.USERNAME},
            email: {
              ...textType,
              boost: FIELD_PRIORITIES.EMAIL,
              analyzer: CustomEmailAnalyzer,
            },
          },
        };
        await client.indices.putMapping({
          ...ES_CONFIG.INDEX,
          body: mapping,
        });
    }
    
    (async () => {
      await client.indices.close(ES_CONFIG.INDEX);
      await addEmailAnalyzer();
      await client.indices.open(ES_CONFIG.INDEX);
      await createUserMapping();
    })();
    

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多