solr搜索技术帖:
http://blog.csdn.net/hu948162999/article/category/2582709
Solrj已经是很强大的solr客户端了。以完全对象的方式对solr进行交互。很小很好很强大。最基本的功能就是管理Solr索引,包括添加、更新、删除和查询等。
在此之前:先介绍一个异常,以前有朋友问过这个,最近查了下solrj的源码。
2014-10-16 17:52:07,486 ERROR [org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer] -
org.apache.solr.common.SolrException: Not Found
或者 org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type application/xml but got text/html
这是报错的solrj部分源代码
method = new HttpPost(server.getBaseURL() + “/update”
+ ClientUtils.toQueryString(requestParams, false));–注意这里路径getBaseURL
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
StringBuilder msg = new StringBuilder();
msg.append(response.getStatusLine().getReasonPhrase()); —–这里NOT FOUND
msg.append(“\n\n”);
msg.append(“\n\n”);
msg.append(“request: “).append(method.getURI());
handleError(new SolrException(ErrorCode.getErrorCode(statusCode), msg.toString())); –执向这一行异常,并且打印msg,这里打印确实有点坑
} else {
onSuccess(response);
}
通过其端点跟踪 ,不难发现是由于URL拼接而成的路径报错,也就是说服务器的请求路径不对。一般是服务器后面跟踪的core的不对,所导致整个请求路径不对
老习惯:直接代码分析
package com.hhc.searchEngine;
import org.apache.log4j.Logger;
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.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.BeanUtils;
import org.springframework.web.servlet.ModelAndView;
import com.ws.cache.model.FieldInfo;
import com.ws.cache.model.Scheme;
import com.ws.utils.SearchInitException;
import com.ws.utils.StringUtils;
import com.ws.utils.converter.ConverterUtils;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Clob;
import java.util.*;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
/**
* 基于solr实现的搜索引擎.
*
* @author huhuichao ([email protected])
* @version V1.0
* @createTime 2014-10-13
*/
@SuppressWarnings("unchecked")
public class SearchEngine {
private static final Logger logger = Logger.getLogger(SearchEngine.class);
private String server = "http://localhost:8080/solr";
private SolrServer solrServer = null;
private SolrServer getSolrServer(Scheme scheme) throws SearchInitException {
try {
solrServer = scheme.getConcurrentUpdateSolrServer();
} catch (Exception e) {
e.printStackTrace();
logger.error("null solr server path! !");
throw new SearchInitException(
"null solr server path! !");
}
return solrServer;
}
/**
* 根据数据模型
* 增加维护索引
* @param scheme --索引方案
* @param list --数据模型
* @throws Exception
*/
public synchronized void AddSearchIndex(Scheme scheme,List
通过url获取solr服务器的时候。注意在solr4.0后 CommonsHttpSolrServer 这个类已经被 HttpSolrServer取代了
在solrj中 提出了3种常用的删除操作对外接口
这是从其源码中拷贝出来的删除代码
/**
* Deletes a single document by unique ID
* @param id the ID of the document to delete
* @throws IOException If there is a low-level I/O error.
*/
public UpdateResponse deleteById(String id) throws SolrServerException, IOException {
return deleteById(id, -1);
}
/**
* Deletes a list of documents by unique ID
* @param ids the list of document IDs to delete
* @throws IOException If there is a low-level I/O error.
*/
public UpdateResponse deleteById(List ids) throws SolrServerException, IOException {
return deleteById(ids, -1);
}
/**
* Deletes documents from the index based on a query
* @param query the query expressing what documents to delete
* @throws IOException If there is a low-level I/O error.
*/
public UpdateResponse deleteByQuery(String query) throws SolrServerException, IOException {
return deleteByQuery(query, -1);
}
//从这段代码 可以看出,就是循环删除单个文本节点操作
public UpdateRequest deleteById(List ids) {
if (deleteById == null) {
deleteById = new LinkedHashMap<>();
}
for (String id : ids) {
deleteById.put(id, null);
}
return this;
}
}
solr 搜索技术帖:
http://blog.csdn.net/hu948162999/article/category/2582709