阿里云ElasticSearch安装开源插件实践

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 阿里云ElasticSearch安装开源插件实践,包含较为常用的NLPchina/elasticsearch-sql与medcl/elasticsearch-analysis-stconvert两个插件的安装、使用测试及同类插件的差异对比

1. 环境准备

1.1. 集群环境

阿里云ElasticSearch V6.7.0

1.2. 数据准备

1.2.1. 索引创建

PUT /products_info
{
  "mappings": {
    "products": {
      "properties": {
        "productName": {
          "type": "text",
          "analyzer": "ik_smart"
        },
        "annual_rate":{
          "type":"keyword"
        },
        "describe": {
         "type": "text",
         "analyzer": "ik_smart"
        },
        "product_age":{
          "type":"integer"
        }
      }
    }
  }
}

1.2.2. 数据导入

POST /products_info/products/_bulk
{"index":{}}
{"productName":"理财产品A","annual_rate":"3.2200%","describe":"180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送","product_age":0}
{"index":{}}
{"productName":"理财产品B","annual_rate":"3.1100%","describe":"90天定投产品,最低10000起投,每天收益到账消息推送","product_age":2}
{"index":{}}
{"productName":"理财产品C","annual_rate":"3.3500%","describe":"270天定投产品,最低40000起投,每天收益立即到账消息推送","product_age":2}
{"index":{}}
{"productName":"理财产品D","annual_rate":"3.1200%","describe":"90天定投产品,最低12000起投,每天收益到账消息推送","product_age":1}
{"index":{}}
{"productName":"理财产品E","annual_rate":"3.0100%","describe":"30天定投产品推荐,最低8000起投,每天收益会消息推送","product_age":5}
{"index":{}}
{"productName":"理财产品F","annual_rate":"2.7500%","describe":"热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息","product_age":11}

1.3. 自定义/开源插件安装流程

  1. 参考阿里云官方文档:https://help.aliyun.com/zh/es/user-guide/upload-and-install-a-custom-plug-in?spm=a2c4g.11186623.0.i8
  2. 在集群详细信息配置界面依次点击如下按钮后,集群将处于生效中的变更状态,变更完成后即可使用
  1. 配置与管理
  2. 插件配置
  3. 自定义插件列表
  4. 上传
  5. 上传本地插件zip压缩包文件
  6. 上传安装

image.png

2. SQL插件

2.1. _sql summary

插件名称:NLPchina/elasticsearch-sql

参考开源地址:https://github.com/NLPchina/elasticsearch-sql/releases/tag/6.7.0.0

2.2. 使用与测试

image.png

版本不同GET请求的路径也有变更,目前我们使用的6.7.0版本需使用/_sql及/_sql/_explain

2.2.1. _sql查询

返回结果与DSL查询返回形式一致

GET _sql
{
  "sql":"select * from products_info where product_age > 0 limit 1,10"
}

image.png

2.2.2. _sql/_explain转换DSL

GET _sql/_explain
{
  "sql":"select * from products_info where product_age > 0 limit 1,10"
}
{
  "from": 1,
  "size": 10,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "range": {
                  "product_age": {
                    "from": 0,
                    "to": null,
                    "include_lower": false,
                    "include_upper": true,
                    "boost": 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}

2.2.3. select tpye

在不同业务使用场景中,历史业务逻辑存在对{index}/{_type}的使用,该开源插件支持识别并查询该场景下数据

GET _sql
{
  "sql":"select * from products_info/products where product_age > 0 limit 1,10"
}

image.png

2.3. 对比aliyun-sql

参考文档:https://help.aliyun.com/zh/es/user-guide/use-the-aliyun-sql-plug-in/?spm=a2c4g.11186623.0.0.f9a81cf3eA4hMC

2.3.1. 安装与开启

aliyun-sql默认安装在阿里云ElasticSearch集群插件中,需要通过如下命令开启使用,否则会报异常:

{
  "error": {
    "root_cause": [
      {
        "type": "access_denied_exception",
        "reason": "sql plugin is disabled!"
      }
    ],
    "type": "access_denied_exception",
    "reason": "sql plugin is disabled!"
  },
  "status": 500
}
PUT _cluster/settings
{
  "transient": {
    "aliyun.sql.enabled": true
  }
}

2.3.2. _alisql查询

  1. GET请求路径不同插件有所不同,如果冲突的话需要按需卸载或者disable
  2. body请求体的key不同,_alisql为"query"
GET /_alisql
{
  "query":"select * from products_info where product_age > 0 limit 1,10"
}

image.png

2.3.3. _alisql/transalte转换DSL

GET /_alisql/translate
{
  "query":"select * from products_info where product_age > 0 limit 1,10"
}

image.png

2.3.4. 不支持/_type的用法..?

  1. 部分用户从自建/其他云厂商迁移到阿里云ElasticSearch,从_sql切换到_alisql,使用select type的语法暂不支持
GET /_alisql/translate
{
  "query":"select * from products_info/products where product_age > 0 limit 1,10"
}
{
  "error": {
    "root_cause": [
      {
        "type": "sql_parse_exception",
        "reason": "Encountered \" \"/\" \"/ \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    "
      }
    ],
    "type": "sql_parse_exception",
    "reason": "Encountered \" \"/\" \"/ \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    ",
    "caused_by": {
      "type": "parse_exception",
      "reason": "Encountered \" \"/\" \"/ \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    "
    }
  },
  "status": 500
}
  1. 部分同学调整语法后发现可以正常运行,表现在替换"/"为"//"。但发现查询结果不符合预期
GET /_alisql/translate
{
  "query":"select * from products_info//products where product_age > 0 limit 1,10"
}
  1. 经过查看SQL转换成的DSL可以看出,alisql应该是将"//"视为了注释,后面的where查询,limit翻页,均没有在DSL中有所体现

image.png

  1. alisql在官网文档中查看并未开源,使用教程中也并未提到相关的语法,如有相关语句修改后正常执行的方案请各位大佬私聊指导

2.3.5. 不支持索引名中含有"-"..?

  1. 创建索引,索引名中含有"-"
PUT /products_info-1
{
  "mappings": {
    "products": {
      "properties": {
        "productName": {
          "type": "text",
          "analyzer": "ik_smart"
        },
        "annual_rate":{
          "type":"keyword"
        },
        "describe": {
         "type": "text",
         "analyzer": "ik_smart"
        },
        "product_age":{
          "type":"integer"
        }
      }
    }
  }
}
  1. 使用开源插件_sql和_alisql查询,_alisql异常如下
{
  "error": {
    "root_cause": [
      {
        "type": "sql_parse_exception",
        "reason": "Encountered \" \"-\" \"- \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    "
      }
    ],
    "type": "sql_parse_exception",
    "reason": "Encountered \" \"-\" \"- \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    ",
    "caused_by": {
      "type": "parse_exception",
      "reason": "Encountered \" \"-\" \"- \"\" at line 1, column 28.\nWas expecting:\n    <EOF> \n    "
    }
  },
  "status": 500
}

image.png

  1. alisql在官网文档中查看并未开源,使用教程中也并未提到相关的语法,如有相关语句修改后正常执行的方案请各位大佬私聊指导

3. 繁体/简体转换插件

3.1. summary

插件名称:medcl/elasticsearch-analysis-stconvert

参考开源地址:https://github.com/medcl/elasticsearch-analysis-stconvert/releases/tag/v6.7.0

3.2. 使用与测试

我们使用analyze的API对该analysis的插件进行使用测试。

analyze的API参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.7/indices-analyze.html#indices-analyze

  1. 该插件提供的analyzer、tokenizer、filter(原:token-filter)、char-filter默认为简体转为繁体,即Traditional->Simplified
GET /_analyze
{
  "text": ["我爱北京天安门Yeah"]
  , "tokenizer": "stconvert"
}

image.png

  1. 需求为繁体转换为简体,将默认的convert_type从「s2t」修改为「t2s」,并在filter中转换为小写
GET /_analyze
{
  "text": ["我愛北京天安門Yeah"]
  ,"tokenizer": {
    "type":"stconvert"
    ,"convert_type" : "t2s"
  }
  ,"filter": ["lowercase"]
}

image.png

  1. 也可以使用指定索引analyzer的方式自定义为繁体转简体。设置convert索引的analyzer为test_convert,并在stconvert的基础上指定convert_type为t2s。
DELETE /convert
PUT /convert
{
  "settings": {
    "analysis": {
      "analyzer": {
        "test_convert":{
          "type":"stconvert",
          "convert_type":"t2s"
        }
      }
    }
  }
}
GET /convert/_analyze
{
  "text": ["我愛北京天安門Yeah"]
  ,"analyzer": "test_convert"
}

image.png

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
3月前
|
运维 监控 Java
探索Elasticsearch在Java环境下的全文检索应用实践
【6月更文挑战第30天】在大数据背景下,Elasticsearch作为分布式搜索分析引擎,因其扩展性和易用性备受青睐。本文指导在Java环境中集成Elasticsearch,涉及安装配置、使用RestHighLevelClient连接、索引与文档操作,如创建索引、插入文档及全文检索查询。此外,还讨论了高级查询、性能优化和故障排查,帮助开发者高效处理非结构化数据全文检索。
124 0
|
24天前
|
存储 关系型数据库 MySQL
浅谈Elasticsearch的入门与实践
本文主要围绕ES核心特性:分布式存储特性和分析检索能力,介绍了概念、原理与实践案例,希望让读者快速理解ES的核心特性与应用场景。
|
25天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
1月前
|
人工智能 自然语言处理 搜索推荐
阿里云Elasticsearch AI搜索实践
本文介绍了阿里云 Elasticsearch 在AI 搜索方面的技术实践与探索。
18838 20
|
1月前
|
数据可视化 Docker 容器
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
这篇文章提供了通过Docker安装Elasticsearch和Kibana的详细过程和图解,包括下载镜像、创建和启动容器、处理可能遇到的启动失败情况(如权限不足和配置文件错误)、测试Elasticsearch和Kibana的连接,以及解决空间不足的问题。文章还特别指出了配置文件中空格的重要性以及环境变量中字母大小写的问题。
一文教会你如何通过Docker安装elasticsearch和kibana 【详细过程+图解】
|
1月前
|
JSON 自然语言处理 数据库
Elasticsearch从入门到项目部署 安装 分词器 索引库操作
这篇文章详细介绍了Elasticsearch的基本概念、倒排索引原理、安装部署、IK分词器的使用,以及如何在Elasticsearch中进行索引库的CRUD操作,旨在帮助读者从入门到项目部署全面掌握Elasticsearch的使用。
|
1月前
|
Ubuntu Oracle Java
如何在 Ubuntu VPS 上安装 Elasticsearch
如何在 Ubuntu VPS 上安装 Elasticsearch
18 0
|
1月前
|
存储 Ubuntu Oracle
在Ubuntu 14.04上安装和配置Elasticsearch的方法
在Ubuntu 14.04上安装和配置Elasticsearch的方法
29 0
|
1月前
|
存储 安全 Java
在CentOS 7上安装和配置Elasticsearch的方法
在CentOS 7上安装和配置Elasticsearch的方法
91 0
|
1月前
|
自然语言处理 Docker 容器
ElasticSearch 实现分词全文检索 - ES、Kibana、IK分词器安装
ElasticSearch 实现分词全文检索 - ES、Kibana、IK分词器安装
28 0

热门文章

最新文章

相关产品

  • 检索分析服务 Elasticsearch版