Solr简单使用

1.添加索引

		// 第一步:把solrJ的jar包添加到工程中。
		// 第二步:建立一个SolrServer,使用HttpSolrServer建立对象。
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
		// 第三步:建立一个文档对象SolrInputDocument对象。
		SolrInputDocument document = new SolrInputDocument();
		// 第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
		document.addField("id", "test001");
		document.addField("item_title", "测试商品");
		document.addField("item_price", "199");
		// 第五步:把文档添加到索引库中。
		solrServer.add(document);
		// 第六步:提交。
		solrServer.commit();

2.删除

		// 第一步:建立一个SolrServer对象。
		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
		// 第二步:调用SolrServer对象的根据id删除的方法。
		solrServer.deleteById("1");
		// 第三步:提交。
		solrServer.commit();

                  SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");java

                  solrServer.deleteByQuery("title:change.me");测试

                  solrServer.commit();xml


3.查询

		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
		// 第二步:建立一个SolrQuery对象。
		SolrQuery query = new SolrQuery();
		// 第三步:向SolrQuery中添加查询条件、过滤条件。。。
		query.setQuery("*:*");
		// 第四步:执行查询。获得一个Response对象。
		QueryResponse response = solrServer.query(query);
		// 第五步:取查询结果。
		SolrDocumentList solrDocumentList = response.getResults();
		System.out.println("查询结果的总记录数:" + solrDocumentList.getNumFound());
		// 第六步:遍历结果并打印。
		for (SolrDocument solrDocument : solrDocumentList) {
			System.out.println(solrDocument.get("id"));
			System.out.println(solrDocument.get("item_title"));
			System.out.println(solrDocument.get("item_price"));
		}