Apache Solr入门教程

Apache Solr支持不同格式,例如数据库,PDF文件,XML文件,CSV文件。

为什么选择Apache Solr

Apache Solr是搜索服务器,提供REST风格API。
Solr基于Lucene。
使用 Apache Zookeeper针对高流量进行优化。

Solr功能

先进的全文搜索功能。
XML,JSON和HTTP – 基于开放接口标准。
高度可扩展和容错。
同时支持模式和无模式配置。
分页搜索和过滤。
支持许多主要语言
丰富的文档。

安装Apache Solr

《Apache Solr入门教程》

bin:启动和停止服务器的脚本。
example: 示例。
server/logs 文件夹,所有Solr日志都写入该文件夹。
server/solr文件夹包含不同的集合或核心(core/collection)。对于各集合或核心的配置和数据都存储在相应的集合或核心文件夹。

Solr内置Jetty服务器

启动Solr

solr start

《Apache Solr入门教程》

默认为端口8983。
http://localhost:8983/solr/

《Apache Solr入门教程》

配置Apache Solr

Apache Solr附带无模式模式选项。这个选项允许用户构建有效的架构,而无需手动编辑模式文件。

建立核心(core)

Solr服务器在独立模式下启动的配置称为核心,
在SolrCloud模式启动的配置称为集合。

首先,创建一个核心的索引数据。
solr create
-c <name>:要创建的核心或集合的名称(必需)。
-d <confdir>:配置目录,在SolrCloud模式非常有用。
-n <configName>:配置名称。这将默认为核心或集合的名称。
-p <port>:本地Solr的实例的端口发送create命令; 默认脚本试图通过寻找运行Solr的实例来检测端口。
-s <shards>:Number of shards to split a collection into, default is 1.
-rf <replicas>:集合中的每个文件的份数。默认值是1。

使用核心名称和配置目录-d参数-c参数。对于所有其它参数使用默认设置。

solr create -c jcg -d basic_configs

《Apache Solr入门教程》

JCG核心被填充在核心选择器上。

《Apache Solr入门教程》

修改Schema.xml文件

《Apache Solr入门教程》

子文件夹conf和data分别保存核心配置和索引数据。

在solr-6.3.0\server\solr\jcg\conf\managed-schema中添加如下内容:

    <field name="cat" type="text_general" indexed="true" stored="true"/>
    <field name="name" type="text_general" indexed="true" stored="true"/>
    <field name="price" type="double" indexed="true" stored="true"/>
    <field name="inStock" type="boolean" indexed="true" stored="true"/>
    <field name="author" type="text_general" indexed="true" stored="true"/> 

《Apache Solr入门教程》

indexedtrue指定字段被索引。
stored指定字段是否被存储。

修改配置必须重启服务器。

solr stop -all

重启服务:

solr start

索引数据

Apache Solr自带SimplePostTool的程序。

《Apache Solr入门教程》

C:\solr-6.3.0\example\exampledocs>java -jar post.jar -h

《Apache Solr入门教程》

Usage: java [SystemProperties] -jar post.jar [-h|-] [<file|folder|url|arg> [<file|folder|url|arg>...]]

索引数据:

java -Dtype=text/csv -Durl=http://localhost:8983/solr/jcg/update  -jar post.jar   books.csv

-dtype – 数据文件的类型。
-Durl – JCG核心的地址。

《Apache Solr入门教程》

导航到以下网址并选择核心JCG:

《Apache Solr入门教程》

按名称搜索

http://localhost:8983/solr/jcg/select?q=name:”A Clash of Kings”

《Apache Solr入门教程》

首字母搜索

http://localhost:8983/solr/jcg/select?q=name:”A”

《Apache Solr入门教程》

使用通配符

http://localhost:8983/solr/jcg/select?q=name:”*of”

《Apache Solr入门教程》

使用条件

如何查询价格低于¥6的书。
http://localhost:8983/solr/jcg/select?q=*&fq=price:[0 TO 6]

《Apache Solr入门教程》

Solr的客户端API的

有不同的客户端API的可用来连接到Solr的服务器。
SolRuby – To connect from Ruby
SolPHP – To connect from PHP
PySolr – To connect from Python
SolPerl – To connect from Perl
SolrJ – To connect from Java
SolrSharp – To connect from C#

进行检索

《Apache Solr入门教程》

    原文作者:每天学点编程
    原文地址: https://www.jianshu.com/p/1b725a783d50
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞