您的位置:首页 > 其它

solr6.1-----solrJ 程序管理索引库

2016-08-21 21:34 190 查看
  solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务。

  所需jar 包

  D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0.jar,以及其依赖 D:\solr-6.1.0\dist\solrj-lib 文件夹下面的所有jar

  

  创建工程添加jar 包

  添加 solr 库索引

@Test
public void create() throws Exception{
System.out.println("======================create===============");
@SuppressWarnings({ "resource", "deprecation" })
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
String[] titles = {
"chihirotj",
"chihirotj",
"CAS latency",
"CAS latency",
"CAS latency",
"CAS latency chihirotj"};
String[] contents = {
"chihirotj this a vear popular book",
"chihirotj i love you dlp",
"CAS latency a a a a a a a",
"CAS latency you ara bearuiflu",
"CAS latency i love this computer",
"CAS latency hao are you"};
int i = 100;
for(int j = 0; j <= 5; j ++){
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id",i++);
//需要在core1/conf/managed-schema中有对应的field
//在solr 6.1 版本中已经不需要了,会自己动态的创建域,类型为strings
doc.addField("title", titles[j]);
doc.addField("content", contents[j]);
docList.add(doc);
}
UpdateResponse rsp = solrClient.add(docList);
System.out.println("Add doc size" + docList.size() + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
UpdateResponse rspcommit = solrClient.commit();
System.out.println("commit doc to index" + " result:" + rsp.getStatus() + " Qtime:" + rspcommit.getQTime());
}


  

  之后在 管理员页面查询数据:点击query 能够查询出,刚刚添加的数据,则添加数据成功。

  


  默认添加的域为 strings 类型。我们需要修改类型,使用能够解析字符串的分词器(title 域lz已经做了修改)



  查询 solr 索引库

@Test
public void query(){
System.out.println("======================query===================");
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
SolrQuery query = new SolrQuery();
//查询条件
query.setQuery("chihirotj");
//高亮字段
query.setParam("hl.fl", "title");
//开启高亮
query.setHighlight(true);
//返回的字符个数
query.setHighlightFragsize(200);
//是否需要完全匹配
//query.setHighlightRequireFieldMatch(true);
//前缀
query.setHighlightSimplePre("<em>");
//后缀
query.setHighlightSimplePost("</em>");
QueryResponse response = null;
try {
response = solrClient.query(query);
System.out.println(response.toString());
System.out.println();
SolrDocumentList docs = response.getResults();
Map<String, Map<String, List<String>>>  maps = response.getHighlighting();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
//配置分词后
System.out.println("id: " + doc.getFieldValue("id") + " title: " + doc.getFieldValue("title"));
}
for (Map<String, List<String>> map : maps.values()) {
List<String> title = map.get("title");
//高量的值
System.out.println(title.iterator().next());
}
} catch (Exception e) {
e.printStackTrace();
}
}


  

    运行结果:

  

  

  删除 索引库

  

   @Test
public void deleteById() {
System.out.println("======================deleteById ===================");
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
try {
String[] ids = {"1", "2", "4"};
for (String id : ids) {
UpdateResponse rsp = solrClient.deleteById(id);
System.out.println("delete id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
}
solrClient.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}


  

参考:
http://www.cnblogs.com/feiye512/p/5630684.html http://lucene.apache.org/solr/6_1_0/solr-solrj/org/apache/solr/client/solrj/SolrClient.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: