初学者Apache Solr教程
本文英文版地址:https://examples.javacodegeeks.com/enterprise-java/apache-solr/apache-solr-tutorial-beginners/
作者姓名:Veeramani Kalyanasundaram
作者简介:Veera是在电信领域工作的软件架构师,拥有丰富的Java中间件技术经验。 他是一名OOAD从业者,对性能工程感兴趣。
翻译备注:本文采用谷歌翻译,请有时间的同学可以从英文版学习。
在Apache Solr Tutorial for Beginners示例中,我们将讨论如何安装最新版本的Apache Solr并告诉您如何配置它。此外,我们将向您展示如何使用示例数据文件执行索引。 Apache Solr支持从不同的源格式索引,包括各种数据库,PDF文件,XML文件,CSV文件等。在这个例子中,我们将研究如何从CSV文件索引数据。
我们首选的环境是Windows。 在开始Solr安装之前,请确保已安装JDK并已正确设置Java_Home。
为什么是Apache Solr
Apache Solr是一个强大的搜索服务器,它支持像API一样的REST。 Solr由Lucene提供支持,可以实现强大的匹配功能,例如短语,通配符,连接,分组和更多的各种数据类型。 它是高度优化的高流量使用Apache Zookeeper。 Apache Solr提供了大量的功能,我们列出了一个高影响功能的子集。
- 高级全文搜索功能。
- 基于开放接口的标准 - XML,JSON和Http。
- 高度可扩展和容错。
- 支持Schema和Schemaless配置。
- 分面搜索和过滤。
- 支持主要语言,如英语,德语,中文,日语,法语等
- 富文档解析。
2.安装Apache Solr
首先,从以下位置下载最新版本的Apache Solr:
http://lucene.apache.org/solr/downloads.html
截至本文,稳定版本可用是5.0.0。 Apache Solr已经经历了从4.xx到5.0.0的各种更改,因此如果您有不同版本的Solr,则需要下载5.xx版本以遵循此示例。
下载Solr zip文件后,将其解压缩到文件夹中。 提取的文件夹将如下所示。
Solr文件夹
bin
文件夹包含用于启动和停止服务器的脚本。 example
文件夹包含几个示例文件。 我们将使用其中一个来演示Solr如何对数据进行索引。server
文件夹包含用于写入所有Solr日志的logs
文件夹。在索引期间检查日志中的任何错误将有所帮助。 服务器下的solr
文件夹包含不同的集合或核心。 每个核/集合的配置和数据存储在相应的核/集合文件夹中。
Apache Solr附带了一个内置的Jetty服务器。 但在我们启动solr实例之前,我们必须验证JAVA_HOME是否在机器上设置。
我们可以使用命令行脚本启动服务器。 让我们从命令提示符转到bin目录并发出以下命令
solr start
这将在默认端口8983下启动Solr服务器。
我们现在可以在浏览器中打开以下URL,并验证我们的Solr实例正在运行。 solr管理工具的具体细节超出了示例的范围。
http://localhost:8983/solr/
Solr管理控制台
3.配置Apache Solr
在本节中,我们将向您展示如何配置solr实例的核心/集合以及如何定义字段。 Apache Solr附带一个名为Schemaless模式的选项。 此选项允许用户构建有效的模式,而无需手动编辑模式文件。但是对于这个例子,我们将使用Schema配置来理解Solr的内部。
3.1创建核心
当Solr服务器在独立模式下启动时,配置称为核心,当它在SolrCloud模式下启动时,配置称为集合。在这个例子中,我们将讨论独立的服务器和核心。 我们将暂停SolrCloud讨论以供日后使用。
首先,我们需要创建一个用于索引数据的Core。 Solr create命令有以下选项:
- -c <name> - 要创建的核心或集合的名称(必需)。
- -d <confdir> - 配置目录,在SolrCloud模式下有用。
- -n <configName> - 配置名称。 此默认值与核心或集合的名称相同。
- -p <port> - 要向其发送create命令的本地Solr实例的端口;默认情况下,脚本尝试通过查找正在运行的Solr实例来检测端口。
- -s <shards> - 将集合拆分为的分片数,默认值为1。
- -rf <replicas> - 集合中每个文档的副本数。默认值为1。
在本例中,我们将使用-c参数作为核心名称,使用-d参数作为配置目录。 对于所有其他参数,我们使用默认设置。
现在在命令窗口中导航solr-5.0.0\bin
文件夹,并发出以下命令。
solr create -c jcg -d basic_configs
我们可以在命令窗口中看到以下输出。
1
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >Creating new core 'jcg' using command:</ span >使用命令创建新核心'jcg':</ span >
|
2
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >http://localhost:8983/solr/admin/cores?action=CREATE&name=jcg&instanceDir=jcg</ span > http:// localhost:8983 / solr / admin / cores?action = CREATE&name = jcg&instanceDir = jcg</ span >
|
3
|
4
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >{</ span > {</ span >
|
5
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >"responseHeader":{</ span > “responseHeader”:{</ span >
|
6
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >"status":0,</ span > “status”:0,</ span >
|
7
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >"QTime":663},</ span > “QTime”:663},</ span >
|
8
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >"core":"jcg"}</ span > “core”:“jcg”}</ span >
|
现在我们导航到以下URL,我们可以看到jcg core被填充在核心选择器中。 您还可以查看核心的统计信息。
http://localhost:8983/solr
Solr JCG核心
3.2修改schema.xml文件
我们需要修改文件夹server\solr\jcg\conf
下的schema.xml
文件,以包含字段。我们将使用随同Solr安装一起提供的示例文件“books.csv”中的一个进行索引。 该文件位于文件夹solr-5.0.0\example\exampledocs
现在我们导航到文件夹server\solr
目录。 您将看到一个名为jcg
的文件夹。 子文件夹即conf
和data
分别具有核心的配置和索引数据。
现在编辑\server\solr\jcg\conf
文件夹中的schema.xml文件,并在uniqueKey元素后面添加以下内容。
schema.xml
1
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< uniqueKey >id</ uniqueKey ></ span > < uniqueKey > id </ uniqueKey ></ span >
|
2
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" > <!-- Fields added for books.csv load--> </ span > <! - 为books.csv加载添加的字段 - ></ span >
|
3
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< field name = "cat" type = "text_general" indexed = "true" stored = "true" /></ span > < field name =“cat”type =“text_general”indexed =“true”stored =“true”/></ span >
|
4
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< field name = "name" type = "text_general" indexed = "true" stored = "true" /></ span > < field name =“name”type =“text_general”indexed =“true”stored =“true”/></ span >
|
5
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< field name = "price" type = "tdouble" indexed = "true" stored = "true" /></ span > < field name =“price”type =“tdouble”indexed =“true”stored =“true”/></ span >
|
6
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< field name = "inStock" type = "boolean" indexed = "true" stored = "true" /></ span > < field name =“inStock”type =“boolean”indexed =“true”stored =“true”/></ span >
|
7
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< field name = "author" type = "text_general" indexed = "true" stored = "true" /></ span > < field name =“author”type =“text_general”indexed =“true”stored =“true”/></ span >
|
我们已将indexed
的属性设置为true。 这指定了用于索引的字段,并且可以使用索引检索记录。 将值设置为false将使该字段只存储,但不能查询。
还要注意,我们有另一个属性叫stored
并设置为true。 这指定字段存储并可以在输出中返回。 将此字段设置为false将使该字段仅编入索引,并且无法在输出中检索。
我们已经为“books.csv”文件中的字段分配了类型。 CSV文件“id”中的第一个字段由schema.xml文件的uniqueKey元素自动处理,用于建立索引。 如果你注意到,我们错过了字段series_t,sequence_i和genre_s,不做任何输入。 但是,当我们执行索引时,所有这些字段也被索引而没有任何问题。 如果你想知道这种情况,请仔细看看schema.xml文件中的dynamicField部分。
schema.xml
01
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_i" type = "int" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ i”type =“int”indexed =“true”stored =“true”/></ span >
|
02
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_is" type = "ints" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ is”type =“ints”indexed =“true”stored =“true”/></ span >
|
03
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_s" type = "string" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ s”type =“string”indexed =“true”stored =“true”/></ span >
|
04
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_ss" type = "strings" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ ss”type =“strings”indexed =“true”stored =“true”/></ span >
|
05
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_l" type = "long" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ l”type =“long”indexed =“true”stored =“true”/></ span >
|
06
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_ls" type = "longs" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ ls”type =“longs”indexed =“true”stored =“true”/></ span >
|
07
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_t" type = "text_general" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ t”type =“text_general”indexed =“true”stored =“true”/></ span >
|
08
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_txt" type = "text_general" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ txt”type =“text_general”indexed =“true”stored =“true”/></ span >
|
09
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_b" type = "boolean" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ b”type =“boolean”indexed =“true”stored =“true”/></ span >
|
10
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_bs" type = "booleans" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ bs”type =“booleans”indexed =“true”stored =“true”/></ span >
|
11
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_f" type = "float" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ f”type =“float”indexed =“true”stored =“true”/></ span >
|
12
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_fs" type = "floats" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ fs”type =“floats”indexed =“true”stored =“true”/></ span >
|
13
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_d" type = "double" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ d”type =“double”indexed =“true”stored =“true”/></ span >
|
14
|
< span class = "notranslate" onmouseover = "_tipon(this)" onmouseout = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< dynamicField name = "*_ds" type = "doubles" indexed = "true" stored = "true" /></ span > < dynamicField name =“* _ ds”type =“doubles”indexed =“true”stored =“true”/></ span >
|
由于我们已经修改了配置,我们必须停止和启动服务器。 为此,我们需要通过命令行从bin目录发出以下命令。
solr stop -all
服务器将立即停止。 现在启动服务器通过命令行从bin目录发出以下命令。
solr start
4.索引数据
Apache Solr附带一个名为SimplePostTool的独立Java程序。 该程序打包到JAR中,安装在example\exampledocs
文件夹下。
现在,我们导航到命令提示符中的example\exampledocs
文件夹,并键入以下命令。您将看到一堆使用该工具的选项。
java -jar post.jar -h
使用格式一般如下
Usage: java [SystemProperties] -jar post.jar [-h|-] [<file|folder|url|arg>
[<file|folder|url|arg>...]]
正如我们前面所说,我们将索引solr安装附带的“books.csv”文件中的数据。 我们将在命令提示符中导航到solr-5.0.0\example\exampledocs
并发出以下命令。
java -Dtype=text/csv -Durl=http://localhost:8983/solr/jcg/update -jar post.jar books.csv
这里使用的SystemProperties是:
- -Dtype - 数据文件的类型。
- -Durl - jcg核心的URL。
文件“books.csv”现在将被索引,并且命令提示符将显示以下输出。
1
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >SimplePostTool version 5.0 . 0 </span> SimplePostTool版本 5.0 . 0 </span>
|
2
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Posting files to [base] url http: //localhost:8983/solr/jcg/update using content-</span>将文件发布到[base] url http:// localhost:8983 / solr / jcg / update using content-</span>
|
3
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >type text/csv...</span>键入text / csv ...</span>
|
4
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >POSTing file books.csv to [base]</span>将文件books.csv发布到[base]</span>
|
5
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > 1 files indexed.</span> 1 个文件已索引。</span>
|
6
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >COMMITting Solr index changes to http: //localhost:8983/solr/jcg/update...</span>提交Solr索引更改为http:// localhost:8983 / solr / jcg / update ...</span>
|
7
|
<span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Time spent: 0 : 00 : 00.647 </span>所用时间: 0 : 00 : 00.647 </span>
|
现在我们导航到以下URL并选择核心jcg。
http://localhost:8983/solr
Solr JCG核心数据
仔细看看统计部分,Num Docs参数将显示索引的行数。
5.访问索引的文档
Apache Solr提供了基于REST的API来访问数据,并且还提供了不同的参数来检索数据。我们将向您展示几个基于场景的查询。
5.1按名称搜索
我们将通过它的名字检索书的细节。 为此,我们将使用以下语法。URL中的参数“q”是查询事件。
在浏览器中打开以下URL。
http://localhost:8983/solr/jcg/select?q=name:"A Clash of Kings"
输出将如下所示。
Solr按名称
5.2按起始字母搜索
现在,我们将告诉你如果我们只知道起始字母或词,并且不记得完整的标题,如何搜索记录。 我们可以使用以下查询来检索结果。
http://localhost:8983/solr/jcg/select?q=name:"A"
输出将列出所有用字母A盯着的书。
Solr开头字母
5.3使用通配符搜索
Solr支持通配符搜索。 我们将在以下查询中显示如何检索名称中包含单词“of”的所有书籍。
http://localhost:8983/solr/jcg/select?q=name:"*of"
输出将列出所有带有“of”字样的书。
Solr通配符搜索
5.4使用条件搜索
Solr支持条件搜索。 它提供了“fq”参数,使用它可以为我们的查询设置条件。我们将向您展示如何在以下查询中查找价格低于$ 6的图书。
http://localhost:8983/solr/jcg/select?q=*&fq=price:[0 TO 6]
输出将仅列出少于6美元的图书。
Solr搜索条件
6. Solr客户端API
有不同的客户端API可用于连接到Solr服务器。 我们列出了一些广泛使用的Solr客户端API。
- SolRuby - 从Ruby连接
- SolPHP - 从PHP连接
- PySolr - 从Python连接
- SolPerl - 从Perl连接
- SolrJ - 从Java连接
- SolrSharp - 从C#
此外,Solr提供了基于REST的API,可以使用JavaScript直接使用。
7.下载模式文件
这是一个Apache Solr的教程为初学者。
您可以在这里下载模式文件: Solr模式文件
初学者Apache Solr教程相关推荐
- Apache Solr入门教程(初学者之旅)
2019独角兽企业重金招聘Python工程师标准>>> 写在前面:本文涉及solr入门的各方面,请逐行阅读,相信能帮助你对solr有个清晰全面的了解并能简单实用. 在Apache S ...
- Apache Solr入门教程
转自:http://blog.csdn.net/u011936655/article/details/51960005 Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各 ...
- 搜索引擎之---Apache solr的实现
Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇分两部分的文章中,Lucene Java™ 的提交人 Grant Ingersoll ...
- solr crud_Spring Data Solr教程:CRUD(差不多)
solr crud 在我的Spring Data Solr教程的上一部分中,我们学习了如何配置Spring Data Solr. 现在该迈出一步,了解我们如何管理Solr实例中存储的信息. 此博客文章 ...
- Spring Data Solr教程:CRUD(差不多)
在我的Spring Data Solr教程的上一部分中,我们学习了如何配置Spring Data Solr. 现在该迈出一步,了解我们如何管理Solr实例中存储的信息. 此博客文章描述了我们如何向So ...
- Spring Data Solr教程:配置
在我的Spring Data Solr教程的上一部分中,我们了解到Solr提供了一个类似REST的HTTP API,该API可用于向Solr索引添加信息并针对索引数据执行查询. 问题在于,在开发环境中 ...
- matlab初学者教程_初学者的Hibernate教程
matlab初学者教程 Welcome to the Hibernate tutorial for Beginners. Hibernate is one of the most widely use ...
- 一个适合初学者的Ant教程
一个适合初学者的Ant教程 一,构建ant环境 要使用ant首先要构建一个ant环境,步骤很简单: 1),安装jdk,设置JAVA_HOME ,PATH ,CLASS_PATH(这些应该是 ...
- solr教程从入门到精通(solr5搜索引擎)-任亮-专题视频课程
solr教程从入门到精通(solr5搜索引擎)-8191人已学习 课程介绍 Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Sol ...
- Solr教程--官方自带数据的三个练习及讨论翻译版本
Solr教程 在你开始之前 解压Solr 练习1:索引Techproducts示例数据 在SolrCloud模式下启动Solr 索引技术产品数据 基本搜索 练习1总结 练习2:修改架构和索引影片数据 ...
最新文章
- ViewPager动态添加、删除Fragment,且提供红色小圆球指示当前位置
- 8月6日云栖精选夜读 | 阿里云CPFS在人工智能/深度学习领域的实践
- android 流式编程,流式编程
- oracle usenl,深入理解Oracle表(1):ORDERED和USE_NL | 学步园
- 131. Palindrome Partitioning
- 自己配置python环境_windows下python环境的配置
- SpringBoot 实现Session共享
- Linux学习总结(38)——Linux超实用的30个命令
- 奔跑中的2015--成长之路
- 【Oracle批量更新】根据一个大表批量更新另一大表的方法比较
- 输出200以内所有素数(python)
- centos host在哪 local_centos怎么查看hostid
- 阿里云体验有礼:使用RDS和ECS搭建个人博客
- 疯狂Android讲义(一)——第一部分
- linux刷新率设置命令,linux修改屏幕刷新率
- pci_register_driver
- matlab无法打开wps的xls文件,xls文件怎么打开,wps无法打开xls文件怎么办
- Halcon匹配助手
- java导出word功能(包含图片)二
- 为什么黑洞远程控制的屏幕传输比Radmin快