import com.yuzw.model.AbnormalSchool;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.GroupResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import static org.junit.Assert.*;
/**
* Created by yuzhanwu.
* Created on 2017/12/25.
*/
public class ServerTestTest {
private SolrServer server;
private SolrServer serverAbnormal;
private CommonsHttpSolrServer httpServer;
private static final String BASE_URL = "http://10.1.1.52:8983/solr";
private static final String SUFFIX_SOLR = "/my_news";
private static final String SUFFIX_ABNORMAL = "/abnormal";
@Before
public void setUp() throws Exception {
System.out.println("before");
try {
server = new CommonsHttpSolrServer(BASE_URL+SUFFIX_SOLR);
serverAbnormal = new CommonsHttpSolrServer(BASE_URL+SUFFIX_ABNORMAL);
// httpServer = new CommonsHttpSolrServer(DEFAULT_URL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
System.out.println("after");
}
@Test
public void query() throws Exception {
SolrParams params = new SolrQuery("*:*");
try {
QueryResponse response = server.query(params);
// GroupResponse gresp = response.getGroupResponse();//注意:此处不能再用resp.getResults()接收结果
// List<GroupCommand> commands = gresp.getValues();
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
@Test
public void queryRateAndIpcount() throws Exception {
SolrQuery params = new SolrQuery("*:*");
try {
QueryResponse response = server.query(params);
GroupResponse gresp = response.getGroupResponse();//注意:此处不能再用resp.getResults()接收结果
List<GroupCommand> commands = gresp.getValues();
SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
fail(list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
@Test
public void addDocument() throws Exception{
//创建文档对象
SolrInputDocument doucument= new SolrInputDocument();
doucument.addField("id", "917770");
doucument.addField("item_title", "华为 P6-C00 电信3G手机(粉色) CDMA2000/GSM 双模双待双通");
doucument.addField("item_price", 134900);
//把文档对象写入文档里
server.add(doucument);
//提交
server.commit();
}
public final void fail(Object o) {
System.out.println(o);
}
@Test
public void addBean() {
AbnormalSchool school = new AbnormalSchool();
school.setId(9999999);
school.setSchoolid(808);
school.setIpcount(5);
school.setRate(3);
try {
serverAbnormal.addBean(school);
serverAbnormal.commit();
} catch (IOException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
solr在java中基本操作
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...