首先solr服务器安装过程不再赘述,在客户端集成的时候踩了点坑,记录下来,大家一起学习~
在pom文件的
dependencyManagement -》 dependencies
标签里面添加
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring.data.solr.version}</version>
</dependency>
properties
标签里面添加
<spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version>
下面是solr的jar包,
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-solr -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
注入SolrClient,及相关代码
@Autowired
private SolrClient client;
@RequestMapping("/testSolr")
public String testSolr(String param) throws IOException, SolrServerException {
try {
//查询参数对象,继承了SolrParams抽象类
ModifiableSolrParams params =new ModifiableSolrParams();
//查询条件
params.add("q","companyName:*"+param+"*");
//这里的分页和mysql分页一样
params.add("start","0");
params.add("rows","10");
QueryResponse query = client.query(params);
//查询结果
SolrDocumentList results = query.getResults();
//将查询结果直接转化为List,这里有个坑,对象每个属性必须要加 @Field("id") 属性,包为import org.apache.solr.client.solrj.beans.Field;
//如果不加属性的话,会返回相等长度的的List,但是List里面每个对象的值均为空
List<SimplePublicCustomer> beans = query.getBeans(SimplePublicCustomer.class);
return results.toString();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
完~
tips:
因为本人比较懒。所以不想去解析返回来的值,再一个一个组装成List,所以看了下
query.getBeans 这个方法的 源码
:
DocumentObjectBinder 类
public <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocList) {
List fields = this.getDocFields(clazz); //这个句话获取你对象里面的属性列表
ArrayList result = new ArrayList(solrDocList.size());
Iterator i$ = solrDocList.iterator();
while(i$.hasNext()) {
SolrDocument sdoc = (SolrDocument)i$.next();
result.add(this.getBean(clazz, fields, sdoc));
}
return result;
}
private List<DocumentObjectBinder.DocField> getDocFields(Class clazz) {
List fields = (List)this.infocache.get(clazz);
if(fields == null) {
Map var3 = this.infocache;
synchronized(this.infocache) {
this.infocache.put(clazz, fields = this.collectInfo(clazz)); //从缓存中获取不到相关信息,则执行collectiInfo方法获取
}
}
return fields;
}
private List<DocumentObjectBinder.DocField> collectInfo(Class clazz) {
ArrayList fields = new ArrayList();
Class superClazz = clazz;
ArrayList members;
for(members = new ArrayList(); superClazz != null && superClazz != Object.class; superClazz = superClazz.getSuperclass()) {
members.addAll(Arrays.asList(superClazz.getDeclaredFields()));
members.addAll(Arrays.asList(superClazz.getDeclaredMethods()));
}
boolean childFieldFound = false;
Iterator i$ = members.iterator();
while(i$.hasNext()) {
final AccessibleObject member = (AccessibleObject)i$.next();
if(member.isAnnotationPresent(Field.class)) { //重点:获取当前字段的注解,如果没有注解,则不添加此字段!
AccessController.doPrivileged(new PrivilegedAction() {
@SuppressForbidden(
reason = "Needs access to possibly private @Field annotated fields/methods"
)
public Void run() {
member.setAccessible(true);
return null;
}
});
DocumentObjectBinder.DocField df = new DocumentObjectBinder.DocField(member);
if(df.child != null) {
if(childFieldFound) {
throw new BindingException(clazz.getName() + " cannot have more than one Field with child=true");
}
childFieldFound = true;
}
fields.add(df);
}
}
return fields;
}
基本上就这些,solr客户端集成基本完事,更详细的请看api等资料。
版权声明:本文为qq779446849原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。