自学内容网 自学内容网

828华为云征文 | 解锁数据价值,使用华为云Flexusx与Solr构建智能搜索应用

前言

在数字化转型的浪潮中,数据已成为企业最宝贵的资产之一。如何高效、准确地解锁这些数据价值,成为众多企业面临的挑战。华为云Flexusx服务器以其卓越的性能、灵活的资源调度能力和强大的可扩展性,为企业提供了一个理想的平台。结合Apache Solr这一强大的开源搜索平台,企业能够在Flexusx上快速构建起智能搜索应用,实现对海量数据的即时索引与智能检索。Flexusx与Solr的融合,不仅提升了搜索效率与准确性,还降低了运维成本,为企业数据价值的深度挖掘与利用开辟了新路径在828华为云企业上云节之际,让我们一同探索这一创新解决方案,开启智能搜索的新篇章。

链接直达华为云Flexus云服务器X实例

在这里插入图片描述

Solr介绍

Apache Solr 是一款基于Apache Lucene的搜索服务器,Apache Lucene 是一个基于 Java 的开源信息检索库。Solr 旨在驱动强大的文档检索或分析应用程序,这些应用程序涉及非结构化数据、半结构化数据或非结构化和结构化数据的混合。它还为有限的关系、图形、统计、数据分析或存储相关用例提供辅助支持。

官方网站:https://solr.apache.org/

官方文档:https://solr.apache.org/guide/solr/latest/index.html

在这里插入图片描述

部署流程

拉取镜像

[root@flexusx-251f ~]# docker pull solr:8.0.0
8.0.0: Pulling from library/solr
67e8aa6c8bbc: Pull complete
627e6c1e1055: Pull complete
0670968926f6: Pull complete
65e458027cc2: Pull complete
6e07b100903d: Pull complete
bbf92622a61b: Pull complete
4a986d565709: Pull complete
09c2bad786ea: Pull complete
41f6bba18f53: Pull complete
05670d30e8db: Pull complete
d94c25f7ce44: Pull complete
Digest: sha256:456f074bc4bd4c78b6e9322c372664b2db389caeaad3f50db864a27c70064354
Status: Downloaded newer image for solr:8.0.0
docker.io/library/solr:8.0.0

创建主机挂载目录运行 solr

[root@flexusx-251f ~]# mkdir /solrdata

编写 docker-compose.yaml文件

[root@flexusx-251f solrdata]# vim docker-compose.yaml
[root@flexusx-251f solrdata]# cat docker-compose.yaml
version: '3'
services:
  solr:
    image: solr:8.0.0
    container_name: my_solr
    ports:
     - "8983:8983"
    volumes:
      - data:/var/solr
    command:
      - solr-precreate
      - gettingstarted
volumes:
  data:

运行容器并查看

[root@flexusx-251f solrdata]# docker-compose up -d
[+] Running 2/2
 ✔ Container solrdata-solr-1  Recreated                                    0.5s
 ✔ Container my_solr          Started                                      0.1s
 
[root@flexusx-251f solrdata]# docker-compose ps
NAME      IMAGE        COMMAND                  SERVICE   CREATED         STATUS         PORTS
my_solr   solr:8.0.0   "docker-entrypoint.s…"   solr      8 seconds ago   Up 7 seconds   0.0.0.0:8983->8983/tcp, :::8983->8983/tcp

配置安全组,放行端口 8983

在这里插入图片描述

在 Web 浏览器查看http://localhost:8983/

在这里插入图片描述

在 UI 中,单击“核心管理”,现在应该会看到“gettingstarted”核心。

在这里插入图片描述

快速上手

创建核心

当 Solr 以独立模式运行时,可以创建“核心”来存储数据

手动创建核心(也可以在webUI界面进行手动创建)

[root@flexusx-251f solrdata]# docker exec -it my_solr solr create_core -c gettingstarted001

UI管理界面可以进行查看

在这里插入图片描述

创建集合

在“SolrCloud”集群中,可以创建“集合”来存储数据

  • 创建集合的第一种方法是转到Solr 管理 UI,从左侧导航菜单中选择“集合”,然后按“添加集合”按钮,为其命名,选择配置集_default,然后按“添加集合”按钮。
  • 第二种方式是通过其中一个容器上的 Solr 控制脚本
[root@flexusx-251f solrdata]# docker exec my_solr solr create -c gettingstarted2
WARNING: Using _default configset with data driven schema functionality. NOT RECOMMENDED for production use.
         To turn off: bin/solr config -c gettingstarted2 -p 8983 -action set-user-property -property update.autoCreateFields -value false
INFO  - 2024-09-22 13:31:36.789; org.apache.solr.util.configuration.SSLCredentialProviderFactory; Processing SSL Credential Provider chain: env;sysprop

Created new core 'gettingstarted2'

在这里插入图片描述

定义架构Define a schema

定义文档包含的一些字段

curl --request POST \
  --url http://localhost:8983/api/collections/techproducts/schema \
  --header 'Content-Type: application/json' \
  --data '{
  "add-field": [
    {"name": "name", "type": "text_general", "multiValued": false},
    {"name": "cat", "type": "string", "multiValued": true},
    {"name": "manu", "type": "string"},
    {"name": "features", "type": "text_general", "multiValued": true},
    {"name": "weight", "type": "pfloat"},
    {"name": "price", "type": "pfloat"},
    {"name": "popularity", "type": "pint"},
    {"name": "inStock", "type": "boolean", "stored": true},
    {"name": "store", "type": "location"}
  ]
}'

索引一些文档

单个文档可以被索引

curl --request POST \
  --url 'http://localhost:8983/api/collections/techproducts/update' \
  --header 'Content-Type: application/json' \
  --data '  {
    "id" : "978-0641723445",
    "cat" : ["book","hardcover"],
    "name" : "The Lightning Thief",
    "author" : "Rick Riordan",
    "series_t" : "Percy Jackson and the Olympians",
    "sequence_i" : 1,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 12.50,
    "pages_i" : 384
  }'

可以在同一个请求中对多个文档进行索引

 curl --request POST \
  --url 'http://localhost:8983/api/collections/techproducts/update' \
  --header 'Content-Type: application/json' \
  --data '  [
  {
    "id" : "978-0641723445",
    "cat" : ["book","hardcover"],
    "name" : "The Lightning Thief",
    "author" : "Rick Riordan",
    "series_t" : "Percy Jackson and the Olympians",
    "sequence_i" : 1,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 12.50,
    "pages_i" : 384
  }
,
  {
    "id" : "978-1423103349",
    "cat" : ["book","paperback"],
    "name" : "The Sea of Monsters",
    "author" : "Rick Riordan",
    "series_t" : "Percy Jackson and the Olympians",
    "sequence_i" : 2,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 6.49,
    "pages_i" : 304
  }
]'

包含文档的文件可以按如下方式进行索引

curl -H "Content-Type: application/json" \
       -X POST \
       -d @example/exampledocs/books.json \
       --url 'http://localhost:8983/api/collections/techproducts/update?commit=true'

提交更改

文档被索引到集合后,它们不会立即可供搜索。为了使它们可搜索,refresh需要执行提交操作(在其他搜索引擎中也称为 OpenSearch 等)。可以使用自动提交定期安排提交,如下所示。

curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' http://localhost:8983/api/collections/techproducts/config

体验和感受

828华为云企业上云节的契机下,我有幸见证了Flexusx服务器与Solr搜索引擎的强强联合,共同解锁了数据价值的无限可能。Flexusx以其卓越的计算能力和灵活的资源配置,为Solr提供了强大的后端支持,使得搜索响应速度显著提升,数据处理能力更是跃升至新高度。这种部署模式不仅优化了搜索结果的准确性和相关性,还实现了对海量数据的快速索引与高效检索。体验过程中,我深刻感受到了智能搜索应用带来的便捷与高效,仿佛为数据海洋装上了导航灯,让信息的获取变得触手可及。这无疑是对华为云技术创新能力和服务品质的又一次有力证明。

产品链接华为云Flexus云服务器X实例_直播_建站服务器-华为云

在这里插入图片描述


原文地址:https://blog.csdn.net/weixin_62782025/article/details/142455656

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!