一丶Solr入门
1.Solr的启动
Solr各版本下载
老版本的时候,需要将war包放到tomcat中,现在只需解压,由于自带jetty容器,可以直接启动

 [root@aaa bin]# ./solr start -force -p 8983
*** [WARN] *** Your open file limit is currently 1024.
It should be set to 65000 to avoid operational disruption.
If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to
false in your profile or solr.in.sh
*** [WARN] *** Your Max Processes Limit is currently 15156.
It should be set to 65000 to avoid operational disruption.
If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to
false in your profile or solr.in.sh
Waiting up to 180 seconds to see Solr running on port 8982 [/]
Started Solr server on port 8982 (pid=4336). Happy searching!

Windos启动:

打开页面发现创建不了Core


官方提供的配置文件在 solr-7.5.0/server/solr/configsets/_default下,将该目录下的conf文件夹放到复制到我们新建的Core(new_core)下,上图所示的test目录在solr-7.5.0/server/solr/new_core,复制过去后,我们再去界面是新建一个test的Core就可以创建


新建完,有这些路径

[root@aaa new_core]# ll

total 12

drwxr-xr-x 3 root root 4096 Apr 13 01:23 conf

-rw-r--r-- 1 root root  131 Apr 13 01:24 core.properties

drwxr-xr-x 5 root root 4096 Apr 13 01:24 data

2.Solr的页面详解

  • Dashboard

    • 仪表盘,显示了该Solr实例开始启动运行的时间、版本、系统资源、jvm等信息。

  • Logging

    • Solr运行日志信息。

  • Core Admin

    • Solr Core的管理界面。在这里可以添加SolrCore实例。

  • java properties

    • Solr在JVM 运行环境中的属性信息,包括类路径、文件编码、jvm内存设置等信息。

  • Tread Dump

    • 显示Solr Server中当前活跃线程信息,同时也可以跟踪线程运行栈信息。

  • Core selector (重点)

    • 一个SolrCore进行详细操作

      • Analysis(重点)

      • 通过此界面可以测试索引分析器和搜索分析器的执行情况。

        注:solr中,分析器是绑定在域的类型中的

      • dataimport

        可以定义数据导入处理器,从关系数据库将数据导入到Solr索引库中。

        默认没有配置,需要手工配置。注: 后面会提到配置文件

      • Document

        通过/update表示更新索引,solr默认根据id(唯一约束)域来更新Document的内容,如果根据id值搜索不到id域则会执行添加操作,如果找到则更新。

        通过此菜单可以创建索引、更新索引、删除索引等操作,界面如下:

      • <!--l overwrite="true" : solr在做索引的时候,如果文档已经存在,就用xml中的文档进行替换-->

        <!--l ommitWithin="1000" : solr 在做索引的时候,每个1000(1秒)毫秒,做一次文档提交。为了方便测试也可以在Document中立即提交,</doc>后添加“<commit/>”-->

      • Query(重点)

注: 常见的查询条件,链接--> 查询

3.Solr的配置文件

3.1managed-schema

注:以前的旧版本的时候,配置文件叫schema.xml

 [root@192 conf]# pwd
/mnt/solr-7.5.0/server/solr/test_core/conf
[root@192 conf]# ll
total 132
drwxr-xr-x 2 root root 4096 Apr 13 08:24 lang
-rw-r--r-- 1 root root 55728 Apr 13 08:59 managed-schema
-rw-r--r-- 1 root root 308 Apr 13 08:24 params.json
-rw-r--r-- 1 root root 873 Apr 13 08:24 protwords.txt
-rw-r--r-- 1 root root 53959 Apr 13 08:24 solrconfig.xml
-rw-r--r-- 1 root root 781 Apr 13 08:24 stopwords.txt
-rw-r--r-- 1 root root 1124 Apr 13 08:24 synonyms.txt
[root@192 conf]#

3.1.1   Filed   定义域

  • Name:指定域的名称 一般映射成表里面的字段

  • Type:指定域的类型

  • Indexed:是否索引

  • Stored:是否存储

  • Required:是否必须

  • multiValued:是否多值,比如商品信息中,一个商品有多张图片,一个Field像存储多个值的话,必须将multiValued设置为true。

3.1.2 dynamicField 动态域

  • Name:指定动态域的命名规则 类型模糊匹配

3.1.3 uniqueKey

  • <uniqueKey>id</uniqueKey>

其中的id是在Field标签中已经定义好的域名,而且该域要设置为required为true。一个配置文件中必须有且仅有一个唯一键。

3.1.4 copyField

  • Source:要复制的源域的域名

    Dest:目标域的域名

    由dest指的的目标域,必须设置multiValued为true。

    3.1.5FiledType

3.1.5 FieldType

  • Name:指定域类型的名称

Class:指定该域类型对应的solr的类型

Analyzer:指定分析器

Type:index、query,分别指定搜索和索引时的分析器

Tokenizer:指定分词器

4.IK分词器

IK下载 密码:igt9。

  • 将两个jar包复制到该路径: solr-7.5.0\server\solr-webapp\webapp\WEB-INF\lib

  • 另外将三个配置文件复制到该路径: solr-7.5.0\server\solr-webapp\webapp\WEB-INF\classes。如果没有classes文件夹就新建一个。

  • 在schema中添加分词器。

 <!-- 我添加的IK分词 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.apache.lucene.analysis.ik.IKTokenizerFactory" useSmart="true"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.apache.lucene.analysis.ik.IKTokenizerFactory" useSmart="true"/>
</analyzer>
</fieldType> <!-- 继续添加业务域 -->
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="plong" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category_name" type="string" indexed="true" stored="true" />
<field name="item_desc" type="text_ik" indexed="true" stored="false" /> <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>
<copyField source="item_desc" dest="item_keywords"/>
  • 重启Solr。

注:这边没有出来结果,不明白为什么。但是windows版的就成功了

5.Solr简单实用

5.1 根据业务配置filed

 <!--product-->
<field name="product_name" type="text_ik" indexed="true" stored="true"/>
<field name="product_catalog" type="string" indexed="true" stored="true"/>
<field name="product_catalog_name" type="string" indexed="true" stored="true" />
<field name="product_price" type="pfloat" indexed="true" stored="true"/>
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>

注:现在高本版的int、float要进一个p字母

5.2引入jar包

  • Mysql的驱动包

  • 把这3个jar包拷贝到 solr\server\solr-webapp\webapp\WEB-INF\lib 下

5.3solrconfig.xml文件配置

<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
在第一个requestHandler下面,加入这段。

5.4创建data-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/solr"
user="root"
password="root"/>
<document>
<entity name="product" query="SELECT pid,name,catalog,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog" name="product_catalog"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
  • dataSource不多说,一看就明白

  • entity --代表数据库表

  • column--代表数据库表字段

  • name--代表solr中字段名

5.5重启Solr

导入数据。

6.Solrj的使用

6.1查询

@Test
public void search01() throws Exception {
// 创建HttpSolrServer
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/test_core");
// 创建SolrQuery对象
SolrQuery query = new SolrQuery();
// 输入查询条件
query.setQuery("product_name:小黄人");
// 执行查询并返回结果
QueryResponse response = server.query(query);
// 获取匹配的所有结果
SolrDocumentList list = response.getResults();
// 匹配结果总数
long count = list.getNumFound();
System.out.println("匹配结果总数:" + count);
for (SolrDocument doc : list) {
System.out.println(doc.get("id"));
System.out.println(doc.get("product_name"));
System.out.println(doc.get("product_catalog"));
System.out.println(doc.get("product_price"));
System.out.println(doc.get("product_picture"));
System.out.println("=====================");
}
}
-- 注:URL那里要加上那个core,不然会报错,之前研究4.xx版本的时候,没有这个问题
查询结果:
匹配结果总数:122
4403
家天下小黄人美耐瓷卡通儿童筷-黄色筷子
47
5.5
2014032515340838.png
=====================
4404
家天下小黄人美耐瓷卡通儿童筷-蓝色筷子
47
5.5
2014032515284014.png
=====================
3
神偷奶爸电影同款&nbsp;惨叫发泄公仔&nbsp;发声小黄人
17
10.0
2014032417271233.png
=====================
4
神偷奶爸电影同款&nbsp;&nbsp;发泄公仔&nbsp;暴眼小黄人
17
13.0
2014032416533215.png
=====================
2054
家天下高保真翻开书页创意小音箱便携小音响SL23
28
18.0
2011120313192588_S.jpg
=====================
125
迷你随身小扑克CL-1902
17
3.0
2011042711514296.jpg
=====================
895
家天下南韩小夹子黑板
20
0.93
2009110217233545.jpg
=====================
1941
家天下法式小酒桶打火机
25
6.8
2011041917513362_S.jpg
=====================
3199
好酷仔迷你小风扇
30
7.5
2013041714005875_S.jpg
=====================
195
家天下小酒窝青蛙情侣挂钩
17
3.2
2009080314562507.jpg
=====================

6.2复杂查询

 @Test
public void search02() throws Exception {
// 创建HttpSolrServer
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/test_core");
// 创建SolrQuery对象
SolrQuery query = new SolrQuery();

// 输入查询条件
query.setQuery("product_name:小黄人");
// query.set("q", "product_name:小黄人");

// 设置过滤条件
// 如果设置多个过滤条件的话,需要使用query.addFilterQuery(fq)
query.setFilterQueries("product_price:[1 TO 10]");

// 设置排序
query.setSort("product_price", ORDER.asc);
// 设置分页信息(使用默认的)
query.setStart(0);
query.setRows(5);

// 设置显示的Field的域集合
query.setFields("id,product_name,product_catalog,product_price,product_picture");

// 设置默认域
query.set("df", "product_keywords");

// 设置高亮信息
query.setHighlight(true);
query.addHighlightField("product_name");
query.setHighlightSimplePre("<em>");
query.setHighlightSimplePost("</em>");

// 执行查询并返回结果
QueryResponse response = server.query(query);
// 获取匹配的所有结果
SolrDocumentList list = response.getResults();
// 匹配结果总数
long count = list.getNumFound();
System.out.println("匹配结果总数:" + count);

// 获取高亮显示信息
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for (SolrDocument doc : list) {
System.out.println(doc.get("id"));

List<String> list2 = highlighting.get(doc.get("id")).get("product_name");
if (list2 != null)
System.out.println("高亮显示的商品名称:" + list2.get(0));
else {
System.out.println(doc.get("product_name"));
}

System.out.println(doc.get("product_catalog"));
System.out.println(doc.get("product_price"));
System.out.println(doc.get("product_picture"));
System.out.println("=====================");
}
}
匹配结果总数:81
3741
高亮显示的商品名称:家天下正品达贺纤长睫毛<em>小</em>帮手1029立体水晶睫毛卡
33
1.15
20080816114400873.jpg
=====================
862
高亮显示的商品名称:家天下南韩<em>小</em>夹子黑板新版
20
1.5
2011071109133007_S.jpg
=====================
4675
高亮显示的商品名称:家天下甜心柄美耐瓷<em>小</em>汤勺1105-粉
47
1.6
2010011214065760.jpg
=====================
4676
高亮显示的商品名称:家天下甜心柄美耐瓷<em>小</em>汤勺1105-蓝
47
1.6
2010011214062611.jpg
=====================
4677
高亮显示的商品名称:家天下甜心柄美耐瓷<em>小</em>汤勺1105-绿
47
1.6
2010011214045423.jpg
=====================

6.3增加索引

  @Test
public void insertAndUpdateIndex() throws Exception {
// 创建HttpSolrServer
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/test_core");
// 创建Document对象
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "c001");
doc.addField("name", "solr test111");
// 将Document对象添加到索引库
server.add(doc);
// 提交
server.commit();
}
{
"responseHeader":{
"status":0,
"QTime":8,
"params":{
"q":"name:solr test111",
"_":"1555242778361"}},
"response":{"numFound":1,"start":0,"docs":[
{
"id":"c001",
"name":["solr test111"],
"_version_":1630790213324242944}]
}}

6.4删除索引

   @Test
public void deleteIndex() throws Exception {
// 创建HttpSolrServer
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr/test_core");

// 根据指定的ID删除索引
// server.deleteById("c001");

// 根据条件删除
server.deleteByQuery("id:c001");

// 删除全部(慎用)
// server.deleteByQuery("*:*");

// 提交
server.commit();
}

 

已经被删除,就找不到了

附:Solr查询用法

  • q - 查询关键字,必须的,如果查询所有使用:

请求的q是字符串

  • fq - (filter query)过虑查询,作用:在q查询符合结果中同时是fq查询符合的,例如::

请求fq是一个数组(多个值)

过滤查询价格从1到20的记录。也可以在“q”查询条件中使用product_price:[1 TO 20],如下:

也可以使用“*”表示无限,例如:

20以上:product_price:[20 TO *]

20以下:product_price:[* TO 20]

  • sort - 排序,格式:sort=<field name>+<desc|asc>[,<field name>+<desc|asc>]… 。示例:

    按价格降序

  • start - 分页显示使用,开始记录下标,从0开始

  • rows - 指定返回结果最多有多少条记录,配合start来实现分页。

实际开发时,知道当前页码和每页显示的个数最后求出开始下标。

  • fl - 指定返回那些字段内容,用逗号或空格分隔多个。

显示商品图片、商品名称、商品价格

  • df-指定一个搜索Field

也可以在SolrCore目录 中conf/solrconfig.xml文件中指定默认搜索Field,指定后就可以直接在“q”查询条件中输入关键字。

  • wt - (writer type)指定输出格式,可以有 xml, json, php, phps, 后面 solr 1.3增加的,要用通知我们,因为默认没有打开。

  • hl 是否高亮 ,设置高亮Field,设置格式前缀和后缀。

 

Solr入门(一)的更多相关文章

  1. Solr入门之SolrServer实例化方式

    随着solr版本的不断升级, 差异越来越大, 从以前的 solr1.2 到现在的 solr4.3, 无论是类还是功能都有很大的变换, 为了能及时跟上新版本的步伐, 在此将新版本的使用做一个简单的入门说 ...

  2. Solr入门介绍

    solr入门案例 solr是apache下的一个全文检索引擎系统. 我们需要在服务器上单独去部署solr, 通过它的客户端工具包solrJ, 就是一个    jar包, 集成到我们项目中来调用服务器中 ...

  3. Apache Solr入门教程(初学者之旅)

    Apache Solr入门教程(初学者之旅) 写在前面:本文涉及solr入门的各方面,建议边思考边实践,相信能帮助你对solr有个清晰全面的了解并能简单实用. 在Apache Solr初学者教程的这个 ...

  4. Solr学习笔记(5)—— Spring Data Solr入门

    一.Spring Data Solr简介 前面已经介绍了通过solrJ来操作solr,那么我们如何将Solr的应用集成到Spring中?Spring Data Solr就是为了方便Solr的开发所研制 ...

  5. Spring Data Solr入门

    如何将Solr的应用集成到Spring中? SpringDataSolr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ的封装. SpringDataSolr入门小Demo 首先目录结 ...

  6. solr入门

    Solr采用Lucene搜索库为核心,提供全文索引和搜索开源企业平台,提供REST的HTTP/XML和JSON的API,如果你是Solr新手,那么就和我一起来入门吧!本教程以solr4.8作为测试环境 ...

  7. Solr入门和实践以及我对Solr的8点理解

    友情提示Solr的内容还是比较多的,一篇文章只能讲解一部分.全面介绍,没兴趣,没时间,也没能力,回报还不大.本文只写点我认为比较重要的知识点,独特的个人想法.仅供参考哦,更多细节需要自己去琢磨. 概述 ...

  8. 后端技术杂谈4:Elasticsearch与solr入门实践

    阮一峰:全文搜索引擎 Elasticsearch 入门教程 作者:阮一峰 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://g ...

  9. java应用之solr入门篇

    前言 solr是apache项目的一款全文搜索应用. 官方文档http://lucene.apache.org/solr/guide/6_6/ 入门流程 1.安装   --->  2.启动  - ...

随机推荐

  1. pandas.DataFrame的groupby()方法的基本使用

    pandas.DataFrame的groupby()方法是一个特别常用和有用的方法.让我们快速掌握groupby()方法的基础使用,从此数据分析又多一法宝. 首先导入package: import p ...

  2. Go中使用seed得到相同随机数的问题

    1. 重复的随机数 废话不多说,首先我们来看使用seed的一个很神奇的现象. func main() { for i := 0; i < 5; i++ { rand.Seed(time.Now( ...

  3. pip 修改镜像源为豆瓣源

    1. 修改配置文件 编辑配置文件,如果没有则新建: $ vi ~/.pip/pip.conf 添加内容如下: [global] index-url = https://pypi.doubanio.co ...

  4. vue运行报错webpack-dev-server: command not found

    翻译过来就是: 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 解决方法: 然后总结下成功的步骤: 1. 直接在项目目录下: cnpm install npm run ...

  5. asp.net core刷新css缓存

    在非spa程序开发的时候.css经常会因为浏览器的缓存机制导致不刷新. 很多前端为了应对这个问题,都会引入webpack或者gulp等工具来处理css缓存的问题. 但是作为一个偏服务器端的程序员来说. ...

  6. Http协议Content-Length详解

    前言 http协议是互联网中最重要的协议之一,虽然看上去很简单,但是实际中经常遇到问题,我们就已经遇到好几次了.有长连接相关的,有报文解析相关的.对http协议不能一知半解,必须透彻理解才行.本文通过 ...

  7. mpvue 星星打分组件

    上图: <template> <div class="container"> <div v-for="(star,index) in sta ...

  8. 微信开发中使用微信JSSDK和使用URL.createObjectURL上传预览图片的不同处理对比

    在做微信公众号或者企业微信开发业务应用的时候,我们常常会涉及到图片预览.上传等的处理,往往业务需求不止一张图片,因此相对来说,需要考虑的全面一些,用户还需要对图片进行预览和相应的处理,在开始的时候我使 ...

  9. python3爬虫环境搭建

    安装python3 sudo apt-get install python3-dev build-essential libssl-dev libffi-dev libxml2 libxml2-dev ...

  10. 基于 Web 端 3D 地铁站可视化系统

    前言 工业互联网,物联网,可视化等名词在我们现在信息化的大背景下已经是耳熟能详,日常生活的交通,出行,吃穿等可能都可以用信息化的方式来为我们表达,在传统的可视化监控领域,一般都是基于 Web SCAD ...