• 【0-1系列】从0-1快速了解搜索引擎Scope以及如何快速安装使用(下)


    前言

    近日,社区版家族正式发布V2024.5版本,其中,社区开发版系列重磅发布Scope开发版以及StellarDB开发版。

    为了可以让大家更进一步了解产品,本系列文章从背景概念开始介绍,深入浅出的为读者介绍Scope的优势以及能力,在上一篇文章中为读者介绍了基础知识、Scope的技术优势以及能力,本篇文章将继续为读者介绍如何安装部署以及使用。

    友情链接

    安装与部署

    企业版(分布式)

    社区订阅版(分布式)

    社区开发版(单机)

    安装教程

    友情提示:安装前请仔细查看安装手册注意事项章节,下方内容仅供参考

    步骤一 将从官网下载下来的产品包解压后上传至安装环境

    • 产品包名称:TDH-Scope-Standalone-Community-Transwarp-2024.5-X86_64-final.tar.gz

    步骤二 执行下述命令进行解压,解压后将出现一个镜像tar包

    tar -zxf TDH-Scope-Standalone-Community-Transwarp-2024.5-X86_64-final.tar.gz

    步骤三 执行下述命令加载镜像

    docker load -i scope-2024.5.tar

    步骤四 执行下方指令启动容器并运行镜像,运行格式为:

    docker run -d --network host -v <本地目录路径>:/opt/transwarp --privileged <镜像名>

    -v参数配置了TDH挂载的本地磁盘路径。该路径下会保存产品运行过程中产生的配置conf、数据data、日志log。再次提醒请不要随意改动做好备份,以及确保该路径下没有历史版本的数据文件。

    操作示例图

    image.png

    步骤五 容器启动后需等待30s至2分钟

    步骤六 浏览器访问管理节点8180端口

    打开客户端浏览器(推荐使用Google Chrome浏览器),访问http://host:8180,比如http://172.16.3.108:8180/。访问这个地址,您会看到下面的登录页面。

    image.png

    初次登录以admin的身份登录,密码也是admin。

    步骤七 按照向导提示进行集群部署与配置即可

    manager3.1_24sc.png

    manager4.1_24sc.png

    manager5.1_24sc.png

    manager6.1_24sc.png

    manager7.1_24sc.png

    manager8.1_24sc.png

    manager10.1_24sc.png

    manager11.1_24sc.png

    安装完成自助申请许可证即可使用,教程请参考手册

    使用示例

    产品使用手册

    演示示例1. Scope多态语法之rest 语法

    创建/删除索引

    创建索引
    1. curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'{
    2. "settings": {
    3. "number_of_shards": 5,
    4. "number_of_replicas": 3
    5. },
    6. "mappings": {
    7. "default_type_": {
    8. "properties": {
    9. "id": { "type": "integer" },
    10. "title": { "type": "text" },
    11. "body": { "type": "text" },
    12. "date": { "type": "date" },
    13. "views": { "type": "integer" },
    14. "tags": { "type": "keyword" }
    15. }
    16. }
    17. }
    18. }
    19. ';
    删除索引
    curl -X DELETE "localhost:9200/my_index?pretty";

    数据插入

    单条插入
    1. curl -X PUT "localhost:9200/my_index/default_type_/1?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "id": 1,
    4. "title": "Scope for Beginners",
    5. "body": "Learn how to use Scope to search and analyze your data",
    6. "date": "2022-05-09",
    7. "views": 1000,
    8. "tags": ["Scope", "search"]
    9. }
    10. ';
    11. curl -X POST "localhost:9200/my_index/default_type_/?pretty" -H 'Content-Type: application/json' -d'
    12. {
    13. "id": 2,
    14. "title": "Advanced Scope",
    15. "body": "Take your Scope skills to the next level",
    16. "date": "2022-05-10",
    17. "views": 500,
    18. "tags": ["Scope", "advanced"]
    19. }
    20. ';
    批量插入
    1. curl -X POST "localhost:9200/my_index/_bulk?pretty" -H 'Content-Type: application/x-ndjson' --data-binary '
    2. { "index" : {"_index" : "my_index","_type" : "default_type_", "_id" : "bulk_1" } }
    3. { "id": 3, "title": "Scope Performance Tuning", "body": "Optimize your Scope cluster for better performance", "date": "2022-05-11","views": 750, "tags": ["Scope", "performance"]}
    4. { "index" : {"_index" : "my_index","_type" : "default_type_", "_id" : "bulk_2" } }
    5. { "id": 4, "title": "Scope Security", "body": "Learn how to secure your Scope cluster", "date": "2022-05-12", "views": 250, "tags": ["Scope", "security"]}
    6. { "index" : {"_index" : "my_index","_type" : "default_type_", "_id" : "bulk_3" } }
    7. {"id": 5, "title": "Scope Monitoring", "body": "Monitor your Scope cluster with the Elastic Stack", "date": "2022-05-13", "views": 100, "tags": ["Scope", "monitoring"]}
    8. ';

    数据查询

    Case1
    1. curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "query": {
    4. "match": {
    5. "body": "Scope"
    6. }
    7. },
    8. "sort": {
    9. "date": { "order": "desc" }
    10. }
    11. }
    12. ';
    Case2
    1. curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d'
    2. {
    3. "query": {
    4. "range": {
    5. "date": {
    6. "gte": "2022-05-11"
    7. }
    8. }
    9. },
    10. "sort": {
    11. "views": { "order": "asc" }
    12. }
    13. }
    14. ';

    基础运维指令

    查看集群状态
    curl -X GET "localhost:9200/_cluster/health?pretty";
    查看索引状态
    curl -X GET "localhost:9200/_cat/indices/my_index?v";

    查看节点状态

    curl -X GET "localhost:9200/_cat/nodes?v";
    查看分片状态
    curl -X GET "localhost:9200/_cat/shards/my_index?v";

    演示示例2. Scope多态语法之sql语法

    创建/删除索引

    删除/创建数据库(非必要)
    1. drop database if exists DDL_Scope_DB CASCADE;
    2. create database if not exists DDL_Scope_DB;
    3. use DDL_Scope_DB;
    删除索引
    drop table if exists sql_demo;
    创建索引
    1. create table sql_demo(
    2. id string,
    3. title string has analyzer 'standard',
    4. author string,
    5. price double,
    6. description string has analyzer 'mmseg'
    7. )stored as scope
    8. with shard number 5 replication 3
    9. tblproperties('scope.key.column'='id');

    数据插入

    单条插入
    1. insert into sql_demo select '1', '百年孤独', '加西亚·马尔克斯', 39.80, '一部代表魔幻现实主义文学巅峰的经典小说。' from system.dual;
    2. insert into sql_demo values('2', '围城', '钱钟书', 29.80, '一部中国现代文学经典,讽刺了旧中国知识分子的冷嘲热讽和无可奈何。');
    批量插入
    1. batchinsert into sql_demo batchvalues(
    2. values('3', '骆驼祥子', '老舍', 22.80, '一部反映旧中国社会底层生活的文学作品,展现了社会底层人民的生活和奋斗。'),
    3. values('4', '茶花女', '小仲马', 18.80, '一部法国浪漫主义文学代表作,描绘了一个上层社会女性的生活和爱情。')
    4. );

    数据查询

    数据查询
    1. select * from sql_demo;
    2. select * from sql_demo order by price;
    3. select * from sql_demo where contains(description,'中国');
    和 rest 一致性
    1. curl -ushiva:shiva -X GET "localhost:9200/ddl_scope_db.sql_demo/_mapping?pretty";
    2. curl -ushiva:shiva -X GET "local:9200/ddl_scope_db.sql_demo/_settings/?pretty&filter_path=**.number_of_shards,**.number_of_replicas";
    3. curl -ushiva:shiva -X GET "local:9200/ddl_scope_db.sql_demo/_search?pretty";

    演示示例3. Scope多态语法之JAVA语法

    创建索引

    1. CreateIndexRequest request = new CreateIndexRequest("create_index_demo");
    2. request.settings(Settings.builder()
    3. .put("index.number_of_shards", 1)
    4. .put("index.number_of_replicas", 1)
    5. );
    6. CreateIndexRequest indexRequest = request.mapping(
    7. " {\n" +
    8. " \"" + "default_type_" + "\": {\n" +
    9. " \"properties\": {\n" +
    10. " \"c_text\": {\n" +
    11. " \"type\": \"text\"\n" +
    12. " },\n" +
    13. " \"c_string_mf\": {\n" +
    14. " \"type\": \"keyword\"\n" +
    15. " }\n" +
    16. " }\n" +
    17. " }\n" +
    18. " }",
    19. XContentType.JSON);
    20. CreateIndexResponse createIndexResponse = highLevelClient.indices().create(indexRequest,RequestOptions.DEFAULT);

    删除索引

    1. DeleteIndexRequest request = new DeleteIndexRequest("create_index_demo");
    2. AcknowledgedResponse deleteIndexResponse = highLevelClient.indices().delete(request,RequestOptions.DEFAULT);

    数据插入

    单条插入
    1. IndexRequest request = new IndexRequest(
    2. "my_index",
    3. "default_type_",
    4. "6");
    5. String jsonString = "{\n" +
    6. " \"id\": 6,\n" +
    7. " \"title\": \"Scope for Beginners\",\n" +
    8. " \"body\": \"Learn how to use Scope to search and analyze your data\",\n" +
    9. " \"date\": \"2022-05-09\",\n" +
    10. " \"views\": 1000,\n" +
    11. " \"tags\": [\"Scope\", \"search\"]\n" +
    12. "}";
    13. IndexRequest source = request.source(jsonString, XContentType.JSON);
    14. IndexResponse index = highLevelClient.index(source, RequestOptions.DEFAULT);
    批量插入
    1. String jsonString1 = "{\"id\": 3, \"title\": \"Scope Performance Tuning\", \"body\": \"Optimize your Scope cluster for better performance\", \"date\": \"2022-05-11\",\"views\": 750, \"tags\": [\"Scope\", \"performance\"]}";
    2. String jsonString2 = "{\"id\": 4, \"title\": \"Scope Security\", \"body\": \"Learn how to secure your Scope cluster\", \"date\": \"2022-05-12\", \"views\": 250, \"tags\": [\"Scope\", \"security\"]}";
    3. String jsonString3 = "{\"id\": 5, \"title\": \"Scope Monitoring\", \"body\": \"Monitor your Scope cluster with the Elastic Stack\", \"date\": \"2022-05-13\", \"views\": 100, \"tags\": [\"Scope\", \"monitoring\"]}";
    4. BulkRequest request = new BulkRequest();
    5. request.add(new IndexRequest("my_index", "default_type_", "3").source(jsonString1,XContentType.JSON));
    6. request.add(new IndexRequest("my_index", "default_type_", "4").source(jsonString2,XContentType.JSON));
    7. request.add(new IndexRequest("my_index", "default_type_", "5").source(jsonString3,XContentType.JSON));
    8. BulkResponse bulkResponses = highLevelClient.bulk(request,RequestOptions.DEFAULT);

    数据查询

    term查询
    1. SearchRequest searchRequest = new SearchRequest().indices("my_index").types("default_type_");
    2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    3. searchSourceBuilder.query(QueryBuilders.termQuery("body", "how"));
    4. searchRequest.source(searchSourceBuilder);
    5. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    match查询(对查询条件进行分词)
    1. SearchRequest searchRequest = new SearchRequest().indices("my_index").types("default_type_");
    2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    3. searchSourceBuilder.query(QueryBuilders.matchQuery("body","Monitor Optimize"));
    4. searchRequest.source(searchSourceBuilder);
    5. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    boolean查询(多条件查询)
    1. SearchRequest searchRequest = new SearchRequest().indices("my_index").types("default_type_");
    2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    3. SearchSourceBuilder mustQuery = searchSourceBuilder.query(QueryBuilders.boolQuery());
    4. mustQuery.query(QueryBuilders.termQuery("body","cluster"));
    5. mustQuery.query(QueryBuilders.termQuery("date","2022-05-13"));
    6. searchRequest.source(mustQuery);
    7. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    演示示例4. 索引与分词机制

    在使用搜索引擎的过程中,通常会涉及诸多属于,如segment/doc/term/token/shard/index等等,其中,segment/doc/term/token都是lucene中的概念。理解这些术语有助于更深入的了解和使用搜索引擎。

    • document:索引和搜索的主要数据载体,对应写入scope中的一个doc。通常以JSON格式存储;
    • field:document中的各个字段,每个字段都有自己的数据类型,使用者可以针对字段内容设置是否对其分词或使用分析器进行分词;
    • term词项:搜索时的一个单位,代表文本中的某个词;
    • token词条:词项(term)在字段(field)中的一次出现,包括词项的文本、开始和结束的位移、类型等信息;
    • index索引:以index为单位组织数据(document),一个index中的数据通常具有相似的特征。需要注意这里的index与宽表数据库中的索引(全局索引)不是一个概念,这里指的是搜索引擎中的数据对象。

    lucene内部使用的是倒排索引的数据结构,将词项(term)映射到文档(document)。例如下图的3个document,进行分词后可以搜索引擎可以很快速的返回的下方问题的答案

    • 查询document id为 2的document?
    • 查询包含choice的document?
    • 查询有choice又有is的document?

    image.png

    那么是如何实现这一能力的?

    分词器介绍

    将文档切分成一系列有意义的单词(term/token)的过程称之为分词,其中,分词器则负责这一过程,以建立索引进行高效的搜索和分析。

    选择一个合适的分词器可以很大程度上提高检索效率,当前比较常见的分词器有以下几种:

    英文分词器
    • 标准分词器(Standard Tokenizer)标准分词器类型是standard,用于大多数欧洲语言,使用Unicode文本分割算法对文档进行分词;
    • 空格分词器(Whitespace Tokenizer)空格分词类型是whitespace,在空格处分割文本;
    • 小写分词器(Lowercase Tokenizer)小写分词器类型是lowercase,在非字母位置上分割文本,并把分词转换为小写形式,功能上是Letter Tokenizer和 Lower Case Token Filter的结合(Combination),但是性能更高,一次性完成两个任务;
    • 经典分词器(Classic Tokenizer)经典分词器类型是classic,基于语法规则对文本进行分词,对英语文档分词非常有用,在处理首字母缩写,公司名称,邮件地址和Internet主机名上效果非常好;
    • ...
    中文分词器
    • IK:普及率最广。IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和最大词长两种切分模式;支持英文字母、数字、中文词汇等分词处理。支持用户词典扩展定义。
    • Pinyin 分词器:pinyin分词器可以让用户输入拼音,就能查找到相关的关键词。比如在某个商城搜索中,输入yonghui,就能匹配到永辉。这样的体验还是非常好的。
    • ...

    不同的分词器会产生不同的分词结果,产生不同的索引,所以相同的查询条件会产生不同的结果。

    举例说明生活中全文检索的应用实例:字典。字典的拼音表和部首检字表就相当于字典的索引,对每一个字的解释是非结构化的,如果字典没有音节表和部首检字表,在茫茫辞海中找一个字只能顺序扫描。然而字的某些信息可以提取出来进行结构化处理,比如读音,就比较结构化,分声母和韵母,分别只有几种可以一一列举,于是将读音拿出来按一定的顺序排列,每一项读音都指向此字的详细解释的页数。我们搜索时按结构化的拼音搜到读音,然后按其指向的页数,便可找到我们的非结构化数据——也即对字的解释。

    全文检索就是把文本中的内容拆分成若干个关键词,然后根据关键词创建索引。查询时,根据关键词查询索引,最终找到包含关键词的文章。整个过程类似于查字典的过程。

    Scope分词器-Standard

    创建索引
    1. curl -X DELETE "localhost:19200/blog_index1?pretty";
    2. curl -X PUT "localhost:19200/blog_index1?pretty" -H 'Content-Type: application/json' -d'{
    3. "mappings": {
    4. "default_type_": {
    5. "properties": {
    6. "id": {
    7. "type": "integer"
    8. },
    9. "blog_name": {
    10. "type": "text",
    11. "analyzer": "standard"
    12. },
    13. "blog_name_english": {
    14. "type": "text",
    15. "analyzer": "standard"
    16. }
    17. }
    18. }
    19. }
    20. }
    21. ';
    数据写入
    1. curl -X POST "localhost:19200/blog_index1/_bulk?pretty" -H 'Content-Type: application/x-ndjson' --data-binary '
    2. { "index" : {"_index" : "blog_index1","_type" : "default_type_", "_id" : "bulk_1" } }
    3. { "id": 1, "blog_name": "Scope 介绍", "blog_name_english": "Introduction to Scope"}
    4. { "index" : {"_index" : "blog_index1","_type" : "default_type_", "_id" : "bulk_2" } }
    5. { "id": 2, "blog_name": "Scope 高级搜索", "blog_name_english": "Advanced Searching in Scope"}
    6. { "index" : {"_index" : "blog_index1","_type" : "default_type_", "_id" : "bulk_3" } }
    7. { "id": 3, "blog_name": "全文检索技术比较", "blog_name_english": "Comparison of Full-Text Search Technologies"}
    8. { "index" : {"_index" : "blog_index1","_type" : "default_type_", "_id" : "bulk_4" } }
    9. { "id": 4, "blog_name": "Scope 数据聚合", "blog_name_english": "Scope Data Aggregation"}
    10. { "index" : {"_index" : "blog_index1","_type" : "default_type_", "_id" : "bulk_4" } }
    11. { "id": 5, "blog_name": "分布式数据库架构", "blog_name_english": "Introduction to NoSQL Databases"}
    12. ';
    英文检索
    1. curl -XGET "localhost:19200/blog_index1/_search?pretty" -H 'Content-Type: application/json' -d'{
    2. "query": {
    3. "term": {
    4. "blog_name_english": "search"
    5. }
    6. }
    7. }';
    中文检索
    1. curl -XGET "localhost:19200/blog_index1/_search?pretty" -H 'Content-Type: application/json' -d'{
    2. "query": {
    3. "term": {
    4. "blog_name": "搜索"
    5. }
    6. }
    7. }';

    Scope分词器-ik_max_word

    创建索引
    1. curl -X DELETE "localhost:9200/blog_index2?pretty";
    2. curl -X PUT "localhost:9200/blog_index2?pretty" -H 'Content-Type: application/json' -d'{
    3. "mappings": {
    4. "default_type_": {
    5. "properties": {
    6. "id": {
    7. "type": "integer"
    8. },
    9. "blog_name": {
    10. "type": "text",
    11. "analyzer": "ik_max_word"
    12. },
    13. "blog_name_english": {
    14. "type": "text",
    15. "analyzer": "standard"
    16. },
    17. "blog_summary": {
    18. "type": "text",
    19. "analyzer": "ik_max_word"
    20. }
    21. }
    22. }
    23. }
    24. }
    25. ';
    数据写入
    1. curl -X POST "localhost:9200/blog_index2/_bulk?pretty" -H 'Content-Type: application/x-ndjson' --data-binary '
    2. { "index" : {"_index" : "blog_index2","_type" : "default_type_", "_id" : "bulk_1" } }
    3. { "id": 1, "blog_name": "Scope 介绍", "blog_name_english": "Introduction to Scope", "blog_summary": "Scope 是一个分布式搜索引擎,用于快速和可扩展地搜索和分析大量数据。"}
    4. { "index" : {"_index" : "blog_index2","_type" : "default_type_", "_id" : "bulk_2" } }
    5. { "id": 2, "blog_name": "Scope 高级搜索", "blog_name_english": "Advanced Searching in Scope", "blog_summary": "学习如何使用 Scope 进行高级搜索和优化查询性能的技巧。"}
    6. { "index" : {"_index" : "blog_index2","_type" : "default_type_", "_id" : "bulk_3" } }
    7. { "id": 3, "blog_name": "全文检索技术比较", "blog_name_english": "Comparison of Full-Text Search Technologies", "blog_summary": "比较不同全文检索技术之间的性能和功能,了解它们在搜索算法和评估方面的优缺点。"}
    8. { "index" : {"_index" : "blog_index2","_type" : "default_type_", "_id" : "bulk_4" } }
    9. { "id": 4, "blog_name": "Scope 数据聚合", "blog_name_english": "Scope Data Aggregation", "blog_summary": "使用 Scope 进行数据聚合和分析,了解如何从大量数据中提取有价值的信息。"}
    10. { "index" : {"_index" : "blog_index2","_type" : "default_type_", "_id" : "bulk_4" } }
    11. { "id": 5, "blog_name": "分布式数据库架构", "blog_name_english": "Introduction to NoSQL Databases", "blog_summary": "探索分布式数据库架构的原理和设计,了解如何在分布式环境中实现数据一致性和高可用性。"}
    12. ';
    英文检索
    1. curl -XGET "localhost:9200/blog_index2/_search?pretty" -H 'Content-Type: application/json' -d'{
    2. "query": {
    3. "term": {
    4. "blog_name_english": "search"
    5. }
    6. }
    7. }';
    中文检索
    1. curl -XGET "localhost:9200/blog_index2/_search?pretty" -H 'Content-Type: application/json' -d'{
    2. "query": {
    3. "term": {
    4. "blog_name": "搜索"
    5. }
    6. }
    7. }';
    8. curl -XGET "localhost:9200/blog_index2/_search?pretty" -H 'Content-Type: application/json' -d'{
    9. "query": {
    10. "term": {
    11. "blog_summary": "搜索"
    12. }
    13. }
    14. }';

    更多使用教程可以参考 Scope使用手册 ,欢迎体验开发版Scope。

  • 相关阅读:
    Spring之日志
    ENSP网络综合实验(WALN+隧道+NAT)
    前端登录加密解密方法
    数字时代云边端如何协同,听听天翼云怎么说
    Spring Boot配置文件(YAML & Properties)总结
    python---匿名函数应用
    Element Plus el-select选择框失去焦点blur
    方舟:生存进化官服和私服区别
    0x00 Spring Boot 3入门学习教程大纲
    你真的了解JAVA中对象和类、this、super和static关键字吗
  • 原文地址:https://blog.csdn.net/tiancaidddddd/article/details/139827230