Solr使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Solr使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3716字,纯文字阅读大概需要6分钟。
内容图文
![Solr使用](/upload/InfoBanner/zyjiaocheng/1300/48fa74fff62a4819aa17817cbe455d43.jpg)
import static org.junit.Assert.*; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Set; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.SpellCheckResponse; import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion; 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.junit.Test; publicclass SolrTest { String baseURL = "http://192.168.30.111:8983/solr/collection1";//http://ip:port/solr/collection1 HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); /** * 查询 * * @throws Exception */ @Test publicvoid test1() throws Exception { // 注意:如果baseURL后面没有指定solrcore,那么默认查询collection1,如果想查询其他core,则需要在后面增加core的名称 SolrQuery params = new SolrQuery(); // params.set("q","*:*"); params.setQuery("*:*"); params.setRows(10); QueryResponse response = httpSolrServer.query(params); SolrDocumentList results = response.getResults(); long numFound = results.getNumFound(); System.out.println("found:" + numFound); int size = results.size(); System.out.println("size:" + size); int num = 0; for (SolrDocument solrDocument : results) { Set<String> keySet = solrDocument.keySet(); num++; for (String key : keySet) { System.out.println("num----" + num + "----key-->" + key + "---------value-->" + solrDocument.get(key)); } System.err.println("========================"); } } /** * 建立索引 * * @throws Exception */ @Test publicvoid test2() throws Exception { SolrInputDocument docs = new SolrInputDocument(); docs.addField("id", 234); docs.addField("name", "soros"); // docs.addField("last_modified", new Date()); httpSolrServer.add(docs); // httpSolrServer.commit(); } /** * 建立索引2 * * @throws Exception */ @Test publicvoid test3() throws Exception { User user = new User(); user.setId(111); user.setName("cat"); user.setCity("shenzhen"); httpSolrServer.addBean(user); // httpSolrServer.commit(); httpSolrServer.commit(true,false,true); // httpSolrServer.optimize(waitFlush, waitSearcher, maxSegments) } /** * 删除 * * @throws Exception */ @Test publicvoid test4() throws Exception { httpSolrServer.deleteById("10"); httpSolrServer.deleteByQuery("text:Maxtor"); httpSolrServer.commit(); } @Test publicvoid test5() throws Exception { SolrQuery params = new SolrQuery(); params.set("qt", "/spell"); params.setQuery("name:sausung"); QueryResponse response = httpSolrServer.query(params); long numFound = response.getResults().getNumFound(); if (numFound == 0) { System.out.println("拼写错误"); SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse(); List<Suggestion> suggestions = spellCheckResponse.getSuggestions(); for (Suggestion suggestion : suggestions) { int numFound2 = suggestion.getNumFound(); System.out.println("推荐搜索关键字数:"+numFound2+"~~~~~~~"); List<String> alternatives = suggestion.getAlternatives(); for (String value : alternatives) { System.out.println("建议值为"+value+"~~~~~~~~~"); } } } else { System.out.println("正确查询"); } } }
import org.apache.solr.client.solrj.beans.Field; public class User { @Field private Integer id; @Field private String name; private String city; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Integer getId() { return id; } publicvoid setId(Integer id) { this.id = id; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } }
原文:http://www.cnblogs.com/cxzdy/p/5126091.html
内容总结
以上是互联网集市为您收集整理的Solr使用全部内容,希望文章能够帮你解决Solr使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】