05、Solr实战:solrj实现对索引的增删改查

一、环境

1、 IDEA;

2、 JDK1.8;

3、 CDH5.7.0;

二、构建开发环境

1、 创建Maven工程,引入solr相关包;

<!--solr-->
<dependency>
	<groupId>org.apache.solr</groupId>
	<artifactId>solr-solrj</artifactId>
	<version>4.10.3-cdh5.7.0</version>
</dependency>

最终pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>BG</groupId>
    <artifactId>Hadoop</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>
    <properties>
        <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
        <hbase.version>1.2.0-cdh5.7.0</hbase.version>
        <solr.version>4.10.3-cdh5.7.0</solr.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--            <dependency>-->
        <!--                <groupId>org.apache.hadoop</groupId>-->
        <!--                <artifactId>hadoop-common</artifactId>-->
        <!--                <version>2.5.0-cdh5.2.0</version>-->
        <!--            </dependency>-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <!--solr-->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>${solr.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

三、功能代码

1、 增删改;

package solr;

import Utils.ReadtxtUtils;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
//import org.junit.Test;

public class IndexManager {

	/**
	 * 创建索引初始版本
	 * @throws Exception
	 */
	public void IndexCreate() throws Exception{
		//创建和Solr服务端连接
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr/collection1");
		
		//创建solr文档对象
		SolrInputDocument doc = new SolrInputDocument();
		//域要先定义后使用,还有注意必须要有id主键域
		//solr中没有专用的修改方法,更新其实就是,先删除同一id的索引,然后,再添加
		doc.addField("id", "a002");
		doc.addField("product_name", "台灯1111");
		doc.addField("product_price", "12.5");
		doc.addField("product_description", "12.5");

		//将文档加入solrServer对象中
		solrServer.add(doc);
		//提交
		solrServer.commit();
	}
	/**
	 * 添加抽象化参数
	 * @param id 文件名称
	 * @param product_name
	 * @param product_price
	 * @param product_description 文件描述内容
	 * @throws Exception
	 */
	public void IndexCreate(String id,String product_name,String product_price,String product_description) throws Exception{
		//创建和Solr服务端连接
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr/collection1");

		//创建solr文档对象
		SolrInputDocument doc = new SolrInputDocument();
		//域要先定义后使用,还有注意必须要有id主键域
		//solr中没有专用的修改方法, 会自动根据id进行查找,如果找到了则删除原来的将新的加入就是修改,如果没找到,将新的直接加入则就是新增
		doc.addField("id", id);
		doc.addField("product_name", product_name);
		doc.addField("product_price", product_price);
		doc.addField("product_description", product_description);

		//将文档加入solrServer对象中
		solrServer.add(doc);

		//提交
		solrServer.commit();
	}

	/**
	 * 删除文档
	 * @throws Exception
	 */
	public void IndexDel() throws Exception{
		//创建和Solr服务端连接
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr/collection1");
		
		//根据主键id进行删除
//		solrServer.deleteById("a002");
		
		//根据查询删除,这里是删除所有*:*
		solrServer.deleteByQuery("*:*");
		//提交
		solrServer.commit();
	}

	public static void main(String[] args) throws Exception {

		//readToString方法,将txt文档内容读出来
		String filecontent=new ReadtxtUtils().readToString("datas/背影-朱自清.txt");
		new IndexManager().IndexCreate("article001","ip/opt/datas/影-朱自清.txt","/opt/datas/影-朱自清.txt",filecontent);
	}
}

2、 查询;

package solr;

import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
//import org.junit.Test;

public class IndexSearch {
	public static void main(String[] args) throws Exception{

		String key="attr_content";//attr_content
		String value="*";

		new IndexSearch().IndexSearchByQuery(key,value);
	}

	/**
	 * 通用查询原始
	 * @throws Exception
	 */
	public void IndexSearch1() throws Exception{
		//连接solr服务端
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr/collection1");
		
		//创建solr查询条件对象
		SolrQuery solrQuery = new SolrQuery();
		//查询所有
		solrQuery.setQuery("*:*");
		
		//查询并获取查询响应对象
		QueryResponse queryResponse = solrServer.query(solrQuery);
		//从查询响应中获取查询结果集对象
		SolrDocumentList results = queryResponse.getResults();
		//打印一共查询到多少条记录,也就是记录总数
		System.out.println("====================================================");
		System.out.println("=====count:" + results.getNumFound());
		//遍历查询结果集
		for(SolrDocument doc : results){
			System.out.println("============="+doc.get("id"));
			System.out.println("============="+doc.get("product_name"));
//			System.out.println("============="+doc.get("product_price"));
			System.out.println("====================================================");
		}
	}

	/**
	 * 修改依据key:value查询
	 * @param key
	 * @param value
	 * @throws Exception
	 */
	public void IndexSearchByQuery(String key,String value) throws Exception{
		//连接solr服务端
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr/collection2");
		
		//创建solr查询条件对象
		SolrQuery solrQuery = new SolrQuery();
		//查询所有
		solrQuery.setQuery(key+":"+value);
		//查询并获取查询响应对象
		QueryResponse queryResponse = solrServer.query(solrQuery);
		//从查询响应中获取查询结果集对象
		SolrDocumentList results = queryResponse.getResults();
		//打印一共查询到多少条记录,也就是记录总数
		System.out.println("====================================================");
		System.out.println("=====count:" + results.getNumFound());
		//遍历查询结果集
		for(SolrDocument doc : results){
			System.out.println("==============UUID : "+doc.get("id"));
			System.out.println("=======Origin Path : "+doc.get("path"));
			System.out.println("===========Content : "+doc.get("attr_content"));
			System.out.println("====================================================");
		}
	}

	/**
	 * 复杂查询原始
	 * 过滤、排序、条数+分页、高亮
	 * @throws Exception
	 */
	public void IndexSearch2() throws Exception{
		//连接solr服务端
		SolrServer solrServer = new HttpSolrServer("http://hadoop01:9999/solr");
		
		//创建solr查询条件对象
		SolrQuery solrQuery = new SolrQuery();
		//查询关键字输入
		solrQuery.setQuery("背影");
		//设置默认搜索域
		solrQuery.set("df", "product_keywords");
		//设置过滤查询
		solrQuery.addFilterQuery("product_price:[1 TO 100]");
		//设置排序,这里是降序
		solrQuery.setSort("product_price", ORDER.desc);
		//=======设置分页========
		//设置起始条数
		solrQuery.setStart(0);
		//设置查询多少条
		solrQuery.setRows(50);
	
		//========设置高亮显示=======
		//高亮默认是关闭的,所以要手动开启
		solrQuery.setHighlight(true);
		//设置需要高亮显示的域
		solrQuery.addHighlightField("product_name");
		//设置高亮前缀
		solrQuery.setHighlightSimplePre("<span style=\"color:red\">");
		//设置高亮后缀
		solrQuery.setHighlightSimplePost("</span>");
		
		//===================查询并获取查询响应对象=====================================
		QueryResponse queryResponse = solrServer.query(solrQuery);
		//从查询响应中获取查询结果集对象
		SolrDocumentList results = queryResponse.getResults();
		//打印一共查询到多少条记录,也就是记录总数
		System.out.println("=====count====" + results.getNumFound());
		//遍历查询结果集
		for(SolrDocument doc : results){
			System.out.println("============="+doc.get("id"));
			//获取高亮
			Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
			List<String> list = highlighting.get(doc.get("id")).get("product_name");
			if(list != null && list.size() > 0){
				String hlName = list.get(0);
				System.out.println("=======high lighting=====" + hlName);
			}
			System.out.println("============="+doc.get("product_name"));
			System.out.println("============="+doc.get("product_price"));
			System.out.println("====================================================");
		}
	}
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: