Solr Join(加入)示例
来源:互联网 发布:鞋子折痕淘宝不让退 编辑:程序博客网 时间:2024/06/11 11:30
本文英文版地址:https://examples.javacodegeeks.com/enterprise-java/apache-solr/solr-join-example/
在Solr Join示例的示例中,我们将讨论如何在Apache Solr中实现文档之间的连接。我们将向您展示如何实现{!join}指定的连接查询解析器插件。在Solr文档中使用连接,其中反标准化数据是耗时或昂贵的。
为了演示Solr Join用法,我们将使用与Solr安装一起提供的基本配置和索引示例文件在Solr中创建一个核心。
这个例子的首选环境是solr-5.0.0。在开始Solr安装之前,请确保已安装JDK并已正确设置Java_Home。
1.安装Apache Solr
首先,让我们从以下位置下载最新版本的Apache Solr:
http://lucene.apache.org/solr/downloads.html
Apache Solr已经经历了从4.xx到5.0.0的各种更改,因此如果您有不同版本的Solr,则需要下载5.xx版本以遵循此示例。
下载Solr zip文件后,将其解压缩到文件夹中。 提取的文件夹将如下所示:
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/
2.配置Apache Solr
在本节中,我们将向您展示如何配置solr实例的核心/集合以及如何定义字段。 Apache Solr附带一个名为Schemaless模式的选项。 此选项允许用户构建有效的模式,而无需手动编辑模式文件。但是对于这个例子,我们将使用Schema配置来理解Solr的内部。
2.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
2.2修改schema.xml文件
我们需要修改文件夹server\solr\jcg\conf
下的schema.xml
文件,以包含字段。我们将使用随Solr安装提供的示例文件“vidcard.xml”和“vendors.xml”进行索引。这些文件位于文件夹solr-5.0.0\example\exampledocs.
现在我们导航到文件夹server\solr
目录。 您将看到一个名为jcg
的文件夹。 子文件夹即conf
和data
分别具有核心的配置和索引数据。
现在编辑\server\solr\jcg\conf
文件夹中的schema.xml文件,并在uniqueKey元素后面添加以下内容。
schema.xml
01
<
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
>
02
<
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
>
03
<
span
class
=
"notranslate"
onmouseover
=
"_tipon(this)"
onmouseout
=
"_tipoff()"
><
span
class
=
"google-src-text"
style
=
"direction: ltr; text-align: left"
><
field
name
=
"manu"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“manu”type =“text_general”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"
><
field
name
=
"manu_id_s"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“manu_id_s”type =“text_general”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"
><
field
name
=
"cat"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
multiValued
=
"true"
/></
span
> <
field
name =“cat”type =“text_general”indexed =“true”stored =“true”multiValued =“true”/></
span
>
06
<
span
class
=
"notranslate"
onmouseover
=
"_tipon(this)"
onmouseout
=
"_tipoff()"
><
span
class
=
"google-src-text"
style
=
"direction: ltr; text-align: left"
><
field
name
=
"features"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
multiValued
=
"true"
/></
span
> <
field
name =“features”type =“text_general”indexed =“true”stored =“true”multiValued =“true”/></
span
>
07
<
span
class
=
"notranslate"
onmouseover
=
"_tipon(this)"
onmouseout
=
"_tipoff()"
><
span
class
=
"google-src-text"
style
=
"direction: ltr; text-align: left"
><
field
name
=
"weight"
type
=
"tdouble"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“weight”type =“tdouble”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"
><
field
name
=
"price"
type
=
"tdouble"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“price”type =“tdouble”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"
><
field
name
=
"popularity"
type
=
"tdouble"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“popularity”type =“tdouble”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"
><
field
name
=
"store"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“store”type =“text_general”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"
><
field
name
=
"inStock"
type
=
"boolean"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“inStock”type =“boolean”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"
><
field
name
=
"manufacturedate_dt"
type
=
"text_general"
indexed
=
"true"
stored
=
"true"
/></
span
> <
field
name =“manufacturedate_dt”type =“text_general”indexed =“true”stored =“true”/></
span
>
我们已将indexed
的属性设置为true。 这指定了用于索引的字段,并且可以使用索引检索记录。 将值设置为false将使该字段只存储,但不能查询。
还要注意,我们有另一个属性叫stored
并设置为true。 这指定字段存储并可以在输出中返回。 将此字段设置为false将使该字段仅编入索引,并且无法在输出中检索。
由于我们已经修改了配置,我们必须停止和启动服务器。 为此,我们需要通过命令行从bin目录发出以下命令:
solr stop -all
服务器将立即停止。 现在启动服务器通过命令行从bin目录发出以下命令:
solr start
3.索引数据
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安装附带的“vidcard.xml”和“vendors.xml”文件中的数据。我们将在命令提示符中导航到solr-5.0.0\example\exampledocs
并发出以下命令。
java -Dtype=application/xml -Durl=http://localhost:8983/solr/jcg/update -jar post.jar vidcard.xml
这里使用的SystemProperties是:
- -Dtype - 数据文件的类型。
- -Durl - jcg核心的URL。
现在将对文件“vidcard.xml”进行索引,并且命令提示符将显示以下输出。
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] urlhttp://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
application/xml...</span>类型应用程序/ xml ...</span>
4
<span class=
"notranslate"
onmouseover=
"_tipon(this)"
onmouseout=
"_tipoff()"
><span class=
"google-src-text"
style=
"direction: ltr; text-align: left"
>POSTing
file
vidcard.xml to [base]</span>将文件vidcard.xml发布到[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 tohttp://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.523</span>所用时间:0:00:00.523</span>
现在,我们将使用以下命令为“vendors.xml”文件中存在的文档创建索引:
java -Dtype=application/xml -Durl=http://localhost:8983/solr/jcg/update -jar post.jar manufacturers.xml
现在将对文件“vendors.xml”进行索引,并且命令提示符将显示以下输出:
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] urlhttp://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
application/xml...</span>类型应用程序/ xml ...</span>
4
<span class=
"notranslate"
onmouseover=
"_tipon(this)"
onmouseout=
"_tipoff()"
><span class=
"google-src-text"
style=
"direction: ltr; text-align: left"
>POSTing
file
manufacturers.xml to [base]</span>将文件manufacturer.xml发布到[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 tohttp://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.143</span>所用时间:0:00:00.143</span>
4.使用join查询
现在,我们将联接查询来了解graphics card
制造商的详细信息。为此,我们需要使用由{!join}
语法指定的连接QueryParser(Plugin)。连接使用Solr的LocalParam语法进行处理。
您可以观察到,Solr中的连接并不真正等同于SQL连接,因为没有关于正在连接的表“from”的信息被转发到最终结果中。Solr中的连接与SQL的“内部查询”更紧密相关。
以下连接Query将找到具有类别“graphics card”的所有文档,然后将它们(制造商)文档合并,并返回制造这些产品的制造商的列表。
http://localhost:8983/solr/jcg/select?q={!join+from=manu_id_s+to=id}cat:"graphics card"
5.下载配置
这是Solr连接的示例。
您可以在这里下载配置文件: schema.xml
- Solr Join(加入)示例
- Solr示例
- solr join查询
- [solr] - IKAnalyzer 分词加入
- [solr] - IKAnalyzer 分词加入
- 多线程join(加入)
- Solr加入中文分词器。
- solr示例程序
- Solr调用示例
- Solr查询语法示例
- Solr自动完成示例
- Solr突出显示示例
- Apache Solr复制示例
- Apache Solr同义词示例
- Solr过滤器查询示例
- Solr Schema.xml示例
- Solr Dataimporthandler示例
- Solr删除查询示例
- leecode 解题总结:75. Sort Colors
- Solr拼写检查示例
- Vue.js 系列教程 2:组件,Props,Slots
- C语言编程-指针遍历二维数组
- springMVC--SSM整合
- Solr Join(加入)示例
- python opencv 学习之视频输入之一
- linux 动态库文件stripped属性理解
- 集合上的函数式编程 示例
- 谷哥的小弟学后台(13)——Cookie
- WPF窗口控件动态模糊效果
- 第四十三讲项目3.3——用柱状图输出考试的平均成绩
- LeetCode-16. 3Sum Closest
- 前端开发者,2017 年你应该学习什么