标签:solr
上一章讲了怎么使用solr admin向solr服务器添加索引,Solr 是一个独立的企业级搜索应用服务器,它对外提供类似于 Web-service 的 API 接口。用户可以通过 http请求,向搜索引擎服务器提交一定格式的 XML 文件,生成索引;也可以通过 Http Get 操作提出查找请求,并得到XML 格式的返回结果。所以我们可以使用httpClient作为客户端向solr服务器添加索引,但是幸运的是,solr为我们提供专门的客户端jar包,solrj,我们可以使用该jar包向solr服务器提交索引。
首先我们导入相关的包,相关的jar包可以通过下载的solr的压缩包中获取,其中包括:
1.dist/solrj-lib
2.dist/solr-solrj-*.jar
把这些jar包导入到classpath中,我们就可以开始使用solrj了。
String url = "http://localhost:8080/solr"; HttpSolrServer server = new HttpSolrServer(url); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. server.setConnectionTimeout(5000); // 5 seconds to establish TCP // Setting the XML response parser is only required for cross // version compatibility and only when one side is 1.4.1 or // earlier and the other side is 3.1 or later. server.setParser(new XMLResponseParser()); // binary parser is used by // default // The following settings are provided here for completeness. // They will not normally be required, and should only be used // after consulting javadocs to know whether they are truly required. server.setSoTimeout(1000); // socket read timeout server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); server.setFollowRedirects(false); // defaults to false // allowCompression defaults to false. // Server side must support gzip or deflate for this to have any effect. server.setAllowCompression(true);
// Note that the following property could be set through JVM level arguments too System.setProperty("solr.solr.home", "/home/shalinsmangar/work/oss/branch-1.3/example/solr"); CoreContainer.Initializer initializer = new CoreContainer.Initializer(); CoreContainer coreContainer = initializer.initialize(); EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");
File home = new File( "/path/to/solr/home" ); File f = new File( home, "solr.xml" ); CoreContainer container = new CoreContainer(); container.load( "/path/to/solr/home", f ); EmbeddedSolrServer server = new EmbeddedSolrServer( container, "core name as defined in solr.xml" );
SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField( "id", "333333333333333", 1.0f ); doc1.addField( "name", "3333333333333333333", 1.0f ); doc1.addField( "price", 10 ); server.add(doc1); server.commit();提交成功后,可以通过solr admin查看索引是否增加了,但是最简单的方式就是调用查询方法,查询一下。
SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("q=3333333333333333333"); QueryResponse queryResponse= server.query(solrQuery); System.out.println(queryResponse.getResults().get(0).getFieldValue("name"));
首先我们需要新建一个java bean。
public class Item { @Field private String id; @Field private String category; @Field private List<String> features; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public List<String> getFeatures() { return features; } public void setFeatures(List<String> features) { this.features = features; } }向solr添加数据。
Item item = new Item(); item.setId("201404300001"); item.setCategory("category"); item.setFeatures(Arrays.asList("feature1","feature2","feature3")); server.addBean(item); server.commit();搜索一下,验证是否添加成功。
SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("q=feature1"); QueryResponse queryResponse= server.query(solrQuery); List<Item> items = queryResponse.getBeans(Item.class); System.out.println(items.get(0).getCategory());
标签:solr
原文地址:http://blog.csdn.net/jaynol/article/details/24776437