写一篇关于solr,solrcloud和,activemq的博客————第一弹solr

这三个货怎么理解呢?我今天就以一个通俗易懂的方式理解下:

第一:solr

关于solr的理解?
这个是对外提供的是HTTP服务,所以我们对外只需要提供solrJ就可以了!
solrJ是个客户端,通过这个客户端就可以访问solr。
还有我们就是访问的时候输入千奇百怪的词语,但是不可能像数据库那么样查找啊!所以我们就用了中文分词,(后面会配置!)
1:我为啥要在我的项目上搭建solr?
简单来说就是来实现搜索的功能,我们可以发现我们访问任何一个网商项目首页基本是这样的例如京东:

https://www.jd.com/?cu=true&utm_source=baidu-pinzhuan&utm_medium=cpc&utm_campaign=t_288551095_baidupinzhuan&utm_term=0f3d30c8dba7459bb52f2eb5eba8ac7d_0_8729441e92534544b3af272afe8f6f4b

但是当我们搜素商品的时候我们可以发现的是我们的网址变成了这样的:

https://search.jd.com/Search?keyword=%E7%9A%AE%E9%9E%8B&enc=utf-8&suggest=2.def.0.V15&wq=%E7%9A%AExie&pvid=b093ebbff96a4f74a51ad0b2c9947071

这样可以发现两个的服务器是不一样的。说明是两个不同的工程。

2:就是在linux搭建solr服务。
我们来看下这个solr服务搭建过程中的几个重要点。
(1)解压压缩包,里面基本的目录
bin,里面各种可执行的命令,里面可以直接执行,但是直接运行在jetty上,类似tomcat,所以就是和tomcat来整合咯!吐槽一下,这货就是一个war包,然后你就懂得的吧!
solrJ的一个jar包,这货就是客户端!
solrhome 配置文件都放在这里。
(2)整合tomcat,将dis里面的war包放在了tomcat的webApps里面。并将一些记录文件也放进去,
(3)配置solrhome,要修改solr工程里面的web.xml。就是告诉solr里面solrhome位置。
接下来启动tomcat就可以访问solrJ的管理界面了。
3:管理界面的一些介绍。
我在这里放一个比较好的博客链接吧!

https://blog.csdn.net/zcl_love_wx/article/details/52092098

讲的很细致!

4:solr和core的理解
solr可以对多个core进行综合管理,并接受请求选择特定的一个或者多个core执行相关任务。
下面来回答什么是solr的core。
core从文件结构的角度来看的话,主要包括
一份索引(也可能还包括拼写检查的索引)、一堆配置文件。
最主要的配置文件是:solrconfig.xml和schema.xml。
老规矩 放链接

https://blog.csdn.net/sswqzx/article/details/84501215

5:设置中文分词配置和业务域的定义这个比较重要,我当初也不怎么理解
中文分词配置好理解,配置就好了。

在将Ik分析器配置的时候已经配置好了业务域类型和业务域。如下:

<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
上面的这一段时我们自己定义的中文分词器。这个是域的定义和类型。
下面就是自己定义的域的类型
id需要定义吗?solr里面是定义好了的。
type里面就是我们定义的类型。分类名称不分词
indexed
<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="long" 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_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"/>

6 solrJ这个问题boss流程
导包

//添加测试
 @Test
	public void testAddDocument() throws Exception {
		//创建一个SolrServer对象。创建一个HttpSolrServer对象
		//需要指定solr服务的url
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
		//创建一个文档对象SolrInputDocument
		SolrInputDocument document = new SolrInputDocument();
		//向文档中添加域,必须有id域,域的名称必须在schema.xml中定义
		document.addField("id", "123");
		document.addField("item_title", "测试商品3");
		document.addField("item_price", 1000);
		//把文档对象写入索引库
		solrServer.add(document);
		//提交
		solrServer.commit();
	}
	//删除
	@Test
	public void deleteDocumentById() throws Exception {
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
		solrServer.deleteById("test001");
		//提交
		solrServer.commit();
	}
	/删除的另外一种方式
	@Test
	public void deleteDocumentByQuery() throws Exception {
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
		solrServer.deleteByQuery("item_title:测试商品3");
		solrServer.commit();
	}
	//
	@Test
	public void searchDocumet() throws Exception {
		//创建一个SolrServer对象
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
		//创建一个SolrQuery对象
		SolrQuery query = new SolrQuery();
		//设置查询条件、过滤条件、分页条件、排序条件、高亮
		//query.set("q", "*:*");
		query.setQuery("手机");
		//分页条件
		query.setStart(0);
		query.setRows(10);
		//设置默认搜索域
		query.set("df", "item_keywords");
		//设置高亮
		query.setHighlight(true);
		//高亮显示的域
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<div>");
		query.setHighlightSimplePost("</div>");
		//执行查询,得到一个Response对象
		QueryResponse response = solrServer.query(query);
		//取查询结果
		SolrDocumentList solrDocumentList = response.getResults();
		//取查询结果总记录数
		System.out.println("查询结果总记录数:" + solrDocumentList.getNumFound());
		for (SolrDocument solrDocument : solrDocumentList) {
			System.out.println(solrDocument.get("id"));
			//取高亮显示
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String itemTitle = "";
			if (list != null && list.size() >0) {
				itemTitle = list.get(0);
			} else {
				itemTitle = (String) solrDocument.get("item_title");
			}
			System.out.println(itemTitle);
			System.out.println(solrDocument.get("item_sell_point"));
			System.out.println(solrDocument.get("item_price"));
			System.out.println(solrDocument.get("item_image"));
			System.out.println(solrDocument.get("item_category_name"));
			System.out.println("=============================================");
		}
		
	}


但是上面的只是测试,我们实际上要把数据库查询到的数据放到solr里面的具体实现呢?

/**
 * 商品数据导入索引库
 * <p>Title: SearchItemServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
 //向容器加入bean
@Service
public class SearchItemServiceImpl implements SearchItemService {
//这个的作用是mapper你懂的
	@Autowired
	private SearchItemMapper searchItemMapper;
	//这货是solrServer在xml文件中配置了。
	@Autowired
	private SolrServer solrServer;
	
	@Override
	public TaotaoResult importItemsToIndex() {
		try {
		//我说下下面的几个的意思,下面的就是和上面的一样,查到数据后,将list里面的每一个都放在了SolrInputDocument对象里面,然后放在了solrServer里面。
			//1、先查询所有商品数据,这个就是和list一样
			List<SearchItem> itemList = searchItemMapper.getItemList();
			//2、遍历商品数据添加到索引库
			for (SearchItem searchItem : itemList) {
				//创建文档对象
				SolrInputDocument document = new SolrInputDocument();
				//向文档中添加域
				document.addField("id", searchItem.getId());
				document.addField("item_title", searchItem.getTitle());
				document.addField("item_sell_point", searchItem.getSell_point());
				document.addField("item_price", searchItem.getPrice());
				document.addField("item_image", searchItem.getImage());
				document.addField("item_category_name", searchItem.getCategory_name());
				document.addField("item_desc", searchItem.getItem_desc());
				//把文档写入索引库
				solrServer.add(document);
			}
			//3、提交
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, "数据导入失败");
		}
		//4、返回添加成功
		return TaotaoResult.ok();
	}

猜你喜欢

转载自blog.csdn.net/weixin_37647123/article/details/87834680