文档章节

Apache Solr 之 使用SolrJ操作索引库

空云万里晴
 空云万里晴
发布于 2014/12/09 19:57
字数 1229
阅读 6368
收藏 10

       Solrj是Solr搜索服务器的一个比较基础的客户端工具,可以非常方便地与Solr搜索服务器进行交互。最基本的功能就是管理Solr索引,包括添加、更新、删除和查询等。对于一些比较基础的应用,用Solj基本够用,而且你可以非常容易地通过使用Solrj的API实现与Solr搜索服务器进行交互,实现对Solr的基本管理功能。如果你的应用比较复杂,可以扩展Solrj来满足需要。

       使用 SolrJ操作索引库:

package com.hcm.solr.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.junit.Before;
import org.junit.Test;

import com.hcm.solr.entity.Message;

public class SolrTest {

	private static Log logger = LogFactory.getLog(SolrTest.class);

	private static final String URL = "http://127.0.0.1:8086/solr";

	private HttpSolrServer server = null;

	@Before
	public void init() {
		// 创建 server
		server = new HttpSolrServer(URL);
	}

	/**
	 * 添加文档
	 */
	@Test
	public void addDoc() {

		SolrInputDocument doc = new SolrInputDocument();

		doc.addField("id", "11");
		doc.addField("title", "this is my document !!");

		try {

			UpdateResponse response = server.add(doc);
			// 提交
			server.commit();

			logger.info("########## Query Time :" + response.getQTime());
			logger.info("########## Elapsed Time :" + response.getElapsedTime());
			logger.info("########## Status :" + response.getStatus());

		} catch (SolrServerException | IOException e) {
			logger.error("", e);
		}
	}

	/**
	 * 添加多个文档
	 */
	@Test
	public void addDocs() {

		String[] titles = new String[] { "aaaaaaa", "bbbbbbb", "ccccccc", "dddddd", "eeeeee" };

		List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();

		int i = 0;
		for (String str : titles) {
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("id", i++);
			doc.addField("title", str);
			docs.add(doc);
		}

		try {

			UpdateResponse response = server.add(docs);
			server.commit();

			logger.info("########## Query Time :" + response.getQTime());
			logger.info("########## Elapsed Time :" + response.getElapsedTime());
			logger.info("########## Status :" + response.getStatus());

		} catch (SolrServerException | IOException e) {
			logger.error("", e);
		}
	}

	/**
	 * 添加一个Entity到索引库
	 */
	@Test
	public void addBean() {

		Message msg = new Message("1001", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
				"Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
				"Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
				"Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
				"Fluentd to differentiate their products with better use of data." });

		try {

			UpdateResponse response = server.addBean(msg);
			server.commit();

			logger.info("########## Query Time :" + response.getQTime());
			logger.info("########## Elapsed Time :" + response.getElapsedTime());
			logger.info("########## Status :" + response.getStatus());

		} catch (SolrServerException | IOException e) {
			logger.error("", e);
		}
	}

	/**
	 * 添加多个Entity到索引库
	 */
	@Test
	public void addBeans() {

		List<Message> msgs = new ArrayList<Message>();

		Message msg = new Message("1001", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
				"Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
				"Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
				"Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
				"Fluentd to differentiate their products with better use of data." });

		Message msg2 = new Message("1002", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
				"Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
				"Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
				"Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
				"Fluentd to differentiate their products with better use of data." });

		msgs.add(msg);
		msgs.add(msg2);

		try {

			UpdateResponse response = server.addBeans(msgs);
			server.commit();

			logger.info("########## Query Time :" + response.getQTime());
			logger.info("########## Elapsed Time :" + response.getElapsedTime());
			logger.info("########## Status :" + response.getStatus());

		} catch (SolrServerException | IOException e) {
			logger.error("", e);
		}
	}

	/**
	 * 删除索引
	 */
	@Test
	public void deleteDoc() {
		try {
			server.deleteById("0");
			server.commit();
		} catch (SolrServerException | IOException e) {
			logger.error("", e);
		}
	}
	
	/**
	 * 更新索引<br>
	 * solr索引库不同于数据库,没有更新的功能。如果想更新,先通过id删除对应的文档,再添加新的文档。
	 */
	@Test
	public void updateDoc() {
		// ... ...
	}

	/**
	 * 查询
	 */
	@Test
	public void testQuery() {
		String queryStr = "*:*";
		SolrQuery params = new SolrQuery(queryStr);
		params.set("rows", 10);
		try {
			QueryResponse response = server.query(params);
			SolrDocumentList list = response.getResults();
			logger.info("########### 总共 : " + list.getNumFound() + "条记录");
			for (SolrDocument doc : list) {
				logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
			}
		} catch (SolrServerException e) {
			logger.error("", e);
		}
	}

	/**
	 * 简单查询(分页)
	 */
	@Test
	public void querySimple() {
		ModifiableSolrParams params = new ModifiableSolrParams();
		params.set("q", "this my");
		params.set("q.op", "and");
		params.set("start", 0);
		params.set("rows", 5);
		params.set("fl", "*,score");
		try {
			QueryResponse response = server.query(params);
			SolrDocumentList list = response.getResults();
			logger.info("########### 总共 : " + list.getNumFound() + "条记录");
			for (SolrDocument doc : list) {
				logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
			}
		} catch (SolrServerException e) {
			logger.error("", e);
		}
	}

	/**
	 * 查询(分页,高亮)
	 */
	@Test
	public void queryCase() {
		String queryStr = "title:this";
		SolrQuery params = new SolrQuery(queryStr);
		params.set("start", 0);
		params.set("rows", 5);

		// 启用高亮组件, 设置高亮
		params.setHighlight(true) 							
			.addHighlightField("title") 					
			.setHighlightSimplePre("<span class=\"red\">")
			.setHighlightSimplePost("</span>")
			.setHighlightSnippets(2)
			.setHighlightFragsize(1000)
			.setStart(0)
			.setRows(10)
			.set("hl.useFastVectorHighlighter", "true")
			.set("hl.fragsize", "200");

		try {
			QueryResponse response = server.query(params);
			SolrDocumentList list = response.getResults();
			logger.info("########### 总共 : " + list.getNumFound() + "条记录");
			for (SolrDocument doc : list) {
				logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
			}

			Map<String, Map<String, List<String>>> map = response.getHighlighting();
			Iterator<String> iterator = map.keySet().iterator();

			while (iterator.hasNext()) {
				String key = iterator.next();
				Map<String, List<String>> values = map.get(key);
				logger.info("############################################################");
				logger.info("############ id : " + key);

				for (Map.Entry<String, List<String>> entry : values.entrySet()) {
					String subKey = entry.getKey();
					List<String> subValues = entry.getValue();

					logger.info("############ subKey : " + subKey);
					for (String str : subValues) {
						logger.info("############ subValues : " + str);
					}
				}
				
			}
			
		} catch (SolrServerException e) {
			logger.error("", e);
		}
	}

}

POJO:

package com.hcm.solr.entity;

import java.io.Serializable;

import org.apache.solr.client.solrj.beans.Field;

public class Message implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private String title;
	private String[] content;

	public Message() {
		super();
	}

	public Message(String id, String title, String[] content) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
	}

	public String getId() {
		return id;
	}

	@Field("id")
	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	@Field("title")
	public void setTitle(String title) {
		this.title = title;
	}

	public String[] getContent() {
		return content;
	}

	@Field("content")
	public void setContent(String[] content) {
		this.content = content;
	}

}


(完)

    



© 著作权归作者所有

空云万里晴

空云万里晴

粉丝 67
博文 52
码字总数 33031
作品 0
广州
高级程序员
私信 提问
加载中

评论(1)

Skqing
Skqing
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
13:07:54.827 [main] DEBUG org.apache.solr.client.solrj.impl.HttpClientUtil - Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=false
Exception in thread "main" org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr/#/new_core: Expected mime type application/octet-stream but got text/html. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html ng-app="solrAdminApp">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this
Solr JAVA客户端SolrJ 4.9使用示例教程

简介 SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr索引的JAVA接口。SolrJ针对Solr提供了Rest 的HTTP接口进行了封装,SolrJ底层是通过使用httpClient中的方法来完成Sol...

cloud-coder
2014/08/21
3.6K
0
Solr 按日期facet 差8小时问题解决

项目中使用solrj来操作solr,日期solr会自动转换: 1.solrj在提交到solr时,时间会因为时区问题减少八小时 2.solr接收到时间后,存为底层lucene索引时时间类型为long型,是正确的时区时间,但...

zachary124
2014/06/24
726
4
Solr环境搭建及IK分词的集成及solrJ的调用(三)【完结】

前两篇的连接到这里: Solr环境搭建及IK分词的集成及solrJ的调用(一) http://my.oschina.net/zimingforever/blog/120732 Solr环境搭建及IK分词的集成及solrJ的调用(二) http://my.oschina.n...

王小明123
2013/04/10
3.4K
0
solr6.6初探之solrj

一. solrj简介:   solrj可以使Java应用程序很方便的访问与操作solr。solrj有几个核心类,分别为:1.SolrClient 2.SolrRequests 3.SolrQuerys 4.SolrReponses   tips:该jar包可以在${solr....

java~nick
2017/10/24
0
0
java操作solr实现索引,查询,删除,拼写检查等功能

使用java操作solr 前面讲了很多对solr的操作都是直接在页面上操作的,实际工作中肯定是要使用java进行操作的,在这我们就看一下如何使用java来操作solr Solr提供了solrj来使用java操作solr,...

超人学院
2015/06/12
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

Activity启动模式二

上篇文章Activity启动模式一主要介绍了Activity的四种启动模式,这些启动模式都是在AndroidManifest中进行配置的。除此之外,Android系统还通过Intent类提供了一些标志位,同样可以指定Activ...

ltlovezh
2分钟前
2
0
三原色还原

1、Color Filter Array — CFA 随着数码相机、手机的普及,CCD/CMOS 图像传感器近年来得到广泛的关注和应用。 图像传感器一般都采用一定的模式来采集图像数据,常用的有 BGR 模式和 CFA 模式...

天王盖地虎626
13分钟前
2
0
kubernetes pod exec接口调用

正文 一般生产环境上由于网络安全策略,大多数端口是不能为集群外部访问的。多个集群之间一般都是通过k8s的ApiServer组件提供的接口通信,如https://192.168.1.101:6443。所以在做云平台时,...

码农实战
50分钟前
6
0
3_数组

3_数组

行者终成事
今天
8
0
经典系统设计面试题解析:如何设计TinyURL(二)

原文链接:https://www.educative.io/courses/grokking-the-system-design-interview/m2ygV4E81AR 编者注:本文以一道经典的系统设计面试题:《如何设计TinyURL》的参考答案和解析为例,帮助...

APEMESH
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部