package com.feng.solr;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
public class SolrTest {
public static final String URL="http://localhost:8080/solr/core";
public static void test(){
HttpSolrClient httpSolrClient = new HttpSolrClient(URL);
try {
//新增文档
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "2");
doc.addField("title", "atitle");
doc.addField("name", "fengsong");
httpSolrClient.add(doc);
httpSolrClient.commit();
//新增文档
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "2");
doc1.addField("title", "atitle1");
doc1.addField("name", "fengsong1");
httpSolrClient.add(doc1);
httpSolrClient.commit();
//查询文档
NamedList<String> params = new NamedList<String>();
params.add("q", "name:*song*");
params.add("start", "0");
params.add("rows", "10");
System.out.println(httpSolrClient.query(SolrParams.toSolrParams(params)));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
httpSolrClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test();
}
}