欢迎投稿

今日深度:

solr学习,

solr学习,


一. 下载solr

http://mirror.bit.edu.cn/apache/lucene/solr/6.6.0

二. 下载tomcat

http://tomcat.apache.org/

三. 搭建solr

1. 将solr-6.6.0\server\solr-webapp\webapp下的内容copy到 tomcat-8.5.15-solr\webapps\solr目录中
2. 将solr-6.6.0\server\lib\ext下的jar包copy到tomcat-8.5.15-solr\webapps\solr\WEB-INF\lib目录中
3. 将solr-6.6.0\server\lib下的metrics-*-3.2.2.jar复制到tomcat-8.5.15-solr\webapps\solr\WEB-INF\lib目录中
4. 将solr-6.6.0\server\resources\log4j.properties复制到tomcat-8.5.15-solr\webapps\solr\WEB-INF\classes目录中

5. 配置solr-core(可根据功能模块配置多个core,减轻solr服务器压力) 5.1 在tomcat-8.5.15-solr\solr\solrhome下创建bin和core01目录 5.2 在tomcat-8.5.15-solr\solr\solrhome\core01目录内创建data目录和core.properties文件(添加name=core01)
5.3 将solr-6.6.0\server\solr下的[文件]复制到tomcat-8.5.15-solr\solr\solrhome 5.4 将solr-6.6.0\server\solr\configsets\sample_techproducts_configs下的conf目录复制到tomcat-8.5.15-solr\solr\solrhome\core01

6. 修改tomcat-8.5.15-solr\webapps\solr\WEB-INF\web.xml 6.1
    <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>D:/tomcat-8.5.15-solr/solr/solrhome</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
   </env-entry>
6.2
   <security-constraint>
    <web-resource-collection>
      <web-resource-name>Disable TRACE</web-resource-name>
      <url-pattern>/</url-pattern>
      <http-method>TRACE</http-method>
	  <http-method>DELETE</http-method>
      <http-method>GET</http-method>
      <http-method>POST</http-method>
      <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>manager</role-name>
    </auth-constraint>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
   <login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>default</realm-name>
    </login-config>

7. 修改tomcat-8.5.15-solr\conf\tomcat-users.xml
    <user username="admin" password="admin" roles="admin,manager"/>

8. 访问http://localhost:8080/solr/index.html








四. 配置schema.xml

1. 将tomcat-8.5.15-solr\solr\solrhome\core01\conf目录下的managed-schema文件另存为schema.xml

五. 配置solrconfig.xml

1.修改tomcat-8.5.15-solr\solr\solrhome\core01\conf\solrconfig.xml的lib标签内容

六. 配置中文分词器

1.添加中文分词jar包IKAnalyzer2012FF_u1.jar到tomcat-8.5.15-solr\webapps\solr\WEB-INF\lib下

七. 配置dataimport

1.

八. solrj

1. 添加jar包 1.1将solr-6.6.0\dist\solr-solrj-6.6.0.jar添加到项目中 1.2将solr-6.6.0\dist\solrj-lib目录下的jar包添加到项目中 1.3将solr-6.6.0\server\lib\ext目录下的jar包添加到项目中 2. 添加或修改索引
	@Test
	public void insertAndUpdateIndex() throws Exception {
		// 创建HttpSolrServer
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr");
		// 创建Document对象
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", "c001");
		doc.addField("name", "solr test111");
		// 将Document对象添加到索引库
		server.add(doc);
		// 提交
		server.commit();
	}
3. 删除索引
	@Test
	public void deleteIndex() throws Exception {
		// 创建HttpSolrServer
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr");
		// 根据指定的ID删除索引
		// server.deleteById("c001");
		// 根据条件删除
		server.deleteByQuery("id:c001");
		// 删除全部(慎用)
		server.deleteByQuery("*:*");
		// 提交
		server.commit();
	}
4. 查询索引
	@Test
	public void search01() throws Exception {
		// 创建HttpSolrServer
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr");
		// 创建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("=====================");
		}
	}

	@Test
	public void search02() throws Exception {
		// 创建HttpSolrServer
		HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr");
		// 创建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(10);

		// 设置显示的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("=====================");
		}
	}























www.htsjk.Com true http://www.htsjk.com/solr/30067.html NewsArticle solr学习, 一. 下载solr http://mirror.bit.edu.cn/apache/lucene/solr/6.6.0 二. 下载tomcat http://tomcat.apache.org/ 三. 搭建solr 1. 将solr-6.6.0\server\solr-webapp\webapp下的内容copy到 tomcat-8.5.15-solr\webapps\solr目录中...
相关文章
    暂无相关文章
评论暂时关闭