本文英文版地址:https://examples.javacodegeeks.com/enterprise-java/apache-solr/solr-schema-xml-example/
在Solr Schema.xml示例的示例中,我们将讨论Apache Solr提供的schema.xml文件中的不同配置参数。schema.xml描述了我们希望Solr索引的文档。 我们可以定义字段类型和字段本身。 字段类型定义很强大,包括有关Solr如何处理传入字段值和查询值的信息。我们将讨论如何创建字段,创建自定义字段类型,在此示例中使用复制字段和使用动态字段。
为了演示schema.xml文件的使用,我们将使用基本配置在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文件后,将其解压缩到文件夹中。 提取的文件夹将如下所示:
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管理控制台
2.创建Solr核心
当Solr服务器在独立模式下启动时,配置称为核心,当在SolrCloud模式下启动时,配置称为集合。在这个例子中,我们将讨论独立的服务器和核心。 我们将暂停SolrCloud讨论以供日后使用。
首先,我们需要创建一个用于索引数据的Core。 Solr create命令有以下选项:
@H_502_192@在本例中,我们将使用-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> |
现在我们导航到以下URL,我们可以看到jcg core被填充在核心选择器中。 您还可以查看核心的统计信息。
http://localhost:8983/solr
3.使用字段类型
我们需要修改文件夹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 > |
我们已将indexed
的属性设置为true。 这指定了用于索引的字段,并且可以使用索引检索记录。 将值设置为false将使该字段只存储,但不能查询。
还要注意,我们有另一个属性叫stored
并设置为true。 这指定字段存储并可以在输出中返回。 将此字段设置为false将使该字段仅编入索引,并且无法在输出中检索。
我们已经为“books.csv”文件中的字段分配了类型。 CSV文件“id”中的第一个字段由schema.xml文件的uniqueKey元素自动处理,用于建立索引。
4.定义字段类型
在创建核心时,默认情况下会创建许多字段类型。 字段类型定义了Solr应如何解释字段中的数据以及如何查询字段。 我们将创建一个字段类型“ text_uppercase”,它将数据转换为大写字母值并对其进行索引。 字段类型定义可以包括四种类型的信息:
@H_502_192@字段类型确定字段类型的大多数行为,但也可以定义可选属性。
现在编辑\server\solr\jcg\conf
文件夹中的schema.xml文件,并在小写字段类型后添加以下内容。以下配置将创建一个字段类型“text_uppercase”。
schema.xml
01 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" > <!-- lowercases the entire field value,keeping it as a single token.</span> <! - 降低整个字段值,将其保留为单个令牌。</span> <span class="notranslate" onmouSEOver="_tipon(this)" onmouSEOut="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">--> </ span > - ></ span > |
02 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< fieldType name = "lowercase" class = "solr.TextField" positionIncrementGap = "100" ></ span > < fieldType name =“lowercase”class =“solr.TextField”positionIncrementGap =“100”></ span > |
03 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< analyzer ></ span > < analyzer ></ span > |
04 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< tokenizer class = "solr.KeywordTokenizerFactory" /></ span > < tokenizer class =“solr.KeywordTokenizerFactory”/></ span > |
05 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< filter class = "solr.LowerCaseFilterFactory" /></ span > < filter class =“solr.LowerCaseFilterFactory”/></ span > |
06 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" ></ analyzer ></ span > </ analyze ></ span > |
07 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" ></ fieldType ></ span > </ fieldType ></ span > |
08 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< fieldType name = "text_uppercase" class = "solr.TextField" positionIncrementGap = "100" ></ span > < fieldType name =“text_uppercase”class =“solr.TextField”positionIncrementGap =“100”></ span > |
09 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< analyzer ></ span > < analyzer ></ span > |
10 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< tokenizer class = "solr.WhitespaceTokenizerFactory" /></ span > < tokenizer class =“solr.WhitespaceTokenizerFactory”/></ span > |
11 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" >< filter class = "solr.UpperCaseFilterFactory" /></ span > < filter class =“solr.UpperCaseFilterFactory”/></ span > |
12 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" ></ analyzer ></ span > </ analyze ></ span > |
13 |
< span class = "notranslate" onmouSEOver = "_tipon(this)" onmouSEOut = "_tipoff()" >< span class = "google-src-text" style = "direction: ltr; text-align: left" ></ fieldType ></ span > </ fieldType ></ span > |