标签:实现 sid mapping 使用 配置文件 frame inux sch jms
1.基于标准的开放接口:Solr搜索服务器支持通过XML、JSON和HTTP查询和获取结果。
2.易管理:Solr可以通过HTML页面管理,Solr配置通过XML完成。
3.可伸缩性:能够有效地复制到另外一个Solr搜索服务器。
4.灵活的插件体系:新功能能够以插件的形式方便的添加到Solr服务器上。
5.强大的数据导入功能:数据库和其他结构化数据源现在都可以导入、映射和转化。
?
解压tomcat
mkdir /usr/local/solr
cd /usr/local/solr
tar -zxvf apache-tomcat-8.5.32.tar.gz
mv apache-tomcat-8.5.32 tomcat
有可能会存在tomcat在centos7中启动缓慢的现象
安装:yum -y install rng-tools
开启 rng-tools服务:service rngd start
解压solr
tar -zxvf solr-4.10.3.tar
解压IK
unzip IKAnalyzer.zip
复制solr.war到tomcat/webapp下
cd /usr/local/solr/solr-4.10.3/example/webapps/
cp solr.war /usr/local/solr/tomcat/webapps/
启动tomcat让solr.war自动解压
/usr/local/solr/tomcat/bin
./startup.sh
关闭tomcat后才能删除solr.war包,否则会连带solr应用一起删除
/usr/local/solr/tomcat/bin
./shutdown.sh
进入到webapps删除solr.war包
cd /usr/local/solr/tomcat/webapps/
rm -rf solr.war
将solr-4.10.3/example/lib/ext/目录下的所有jar包拷贝到/usr/local/solr/tomcat/webapps/solr/WEB-INF/lib目录中
cd /usr/local/solr/solr-4.10.3/example/lib/ext
cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib
将solr-4.10.3/example/目录下的solr文件夹复制到/usr/local/solr/目录下,并且返回到/usr/local/solr目录下将solr重命名为solrhome
cd /usr/local/solr/solr-4.10.3/example/
cp -r solr /usr/local/solr/
cd /usr/local/solr
mv solr solrhome
配置tomcat/webapps/solr/WEB-INF/web.xml家的位置
cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/
vim web.xml
添加solrhome(/usr/local/solr/solrhome)
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/usr/local/solr/solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
启动tomcat
/usr/local/solr/tomcat/bin/
./startup.sh
把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目录下
cd /usr/local/solr/IKAnalyzer/
cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
创建WEB-INF/classes文件夹
cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/
mkdir classes
把扩展词典、停用词词典、配置文件放到 solr 工程的 WEB-INF/classes 目录下
cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
cp /usr/local/solr/IKAnalyzer/IKAnalyzer.cfg.xml ./
cp /usr/local/solr/IKAnalyzer/ext_stopword.dic ./
将ext_stopword.dic文件名修改为stopword.dic
mv ext_stopword.dic stopword.dic
修改IKAnalyzer.cfg.xml配置文件
stopword.dic已经有了,ext.dic还没有
cd /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
vi IKAnalyzer.cfg.xml
:wq
<?xml version=1.0" encoding='UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IKAnalyzer扩展配置</comment>
<--用户可以在这里配置自己的扩展字典-->
<entry key="ext_dict">ext.dic;</entry>
<--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">stopword.dic</entry>
</properties>
创建ext.dic
touch ext.dic
stopword.dic-停止词典
切分词的时候, 凡是出现在停止词典中的词都会被过滤掉.
ext.dic-扩展词典
凡是专有名词都会放到这里, 如果自然语义中不是一个词, 放到这里后solr
切分词的时候就会切分成一个词.
配置分词器
修改 Solrhome 的 schema.xml 文件
cd /usr/local/solr/solrhome/collection1/conf
vim schema.xml
在最后添加
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
自定义域名使用自己创建的分词器
<field name="content_ik" type="text_ik" indexed="true" stored="true"/>
配置完毕后重启tomcat
field普通域
大多数情况都可以用这个域来完成, 主要定义了域名和域的类型.
copyField复制域
复制域中有source叫做源域, dest代表目标域,
在维护数据的时候, 源域中的内容会复制到目标域中一份,
从目标域中搜索, 就相当于从多个源域中搜索一样
dynamicField动态域
solr中域名要先定义后使用, 没有定义就使用会报错,
起到模糊匹配的效果,可以模糊匹配没有定义过的域名,相当于通配符的作用
uniqueKey主键域
在添加数据的时候必须有主键域, 没有会报错, 这个不用添加也不用修改, 就使用这个默认的域名id就可以.
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<field name="content_ik" type="text_ik" indexed="true" stored="true"/>
<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" />
<field name="item_updatetime" type="date" indexed="true" stored="true" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
动态域
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
<!-- solr客户端 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solrj.version}</version>
</dependency>
?
//修改时, 会把以前的内容删除, 然后再添加
@Test
public void testIndexCreateAndUpdate() throws Exception {
/**
* 创建和solr服务器连接
* http://192.168.1.88:8080/solr是连接的默认实例也就是collection1实例
* http://192.168.1.88:8080/solr/collection2实例
*/
SolrServer solrServer = new HttpSolrServer("http://192.168.1.88:8080/solr");
//创建文档对象
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "002");
doc.addField("title", "后裔");
doc.addField("price", "250");
//添加或者修改
solrServer.add(doc);
//提交
solrServer.commit();
}
}
?
@Test
public void testIndexSearch() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.1.88:8080/solr");
//创建查询对象
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery("*:*");
//查询并返回响应
QueryResponse queryResponse = solrServer.query(query);
//从响应中获取结果集
SolrDocumentList results = queryResponse.getResults();
System.out.println("count=" + results.getNumFound());
for (SolrDocument result : results) {
System.out.println("id=" + result.get("id"));
System.out.println("title=" + result.get("title"));
}
}
@Test
public void testIndexDelete() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.1.88:8080/solr");
//单个删除
//solrServer.deleteById("001");
//删除所有
solrServer.deleteByQuery("*:*");
//提交
solrServer.commit();
}
<properties>
<spring.version>5.1.7.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<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>5.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<?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服务器地址 -->
<solr:solr-server id="solrServer" url="http://192.168.1.88:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
import lombok.Data;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.Dynamic;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
@Data
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 商品id,同时也是商品编号
*/
@Field
private Long id;
/**
* 商品标题
*/
@Field("item_title")
private String title;
/**
* 商品卖点
*/
private String sellPoint;
/**
* 商品价格,单位为:元
*/
@Field("item_price")
private BigDecimal price;
private Integer stockCount;
/**
* 库存数量
*/
private Integer num;
/**
* 商品条形码
*/
private String barcode;
/**
* 商品图片
*/
@Field("item_image")
private String image;
/**
* 所属类目,叶子类目
*/
private Long categoryid;
/**
* 商品状态,1-正常,2-下架,3-删除
*/
private String status;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
@Field("item_updatetime")
private Date updateTime;
private String itemSn;
private BigDecimal costPirce;
private BigDecimal marketPrice;
private String isDefault;
@Field("item_goodsid")
private Long goodsId;
private String sellerId;
private String cartThumbnail;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;
public Map<String, String> getSpecMap() {
return specMap;
}
public void setSpecMap(Map<String, String> specMap) {
this.specMap = specMap;
}
}
<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" />
<field name="item_updatetime" type="date" indexed="true" stored="true" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
?
cd /usr/local/solr/solrhome/collection1/conf
vim schema.xml
<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" />
<field name="item_updatetime" type="date" indexed="true" stored="true" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
?
/usr/local/solr/tomcat/bin/shutdown.sh
/usr/local/solr/tomcat/bin/startup.sh
public class Item implements Serializable {
/**
* 商品id,同时也是商品编号
*/
@Field
private Long id;
/**
* 商品标题
*/
@Field("item_title")
private String title;
/**
* 商品卖点
*/
private String sellPoint;
/**
* 商品价格,单位为:元
*/
@Field("item_price")
private BigDecimal price;
private Integer stockCount;
/**
* 库存数量
*/
private Integer num;
/**
* 商品条形码
*/
private String barcode;
/**
* 商品图片
*/
@Field("item_image")
private String image;
/**
* 所属类目,叶子类目
*/
private Long categoryid;
/**
* 商品状态,1-正常,2-下架,3-删除
*/
private String status;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
private String itemSn;
private BigDecimal costPirce;
private BigDecimal marketPrice;
private String isDefault;
@Field("item_goodsid")
private Long goodsId;
private String sellerId;
private String cartThumbnail;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
private static final long serialVersionUID = 1L;
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-solr.xml")
public class TestIndexManager {
@Autowired
private SolrTemplate solrTemplate;
//添加更新
@Test
public void testIndexCreatAndUpdate() {
List<Item> itemList = new ArrayList<Item>();
for(long i= 1; i < 100; i++) {
Item item = new Item();
item.setId(i);
item.setTitle("鲁班" + i);
item.setCategory("射手");
item.setPrice(new BigDecimal("100"+i));
item.setBrand("王者");
itemList.add(item);
}
//保存
//solrTemplate.saveBean(itemList);
solrTemplate.saveBeans(itemList);
//提交
solrTemplate.commit();
}
//删除
@Test
public void testIndexDelte() {
//根据主键域id删除
//solrTemplate.deleteById("1");
//创建查询对象
Query query = new SimpleQuery("*:*");
//根据查询条件删除
solrTemplate.delete(query);
//提交
solrTemplate.commit();
}
//查询
@Test
public void testSearch() {
//创建查询对象
//Query query = new SimpleQuery("*:*");
//创建查询对象
Query query = new SimpleQuery();
//创建查询条件对象, 注意这里的Criteria对象和mybatis中的那个不是同一个, 只是同名而已
Criteria criteria = new Criteria("item_title").contains("鲁班");
//查询对象中放入查询条件
query.addCriteria(criteria);
//从第几条开始查询
query.setOffset(11);
//设置每页查询多少条数据
query.setRows(20);
//查询并返回结果
ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
//总页数
int totalPages = items.getTotalPages();
//查询到的总记录数
long totalElements = items.getTotalElements();
//查询到的数据集合
List<Item> content = items.getContent();
//每页有多少条数据
int numberOfElements = items.getNumberOfElements();
System.out.println("==========");
System.out.println(content);
}
}
?
<dependencies>
<dependency>
<groupId>com.itxk</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- solr -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.RELEASE</version>
</dependency>
<?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服务器地址 -->
<solr:solr-server id="solrServer" url="http://192.168.1.88:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.fmjava.core.util"></context:component-scan>
</beans>
@Data
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 商品id,同时也是商品编号
*/
@Field
private Long id;
/**
* 商品标题
*/
@Field("item_title")
private String title;
/**
* 商品卖点
*/
private String sellPoint;
/**
* 商品价格,单位为:元
*/
@Field("item_price")
private BigDecimal price;
private Integer stockCount;
/**
* 库存数量
*/
private Integer num;
/**
* 商品条形码
*/
private String barcode;
/**
* 商品图片
*/
@Field("item_image")
private String image;
/**
* 所属类目,叶子类目
*/
private Long categoryid;
/**
* 商品状态,1-正常,2-下架,3-删除
*/
private String status;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
@Field("item_updatetime")
private Date updateTime;
private String itemSn;
private BigDecimal costPirce;
private BigDecimal marketPrice;
private String isDefault;
@Field("item_goodsid")
private Long goodsId;
private String sellerId;
private String cartThumbnail;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
private String spec;
@Field("item_seller")
private String seller;
@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;
public Map<String, String> getSpecMap() {
return specMap;
}
public void setSpecMap(Map<String, String> specMap) {
this.specMap = specMap;
}
}
@Component
public class DataImportToSolr {
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private ItemDao itemDao;
public void importItemDataToSolr() {
ItemQuery query = new ItemQuery();
ItemQuery.Criteria criteria = query.createCriteria();
criteria.andStatusEqualTo("1");
List<Item> items = itemDao.selectByExample(query);
if (items != null) {
for (Item item : items) {
//获取规格json格式字符串
String specJsonStr = item.getSpec();
Map map = JSON.parseObject(specJsonStr, Map.class);
item.setSpecMap(map);
}
//保存
solrTemplate.saveBeans(items);
//提交
solrTemplate.commit();
}
}
public static void main(String[] args) {
ApplicationContext contexnt =
new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
DataImportToSolr bean = (DataImportToSolr)contexnt.getBean("dataImportToSolr");
bean.importItemDataToSolr();
}
}
标签:实现 sid mapping 使用 配置文件 frame inux sch jms
原文地址:https://www.cnblogs.com/mumuyinxin/p/11708121.html