solr的基本使用
Solr
概念:
1. 搜索引擎的技术,建立在Lucene之上,可以解决跨平台,跨语言的问题.(Lucene本身是个jar包,也就是API,不能独立运行,需要程序的调用来完成全局检索,不具备跨平台,跨语言).
2. 基于Lucene的全文检索服务,是可以独立运行的(war包,运行在tomcat).
solr安装
1. 解压新的tomcat
2. 将solr进行解压
3. 找到solr.war:
* D:\develop\lucene\solr\solr-4.10.3\dist 下,自己的解压目录
* D:\develop\lucene\solr\solr-4.10.3\example\webapps 下也有
4. 将war包放在tomcat的webapps下
5. 启动tomcat,解压war包,关闭tomcat
6. 将D:\develop\lucene\solr\solr-4.10.3\example下有个solr文件夹,将该文件夹拷贝到D盘根目录(习惯)
* solrHome: 是solr运行的主目录
* solrCore: 类似于数据库,可以单独对外提供索引和搜索的服务
* 一个solrHome下包含多个solrCore
7. 在tomcat的webapps下的solr的解压文件夹下的WEB-INF下的web.xml,进行修改:
第41到45行(修改<env-entry-value>的目录为第六步复制后的那个目录):
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:\solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
8. 复制扩展包:
D:\develop\lucene\solr\solr-4.10.3\example\lib\ext目录下的所有包复制到tomcat的webapps下的solr下的WEB-INF下的lib下,然后可以启动tomcat,在网页进行访问 localhost:8080/solr
中文分析器IK Analyzer
1. 为什么要使用IK呢?
因为solr是由外国人研发,对汉语的分词是一个汉字一个汉字分的,所以引入IK,对中文有很好的分词效果,并且可以自定义扩充分词词典
2. 安装IK
1. 解压IK Analyzer 2012FF_hf1.zip
2. 将解压目录下的IKAnalyzer2012FF_u1.jar放在tomcat下的webapps下的solr下的WEB-INF下的lib下
3. 拷贝解压目录下的IKAnalyzer.cfg.xml,ext_stopword.dic,mydict.dic放在tomcat下的webapps下的solr下的WEB-INF下的classes下,classes不存在创建一个就好
4. 到前面安装solr时的那个solrhome下,修改collection1\conf\schema.xml,在1152行添加:
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
5. 选择分词的时候选择text_ik,就可以使用中文分析器了.(笔者第一次就是选择text,一直失败,谨此注意一下)
配置域:
1. 域是针对当前项目而言的
* 需要根据什么样的条件进行查询
* 查询出来的记过需要展示的数据信息
* 需要用到的字段
2. 使用solr进行索引创建,所指定的域 必须在schema.xml中存在,否则会创建失败.
3. 关于域的属性:
* name: 所指定域的名称
* type: 域的类型-----> 引用filedType中的name属性(是否分词)---->为了索引
* indexed: 是否索引----> 为了搜索
* stored: 是否存储----> 为了展示
* required: 是否必须
* multiValued: 是否多值
4. 针对本项目,修改schema.xml文件,在</schema>之前添加:
<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />
5. 复制域(多个字段进行搜索):
<field name="item_keywords" type="text_ik" indexed="true" stored="false"
multiValued="true"/>
6. 动态域(通配)
7. 主键:
主键----> 必填,唯一
SolrJ(原理: http请求和响应)
Solr官方提供的API(理解)
SpringDataSolr(对SolrJ的封装)
1. 引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
2. 基本的操作:
package springdatasolr;
import com.wzlove.pojo.TbItem;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName SpringDataSolrDemo
* @Author wz157
* @Date 2018/11/8 15:22
* @Description TODO
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-solr.xml"})
public class SpringDataSolrDemo {
// 注入SolrTemplate
@Autowired
private SolrTemplate solrTemplate;
/**
* 测试添加索引
*/
@Test
public void testAdd(){
TbItem item = new TbItem();
item.setId(1L);
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店");
item.setTitle("华为 Mate9");
item.setPrice(new BigDecimal(2000));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
/**
* 根据主键进行查询,getById()
*/
@Test
public void findByUniqueKey(){
TbItem byId = solrTemplate.getById(1, TbItem.class);
System.out.println(byId);
}
/**
* 根据主键进行删除,deleteById() 记得传字节码文件
*/
@Test
public void deleteByUniqueKey(){
solrTemplate.deleteById("1");
// 提交
solrTemplate.commit();
}
/**
* 批量插入,saveBeans
*/
@Test
public void testAddList(){
List<TbItem> list = new ArrayList<TbItem>(100);
for (int i = 0; i < 100; i++) {
TbItem item = new TbItem();
item.setId(i + 1L);
item.setBrand("华为" + i);
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为2号专卖店" + i);
item.setTitle("华为 Mate9" + i );
item.setPrice(new BigDecimal(2000 + i));
list.add(item);
}
solrTemplate.saveBeans(list);
solrTemplate.commit();
}
/**
* 分页查询(总页数,总记录数)
* offset 开始索引
* rows 每页记录数
*/
@Test
public void testQueryByPage(){
// 创建查询条件,查询所有使用 *:*
Query query = new SimpleQuery("*:*");
// 设置分页条件
query.setOffset(2);
query.setRows(5);
// 执行分页查询
ScoredPage<TbItem> tbItems = solrTemplate.queryForPage(query, TbItem.class);
// 解析分页结果
for (TbItem tbItem : tbItems) {
System.out.println(tbItem.getBrand());
System.out.println(tbItem.getPrice());
}
System.out.println("总记录的数 : " + tbItems.getTotalElements());
System.out.println("总页数 : " + tbItems.getTotalPages());
}
/**
* 条件查询
*/
@Test
public void testPageQueryMutil(){
// 创建查询条件,查询所有使用 *:*
Query query = new SimpleQuery("*:*");
// 设置查询条件(Criterial)
Criteria criteria = new Criteria("item_brand").contains("2");
criteria = criteria.and("item_seller").contains("2");
query.addCriteria(criteria);
// 执行分页查询
ScoredPage<TbItem> tbItems = solrTemplate.queryForPage(query, TbItem.class);
// 解析分页结果
for (TbItem tbItem : tbItems) {
System.out.println(tbItem.getBrand() + " " + tbItem.getPrice() + " " + tbItem.getSeller());
}
System.out.println("总记录的数 : " + tbItems.getTotalElements());
System.out.println("总页数 : " + tbItems.getTotalPages());
}
/**
* 全部删除
*/
@Test
public void testDeleteAll(){
Query query = new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}
}
3. 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- solr 服务器地址,相当于配置了HttpSolrServer这个bean -->
<solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr/collection1" />
<!-- solr 模板,使用 solr 模板可对索引库进行 CRUD 的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
4. 实体:
public class TbItem implements Serializable {
@Field
private Long id;
@Field("item_title")
private String title;
@Field("item_price")
private BigDecimal price;
@Field("item_image")
private String image;
@Field("item_goodsid")
private Long goodsId;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
@Field("item_seller")
private String seller;
get和set
@Override
public String toString() {
return "TbItem{" +
"id=" + id +
", title='" + title + '\'' +
", sellPoint='" + sellPoint + '\'' +
", price=" + price +
", stockCount=" + stockCount +
", num=" + num +
", barcode='" + barcode + '\'' +
", image='" + image + '\'' +
", categoryid=" + categoryid +
", status='" + status + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
", itemSn='" + itemSn + '\'' +
", costPirce=" + costPirce +
", marketPrice=" + marketPrice +
", isDefault='" + isDefault + '\'' +
", goodsId=" + goodsId +
", sellerId='" + sellerId + '\'' +
", cartThumbnail='" + cartThumbnail + '\'' +
", category='" + category + '\'' +
", brand='" + brand + '\'' +
", spec='" + spec + '\'' +
", seller='" + seller + '\'' +
'}';
}
}
5. 需要注意的是在solr库的schema约束中配置域
solr的基本使用的更多相关文章
- solr服务中集成IKAnalyzer中文分词器、集成dataimportHandler插件
昨天已经在Tomcat容器中成功的部署了solr全文检索引擎系统的服务:今天来分享一下solr服务在海量数据的网站中是如何实现数据的检索. 在solr服务中集成IKAnalyzer中文分词器的步骤: ...
- Solr 排除查询
前言 solr排除查询也就是我们在数据库和程序中经常处理的不等于,solr的语法是在定语前加[-].. StringBuilder sbHtml=new StringBuilder(); shBhtm ...
- Solr高级查询Facet
一.什么是facet solr种以导航为目的的查询结果成为facet,在用户查询的结果上根据分类增加了count信息,然后用户根据count信息做进一步搜索. facet主要用于导航实现渐进式精确搜索 ...
- [Solr] (源) Solr与MongoDB集成,实时增量索引
一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中,直接向Solr服务发送请求,返回xml.js ...
- sorl6.0+jetty+mysql搭建solr服务
1.下载solr 官网:http://lucene.apache.org/solr/ 2.目录结构如下 3.启动solr(默认使用jetty部署) 在path路径下将 bin文件夹对应的目录加入,然后 ...
- Solr Facet 默认值
前言 今天在用Solr Facet遇到了默认值的问题,我用Facet.field查询发现数据总共100条,刚开始没有注意,发现少个别数据,但是用这几个个别的id查询又能查出来数据.才发现是Facet默 ...
- solr添加多个core
在D:\solr\solr_web\solrhome文件夹下: 1)创建core0文件夹 2)复制D:\solr\solr_web\solrhome\configsets\basic_configs/ ...
- solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)
问题描述 报如下错误,很显然,问题原因:空指针异常: ERROR (localhost-startStop-1) [ ] o.a.s.h.d.s.SolrDataImportProperties ...
- Solr实战:使用Hue+Solr实现标签查询
公司最近在研究多条件组合查询方案,Google的一位技术专家Sam和我们讨论了几个备选方案. Sam的信: 我做了进一步研究,目前有这么几种做法: 1) 最直接粗暴,只做一个主index,比如按行业+ ...
- solr.net的使用
引子 最近在做一个日志系统,用普通关系型数据库做数据查询遇到了查询的瓶颈,想到了用成熟的搜索应用服务,我所知道的比较成熟的搜索应用服务有solr和es(elasticsearch),由于时间比较仓促, ...
随机推荐
- Swift使用AlamoFire超时设置和事件处理
一直在写swift项目,正好碰到服务器部署,请求超时或者请求失败的问题,页面就卡着不动了.顺手解决一下吧 差了些资料,说要设置超时时间 方法一: static let sharedSessionMan ...
- WPF编程,C#中弹出式对话框 MessageBox 的几种用法。
原文:WPF编程,C#中弹出式对话框 MessageBox 的几种用法. 1.MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息. 2.Mes ...
- vim打开多窗口、多文件之间的切换
打开多个文件: 一.vim还没有启动的时候: 1.在终端里输入 vim file1 file2 ... filen便可以打开所有想要打开的文件 2.vim已经启动 输入 :e file 可以再打开一 ...
- Egret(白鹭引擎)——“TypeError: Cannot read property 'asCom' of null”
前言 相信我,这个错误新手都不陌生:TypeError: Cannot read property 'asCom' of null 还有,一定要看我上一篇,哦不(人家应该是报了这个错,才找到看到这篇文 ...
- HTML 表格实例
1.表格这个例子演示如何在 HTML 文档中创建表格. <p>每个表格由 table 标签开始.</p><p>每个表格行由 tr 标签开始.</p>&l ...
- 整理一些常用的前端CND加速库,VUE,Jquery,axios
VUE https://cdn.staticfile.org/vue/2.2.2/vue.min.js Jquery https://cdn.bootcss.com/jquery/3.4.0/jque ...
- 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard
目录 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard 正文 对我的启发 机器学习英雄访谈录之 DL 自由职业者:Tuatini Godard Sanyam Bhutani 是 M ...
- (1) English Learning
1. no-brainer 不必花脑筋的事物 This tool is really no-brainer that almost everyone can use it. 这个工具太简单用了,不会 ...
- TimelineJS JSON 数据格式 - 译文 [原创]
TimelineJS 是用于绘制时间轴的 Javascript 开源脚本,目前是 TimelineJS3 版.参阅 https://github.com/NUKnightLab/TimelineJS3 ...
- Cloud Native Weekly | Kubernetes 1.13发布
云原生一周精选 1——Kubernetes 1.13发布 2——Kubernetes首次出现重大安全漏洞 3——Docker和微软公司推出云原生应用的部署规范 4——谷歌推出beta版本的Cloud ...