Solr安装linux(centos7)以及使用SolrJ可以实现索引库的增删改查操作学习

因商城项目用到,所以学习。

复制solrwar包到linux下(solr版本不一样,我这里就不推荐了,jdk1.7以上必须有)

将其单放在solr目录下

solr.war 部署在tomcat


启动tomcat


项目发布成功就有此项目,删除solr.war(注意要关闭tomcat)


项目还需要几个日志jar包不然会报错。


复制solr项目


usr/local/solr目录下


更改配置文件告诉它solrhome在哪个位置

vi tomcat/webapps/solr/WEB-INF/web.xml



启动tomcat

1我启动错了,搞了半个小时才发现。







Solr业务域的配置:

复制IKlinux下。



[root@localhost IK Analyzer 2012FF_hf1]# cpIKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/

 

[root@localhost IK Analyzer 2012FF_hf1]#mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes

把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下:

 

[root@localhost IK Analyzer 2012FF_hf1]# cpext_stopword.dic IKAnalyzer.cfg.xml

mydict.dic/usr/local/solr/tomcat/webapps/solr/WEB-INF/classes


要使中文分析器生效还需配置:

按G查看页底

添加FieldType:

<fieldType name="text_ik" class="solr.TextField">

  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>

</fieldType>

配置业务域,type制定使用自定义的FieldType。

设置业务系统Field(更具项目要求name可以更改)

<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"/>





重启tomcat




1.  使用solrJ管理索引库

使用SolrJ可以实现索引库的增删改查操作。

1.1. 添加文档

第一步:把solrJjar包添加到工程中。

第二步:创建一个SolrServer,使用HttpSolrServer创建对象。

第三步:创建一个文档对象SolrInputDocument对象。

第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。

第五步:把文档添加到索引库中。

第六步:提交。

// 第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。

@Test
	public void addDocument() throws Exception {
		// 1、把solrJ的jar包添加到工程。
		// 2、创建一个SolrServer对象。创建一个和sorl服务的连接。HttpSolrServer。
		//如果不带Collection默认连接Collection1
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
		// 3、创建一个文档对象。SolrInputDocument。
		SolrInputDocument document = new SolrInputDocument();
		// 4、向文档对象中添加域。必须有一个id域。而且文档中使用的域必须在schema.xml中定义。
		document.addField("id", "test001");
		document.addField("item_title", "测试商品");
		// 5、把文档添加到索引库
		solrServer.add(document);
		// 6、Commit。
		solrServer.commit();
	}


删除文档
根据id删除

	
第一步:创建一个SolrServer对象。
第二步:调用SolrServer对象的根据id删除的方法。
第三步:提交。

@Test

     publicvoid deleteDocumentById() throws Exception {

          // 第一步:创建一个SolrServer对象。

          SolrServer solrServer = newHttpSolrServer("http://192.168.25.128:8080/solr");

          // 第二步:调用SolrServer对象的根据id删除的方法。

          solrServer.deleteById("1");

          // 第三步:提交。

          solrServer.commit();

     }

  根据查询删除


@Test
	public void deleteDocumentByQuery() throws Exception {
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr");
		solrServer.deleteByQuery("title:change.me");
		solrServer.commit();
	}

1.1. 查询索引库

查询步骤:

第一步:创建一个SolrServer对象

第二步:创建一个SolrQuery对象。

第三步:向SolrQuery中添加查询条件、过滤条件。。。

第四步:执行查询。得到一个Response对象。

第五步:取查询结果。

第六步:遍历结果并打印。

 

1.1.1.   简单查询


@Test
	public void queryDocument() throws Exception {
		// 第一步:创建一个SolrServer对象
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr");
		// 第二步:创建一个SolrQuery对象。
		SolrQuery query = new SolrQuery();
		// 第三步:向SolrQuery中添加查询条件、过滤条件。。。
		query.setQuery("*:*");
		// 第四步:执行查询。得到一个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"));
			System.out.println(solrDocument.get("item_title"));
			System.out.println(solrDocument.get("item_price"));
		}
	}
 
 
带高亮显示
@Test
	public void queryDocumentWithHighLighting() throws Exception {
		// 第一步:创建一个SolrServer对象
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
		// 第二步:创建一个SolrQuery对象。
		SolrQuery query = new SolrQuery();
		// 第三步:向SolrQuery中添加查询条件、过滤条件。。。
		query.setQuery("测试");
		//指定默认搜索域
		query.set("df", "item_keywords");
		//开启高亮显示
		query.setHighlight(true);
		//高亮显示的域
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em>");
		query.setHighlightSimplePost("</em>");
		// 第四步:执行查询。得到一个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 = null;
			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_price"));
		}
	}











猜你喜欢

转载自blog.csdn.net/qq_39136928/article/details/80578598
今日推荐