热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何处理PB级别数据(2)——ApacheSolr之使用SolrJ操作索引库

Solrj是Solr搜索服务器的一个比较基础的客户端工具,可以非常方便地与Solr搜索服务器进行交互。最基本的功能就是管理Solr索引,包括添加、更新、删除和查询等。对于一些比较基础的应用,用Sol

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

       使用 SolrJ操作索引库:

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 docs = new ArrayList();

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 msgs = new ArrayList();

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);
}
}

/**
* 更新索引

* 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("")
.setHighlightSimplePost("
")
.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>> map = response.getHighlighting();
Iterator iterator = map.keySet().iterator();

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

for (Map.Entry> entry : values.entrySet()) {
String subKey = entry.getKey();
List 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;
}

}



推荐阅读
author-avatar
手机用户2502886741
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有