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; public class SolrTest { String baseURL = "http://192.168.30.111:8983/solr/collection1";// http://ip:port/solr/collection1 HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); /** * 查询 * * @throws Exception */ @Test public void 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 public void 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 public void 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 public void test4() throws Exception { httpSolrServer.deleteById("10"); httpSolrServer.deleteByQuery("text:Maxtor"); httpSolrServer.commit(); } @Test public void 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; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
时间: 2024-10-27 09:09:19