今天用到了ES搜索中的线点搜索。下面说一下线点的使用与配置,搜索参数规则与solr搜索规则一样,稍后也说一下。最后说一下java调取获取数据。
1、线点搜索使用
先创建一个视图
然后在线点平台建一个索引表
创建索引任务
然后启动任务 ,将数据导入。成功后就可以查询了
2、下面说一下搜索规则,比如要查找字段name='张三' 和 name='李四' 和 name='王五'的所有数据。类似name in ( ?)
传统规则:q=+("搜索词") +(signDate: " year ") +(orgCodeList:"code3" orgCodeList:"code2")
自己在java调取时没这么用,看一下我的写法:
/**
* 搜索
*
* @author zjq
* @param param
* 查询条件 start - rows 数据条数
*/
public List<Map<String, String>> getQuery(String param, String start, String rows,String year,String orgname) {
ArrayList<String> userOrgCodeList=getOrgName();
if(userOrgCodeList.size()>0){
param+="(";
for(String orgName:userOrgCodeList){
//orgCode 转 orgName
String sqlorg = "select orgName from xxxx where orgCode = ?";
Map<String,Object> orgNamen=findOneForJdbc(sqlorg, orgName) ;
//参数拼接 departmentName:名称 departmentName:名称2
param+="departmentName:"+orgNamen.get("orgName").toString()+"%20";
}
param+=")";
}
if(StringUtil.isNotEmpty(param)){
if(StringUtil.isNotEmpty(year)){
param+="%20AND%20signDate:"+year+"-*";
}
if(StringUtil.isNotEmpty(orgname)){
param+="%20AND%20departmentName:"+orgname;
}
}else{
if(StringUtil.isNotEmpty(year)){
param+="signDate:"+year+"-*";
}
if (StringUtils.isBlank(param)) {
if(StringUtil.isNotEmpty(orgname)){
param+="departmentName:"+orgname;
}
} else {
if(StringUtil.isNotEmpty(orgname)){
param+="%20AND%20departmentName:"+orgname;
}
}
}
if (StringUtils.isBlank(param)) {
param += "&sort=signDate%20desc";
} else {
param += "&sort=signDate%20desc";
}
Map<String, Object> result = new SpellCheckRequest("线点地址", "api/documents",
"索引名称", param, start, rows).execute().asMap();
List<Map<String, String>> List = new ArrayList<Map<String, String>>();
if (result != null) {
String total = result.get("total").toString();
Object list = result.get("documents");
if (list != null) {
List<Map<String, Object>> listData = (List<Map<String, Object>>) list;
if (listData.size() > 0) {
for (Map<String, Object> mapo : listData) {
Map<String, String> mapList = new HashMap<String, String>();
String keyword = mapo.get("fields").toString();
JSONObject jsonArray = null;
jsonArray = new JSONObject(keyword);
mapList.put("departmentName", jsonArray.getString("departmentName"));
mapList.put("total", total);// 总数
List.add(mapList);
}
}
}
}
return List;
}
也就是 (name:张三%20name:李四%20name:王五%20)
3、调取线点获取数据
package com.xdcloudsearch.client.request;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.jeecgframework.core.util.StringUtil;
import com.xdcloudsearch.client.ReturnContent;
public class SpellCheckRequest extends APIRequest {
public SpellCheckRequest(String baseServerUri,String api, String core, String input,String start,String rows) {
super(baseServerUri);
this.core = core;
this.api = api;
this.start = start;
this.rows = rows;
this.input = input;
}
@Override
public ReturnContent execute(String token) {
String url="";
if("api/hotwords".equals(api)){
url=server + "/" + api + "/30000/";
}else if("api/documents".equals(api)){
if(StringUtil.isNotEmpty(input)){
url=server + "/" + api + "/" + this.core+"?q="+input+"&start="+start+"&rows="+rows+"";
}else{
url=server + "/" + api + "/" + this.core+"?start="+start+"&rows="+rows+"";
}
}
Request request = Request.Get(url);
request.setHeader("token", token);
try {
return new ReturnContent(request.execute().returnContent().asString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}