solrJ的增删改查,solrDocument与Bean的映射,直接增删该查bean完成对索引的操作
package com.he.mysolr;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
public>public static void addIndex() { HttpSolrClient client
= App.getServer(); List list
= new ArrayList(); TestBO bean
= new TestBO(); bean.setId(
"12131232213"); bean.setName_s(
"hzzhzz"); bean.setScore_i(
100); list.add(bean);
try { client.addBeans(list);
client.commit();
}
catch (SolrServerException e) {// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delete(String condition) {
HttpSolrClient client = App.getServer();
try {
client.deleteByQuery(condition);
client.commit();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void query() {
HttpSolrClient client = App.getServer();
SolrQuery sQuery = new SolrQuery();
sQuery.setQuery("id:1*");
QueryResponse queryResponse =null;
try {
queryResponse = client.query(sQuery);
List<TestBO> beans = queryResponse.getBeans(TestBO.class);
for (TestBO testBO : beans) {
System.out.println(testBO);
}
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//addIndex();
//delete("id:12131232*");
query();
}
}
页:
[1]