首先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等資料。