Solr的使用 — 检索

Solr的使用 — 检索,第1张

本文是延续 Solr的使用 系列,前一篇文章已经讲了 Solr 的部署和数据推送,这里主要以示例方式讲述 Solr 的常见查询语法,同时介绍如何使用 PHP 语言的客户端 solarium 同 Solr 集群进行数据交互。

想要详细地了解 Solr 查询语法,可参考 官方wiki 。

用于示例的数据,我已经推送到了 Solr , 见这里 。数据 Core 为 rooms,数据格式形如:

通过向 Solr 集群 GET 请求 /solr/core-name/select?query 形式的查询 API 完成查询,其中 core-name 为查询的 Core 名称。查询语句 query 由以下基本元素项组成,按使用频率先后排序:

wt 设置结果集格式,支持 json、xml、csv、php、ruby、pthyon,序列化的结果集,常使用 json 格式。

fl 指定返回的字段,多指使用“空格”和“,”号分割,但只支持设置了 stored=true 的字段。 * 表示返回全部字段,一般情况不需要返回文档的全部字段。

字段别名 :使用 displayName:fieldName 形式指定字段的别名,例如:

函数 :fl 还支持使用 Solr 内置函数 ,例如根据单价算总价:

fq 过滤查询条件,可充分利用 cache,所以可以利用 fq 提高检索性能。

sort 指定结果集的排序规则,格式为 <fieldName>+<sort>,支持 asc 和 desc 两种排序规则。例如按照价格倒序排列:

也可以多字段排序,价格和面积排序:

查询字符串 q 由以下元素项组成,字段条件形如 fieldName:value 格式:

以上元素项的默认值由 solrconfig.xml 配置文件定义。通常查询时设置 q=*:* ,然后通过 fq 过滤条件来完成查询,通过缓存提高查询性能。

Solr 的模糊查询使用占位符来描述查询规则,如下:

查询小区名称中包含“嘉”的房源信息

Solr 的模糊查询为:

单精确值查询是最简单的查询,类似于 SQL 中 = *** 作符。查询小区 id 为 1111027377528 的房源信息:

Solr 中查询为:

多精确值查询是单精确值查询的扩展,格式为 (value1 value2 ...) ,功能类似于 SQL 的 IN *** 作符。查询小区 id 为 1111027377528 或者 1111047349969 的房源信息:

Solr 中查询为:

范围查询是查询指定范围的值(数字和时间),格式为 [value1 TO value2] ,类似于 SQL 的 BETWEEN *** 作符。查询价格在 [2000, 3000] 的房源信息:

Solr 中范围查询为:

几个特殊的范围查询:

将基本查询结合布尔查询,就可以实现大部分复杂的检索场景。布尔查询支持以下几种布尔 *** 作:

查询北京市价格区间在 [2000, 3000] 或者上海市价格区间在 [1500, 2000] 的房源信息:

转换为逻辑与布尔查询:

在实际中分组查询比较常见,当然 Solr 也支持分组查询。分组查询语句由以下基本元素项组成(常用部分):

查询西二旗内价格最便宜小区的房源信息:

Group 分组查询为:

结果为:

在大多数情况下,Group 分组已经能满足我们的需求,但是如果待分组字段为多值,Group 分组已经无能为力了,这时使用 Facet 就能轻松解决。

Solr 的 Facet 语句由以下基本元素构成(常用):

例如,统计每个商圈的房源分布情况并倒序排列,由于 bizcircleCode 字段为多值,Facet 查询为:

结果如下:

Solr 的 geofilt 过滤器可以实现 LBS 检索,但要在 schema.xml 配置中将需检索字段的字段类型设置为 solr.LatLonType 类型。geofilt 过滤器参数列表如下:

示例中的 location 字段,值为 “40.074203,116.315445”,类型配置为:

则检索坐标点 40.074203,116.315445 附近 2 公里的房源信息:

Solr 提供一些函数以实现逻辑或数学运算。其中常用 数学运算 函数列表如下:

常用的 逻辑运算 函数:

这些函数可以使用在返回值或者查询条件上。例如返回每个房源的每平方米价格信息:

PHP 可以使用 solarium 客户端,实现 Solr 数据源的检索,详细使用说明 见这里 。

solarium 客户端需要配置 Solr 的基本信息。如下:

solarium 提供的查询方法较丰富,整理后如下表所示:

查询北京市的所有房源信息,如下:

solarium 提供的分组查询方法如下表所示(常用):

获取西二旗每个小区的房源分布信息,如下:

solarium 提供的 Facet 查询方法,如下表(常用):

获取北京市每个商圈的房源分布信息,如下:

到这里,Solr 系列就整理完毕了,未涉及的部分后续接触时再补充。这两天利用休息时间充电,自己在 Solr 方面的技能也算是上了一个台阶了。

在solr与tomcat整合文章中,我用的索引库是mycore,现在就以这个为例。

首先要准备jar包:solr-dataimporthandler-4.8.1.jar、solr-dataimporthandler-extras-4.8.1.jar和mysql-connector-java-5.0.7-bin.jar这三个包到solr的tomcat的webapps\solr\WEB-INF\lib下

在这个文件夹的conf下配置两个文件,添加一个文件。先配置solrconfig.xml。

在该文件下添加一个新节点。

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">

<lst name="defaults">

<str name="config">data-config.xml</str>

</lst>

</requestHandler>

在solrconfig.xml的同目录下创建data-config.xml。

配置:

复制代码

<dataConfig>

<dataSource type="JdbcDataSource"

driver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/courseman"

user="root"

password="mysql" />

<document>

<entity name="student"

query="SELECT * FROM student">

<field column="id" name="id" />

<field column="name" name="name" />

<field column="gender" name="gender" />

<field column="major" name="major" />

<field column="grade" name="grade" />

</entity>

</document>

</dataConfig>

复制代码

schemal.xml的配置

复制代码

<?xml version="1.0" ?>

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License")you may not use this file except in compliance with

the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

-->

<schema name="example core one" version="1.1">

<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>

<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>

<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>

<!-- general -->

<field name="id" type="int"indexed="true" stored="true" />

<field name="gender" type="string"indexed="true" stored="true" />

<field name="name" type="string"indexed="true" stored="true" />

<field name="major" type="string"indexed="true" stored="true" />

<field name="grade" type="string"indexed="true" stored="true" />

<field name="_version_" type="long" indexed="true" stored="true"/>

<!-- field to use to determine and enforce document uniqueness. -->

<uniqueKey>id</uniqueKey>

<!-- field for the QueryParser to use when an explicit fieldname is absent -->

<defaultSearchField>name</defaultSearchField>

<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->

<solrQueryParser defaultOperator="OR"/>

</schema>

复制代码

默认的文件不是这样的,稍微改动了一下。

field 的type类型是根据fieldtype 的name定义的。class是solr自定义的不能更改。

shcema.xml文件的field字段的属性介绍:

(1)name:字段名称

(2)type:字段类型(此处type不是java类型,而是下面定义的fieldType)

(3)indexed:是否索引看true--solr会对这个字段进行索引,只有经过索引的字段才能被搜索、排序等;false--不索引

(4)stored:是否存储看true--存储,当我们需要在页面显示此字段时,应设为true,否则false。

(5)required:是否必须看true--此字段为必需,如果此字段的内容为空,会报异常;false--不是必需

(6)multiValued:此字段是否可以保存多个值看

(7)omitNorms:是否对此字段进行解析看有时候我们想通过某个字段的完全匹配来查询信息,那么设置 indexed="true"、omitNorms="true"。

(8)default:设置默认值

有这样一个FieldType描述:

<fieldType name="text_general" positionIncrementGap="100">

<analyzer type="index">

<tokenizer/>

<filter ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />

<filter/>

</analyzer>

<analyzer type="query">

<tokenizer/>

<filter ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />

<filter synonyms="synonyms.txt" ignoreCase="true" expand="true"/>

<filter/>

</analyzer>

</fieldType>

属性说明:

(1)name:类型名称,<field>中的type引用的就是这个name

(2)class:solr自定义的类型

(3)<analyzer type="index">定义建立索引时使用的分词器及过滤器

(4)<analyzer type="query">定义搜索时所使用的分词器及过滤器

(5)<tokenizer/>定义分词器

(6)<filter/>定义过滤器

uniqueKey属性

<uniqueKey>id</uniqueKey>

类似于数据表数据的id,solr索引库中最好定义一个用于标示document唯一性的字段,此字段主要用于删除document。

defaultSearchField属性

就是你在做query搜寻时若不指定特定栏位做检索时, Solr就会只查这个栏位.

<defaultSearchField>default</defaultSearchField>

copyField属性

是用来复制你一个栏位里的值到另一栏位用. 如你可以将name里的东西copy到major里, 这样solr做检索时也会检索到name里的东西.

<copyField source="name" dest="major"/>

现在可以将数据库的数据导入solr了。

点击Execute就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/sjk/9622347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存