ElasticSearch 配置及基本搜索
本文没有做详细的说明,只是做完了之后,做一个笔记吧算是。
ES.properties 配置文件链接
#ElasticSearch
clientIP=127.0.0.1
clientPort=9300
productIndexPrefix=qq_product_
productIndexName=qq_product
productIndexType=qq_product
代码块
采用ElasticSearch 6.0 服务,例如:
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import com.alibaba.fastjson.JSONObject;
import com.eaju.bos.vo.ProductAll;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.elasticsearch.client.transport.*;
public class ElasticSearchUtil {
private final static Logger logger=org.slf4j.LoggerFactory.getLogger(ElasticSearchUtil.class);
private static TransportClient client=null;
//获取连接
public static TransportClient getInstance() {
if (client == null) {
try {
String clientIP = ResourcesUtil.getValue("ES", "clientIP");
String clientPort = ResourcesUtil.getValue("ES", "clientPort");
/*Settings settings = Settings.builder()
.put("cluster.name", "test-yianju-es").build();*/
/*client = new TransportClient()
.addTransportAddress(new TransportAddress(InetAddress.getByName(clientIP), Integer.parseInt(clientPort)));*/
//集群链接
/*client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName(clientIP), Integer.parseInt(clientPort)));*/
//单机链接
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new TransportAddress(InetAddress.getByName(clientIP), Integer.parseInt(clientPort)));
} catch (UnknownHostException e) {
logger.error("ElasticSearch 创建连接失败!!");
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
return client;
}
/**
* 加载数据用(增加天数的方式)
* @param startTime
* @param endTime
* @throws IOException
*/
public void loadProToEs(String startTime,String endTime) throws IOException{
logger.debug(" ElasticSearchUtil loadProToEs start...");
try {
client = ElasticSearchUtil.getInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatterD = new SimpleDateFormat("yyyy-MM-dd");
//开始时间
Date sTime = format1.parse(startTime);
//结束时间
Date eTime = format1.parse(endTime);//new Date();
while(!sTime.after(eTime)){
System.out.println("sTime=="+sTime+" eTime=="+eTime);
List<ProductAll> listProAll = new ArrayList<ProductAll>();
Calendar c = Calendar.getInstance();
c.setTime(sTime);
c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
//加一天后的日期
Date tomorrow = c.getTime();
if(formatterD.format(sTime) == formatterD.format(eTime) || formatterD.format(sTime).equals(formatterD.format(eTime))){
listProAll = loadProduct(formatter.format(sTime),formatter.format(eTime));
logger.debug(" starttime = "+sTime+" end time = "+eTime+" end!!!");
}else{
logger.debug(" starttime = "+sTime+" end time = "+tomorrow+" next!!!");
listProAll = loadProduct(formatter.format(sTime),formatter.format(tomorrow));
}
if(null != listProAll && listProAll.size() >0){
createProductIndex(listProAll);
}
//给开始日期加一天
sTime = tomorrow;
}
} catch (Exception e) {
logger.debug(" ElasticSearchUtil loadProToEs error...");
e.printStackTrace();
}
logger.debug(" ElasticSearchUtil loadProToEs end...");
}
/**
* 每小时跑一次
* @throws IOException
*/
public void loadProToEsHour() throws IOException{
logger.debug(" ElasticSearchUtil loadProToEsHour start...");
try {
client = ElasticSearchUtil.getInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatterD = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
//开始时间
Date sTime = new Date();
//结束时间
List<ProductAll> listProAll = new ArrayList<ProductAll>();
Calendar c = Calendar.getInstance();
c.setTime(sTime);
c.add(Calendar.HOUR_OF_DAY, -1);// 日期减一小时
//加一天后的日期
Date eTime = c.getTime();
System.out.println("sTime=="+sTime+" eTime=="+eTime);
listProAll = loadProduct(formatter.format(eTime),formatter.format(sTime));
if(null != listProAll && listProAll.size() >0){
createProductIndex(listProAll);
}
} catch (Exception e) {
logger.debug(" ElasticSearchUtil loadProToEsHour error...");
e.printStackTrace();
}
logger.debug(" ElasticSearchUtil loadProToEsHour end...");
}
/**
* 创建商品索引
* @param jsondata
* @throws IOException
*/
public void createProductIndex(List<ProductAll> listProAll) throws IOException{
Gson gson = new Gson();
String indexname = ResourcesUtil.getValue("ES", "productIndexName");
String type = ResourcesUtil.getValue("ES", "productIndexType");
String productIndexPrefix = ResourcesUtil.getValue("ES", "productIndexPrefix");
System.out.println(gson.toJson(listProAll));
client = ElasticSearchUtil.getInstance();
for(ProductAll proAll : listProAll){
try {
if("0" == proAll.getIsEnable() || "0".equals(proAll.getIsEnable())){
System.out.println(" 写入--"+gson.toJson(proAll));
//创建索引库 需要注意的是.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)这里一定要设置,否则第一次建立索引查找不到数据
IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type,productIndexPrefix+proAll.getCdcmId()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);//..setRefresh(true);
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("cdcmId", proAll.getCdcmId())
.field("proName", proAll.getProName())
.field("cdcmLength", proAll.getCdcmLength())
.field("cdcmWidth", proAll.getCdcmWidth())
.field("cdcmHeight", proAll.getCdcmHeight())
.field("cdcmCube", proAll.getCdcmCube())
.field("proCode", proAll.getProCode())
.field("proClass", proAll.getProClass())
.field("cusGroup", proAll.getCusGroup())
.field("isEnable", proAll.getIsEnable())
.endObject();
IndexResponse response = requestBuilder.setSource(builder).get();
}else{
deleteIndex(indexname, type,productIndexPrefix+proAll.getCdcmId());
};
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除索引
* @param jsondata
*/
public boolean deleteIndex(String indexName,String indexType,String indexId){
boolean ret = false;
try {
client = ElasticSearchUtil.getInstance();
DeleteResponse dResponse = client.prepareDelete(indexName, indexType, indexId).execute().actionGet();
ret = dResponse.isFragment();
} catch (Exception e) {
logger.error(e.getMessage(),e);
e.printStackTrace();
}
return ret;
}
/**
* 加载初始化商品数据
* @return
*/
public List<ProductAll> loadProduct(String startTime,String endTime) {
logger.debug("start execute ProductServiceImpl.loadProduct method ..");
Gson gson = new Gson();
String result = "";
List<ProductAll> listProductAll = new ArrayList<ProductAll>();
String url = PropertyUtil.getProperty("/http.properties", "queryAllProduct");
try {
Map<String,Object> param = new HashMap<String, Object>();
param.put("startTime", startTime);
param.put("endTime", endTime);
result = HttpClientUtil.postJson(url, gson.toJson(param));
listProductAll = gson.fromJson(result, new TypeToken<List<ProductAll>>(){}.getType());
} catch (IOException e) {
logger.debug("execute ProductServiceImpl.loadProduct method error..");
e.printStackTrace();
}
logger.debug("end execute ProductServiceImpl.loadProduct method ..");
return listProductAll;
}
public List<ProductAll> searcher(QueryBuilder queryBuilder,int pageSize){
client = ElasticSearchUtil.getInstance();
List<ProductAll> list = new ArrayList<ProductAll>();
String indexname = ResourcesUtil.getValue("ES", "productIndexName");
String type = ResourcesUtil.getValue("ES", "productIndexType");
SearchRequestBuilder builder = client.prepareSearch(indexname).
setTypes(type).setSearchType(SearchType.DEFAULT)
.setFrom(0).setSize(pageSize);
builder.setQuery(queryBuilder);
SearchResponse response = builder.execute().actionGet();
System.out.println(" ========= " + response);
System.out.println(" hits-=-=-=-= " +response.getHits().getTotalHits());
SearchHits hits = response.getHits();
System.out.println("查询到记录数=" + hits.getTotalHits());
SearchHit[] searchHists = hits.getHits();
if(searchHists.length>0){
for(int i=0;i< searchHists.length;i++){
ProductAll pa = new ProductAll();
JSONObject json = new JSONObject().parseObject(searchHists[i].getSourceAsString());
pa.setCdcmId(String.valueOf(json.get("cdcmId")));
pa.setProName(String.valueOf(json.get("proName")));
if(null != json.get("cdcmLength") && !"".equals(json.get("cdcmLength"))){
pa.setCdcmLength( json.getDouble("cdcmLength"));
}else{
pa.setCdcmLength(0.0);
}
if(null != json.get("cdcmWidth") && !"".equals(json.get("cdcmWidth"))){
pa.setCdcmWidth( json.getDouble("cdcmWidth"));
}else{
pa.setCdcmWidth(0.0);
}
if(null != json.get("cdcmHeight") && !"".equals(json.get("cdcmHeight"))){
pa.setCdcmHeight( json.getDouble("cdcmHeight"));
}else{
pa.setCdcmHeight(0.0);
}
if(null != json.get("cdcmCube") && !"".equals(json.get("cdcmCube"))){
pa.setCdcmCube( json.getDouble("cdcmCube"));
}else{
pa.setCdcmCube(0.0);
}
pa.setProClass(String.valueOf(json.get("proClass")));
pa.setProCode(String.valueOf(json.get("proCode")));
pa.setCusGroup(String.valueOf(json.get("cusGroup")));
pa.setIsEnable(String.valueOf(json.get("isEnable")));
list.add(pa);
}
}
return list;
}
public static void main(String[] args) throws IOException {
ElasticSearchUtil esu = new ElasticSearchUtil();
esu.loadProToEs("2016-04-01 00:00:00","2017-11-30 00:00:00");
//Gson gson = new Gson();
//BoolQueryBuilder qb = QueryBuilders.boolQuery()
//必须查询
//.must(new QueryStringQueryBuilder("宣传").field("proName"))
//或方法
//.must(QueryBuilders.boolQuery()
//.should(QueryBuilders.wildcardQuery("proClass","10122").boost(10f));
//.should(QueryBuilders.wildcardQuery("宣传","proName"));
// );
//.should(new QueryStringQueryBuilder("49276").field("proName"))
// .should(new QueryStringQueryBuilder("49276").field("cdcmId"));
//一个字段 多个查询条件
//WildcardQueryBuilder qb = QueryBuilders.wildcardQuery("proName","*通用*");
//一个查询条件 多个字段匹配(和or同)
//MultiMatchQueryBuilder qb = QueryBuilders.multiMatchQuery("49276", "cdcmId","proName");
//System.out.println(System.currentTimeMillis());
//List<ProductAll> list = esu.searcher(qb);
// System.out.println(System.currentTimeMillis());
//System.out.println(gson.toJson(list));
}
}
es 搜索配置
ElasticSearchUtil esu = new ElasticSearchUtil();
//MultiMatchQueryBuilder qb = QueryBuilders.multiMatchQuery(keyWord, "proCode","proName");
BoolQueryBuilder qb = QueryBuilders.boolQuery()
//必须查询
.must(new QueryStringQueryBuilder(user.getCusGroup()).field("cusGroup"))
.must(QueryBuilders.multiMatchQuery(keyWord, "proCode","proName"));
/*.should(new QueryStringQueryBuilder(keyWord).field("proName"))
.should(new QueryStringQueryBuilder(keyWord).field("proCode"));*/
查询字段
public List<ProductAll> searcher(QueryBuilder queryBuilder,int pageSize){
client = ElasticSearchUtil.getInstance();
List<ProductAll> list = new ArrayList<ProductAll>();
String indexname = ResourcesUtil.getValue("ES", "productIndexName");
String type = ResourcesUtil.getValue("ES", "productIndexType");
SearchRequestBuilder builder = client.prepareSearch(indexname).
setTypes(type).setSearchType(SearchType.DEFAULT)
.setFrom(0).setSize(pageSize);
builder.setQuery(queryBuilder);
SearchResponse response = builder.execute().actionGet();
System.out.println(" ========= " + response);
System.out.println(" hits-=-=-=-= " +response.getHits().getTotalHits());
SearchHits hits = response.getHits();
System.out.println("查询到记录数=" + hits.getTotalHits());
SearchHit[] searchHists = hits.getHits();
if(searchHists.length>0){
for(int i=0;i< searchHists.length;i++){
ProductAll pa = new ProductAll();
JSONObject json = new JSONObject().parseObject(searchHists[i].getSourceAsString());
pa.setCdcmId(String.valueOf(json.get("cdcmId")));
pa.setProName(String.valueOf(json.get("proName")));
if(null != json.get("cdcmLength") && !"".equals(json.get("cdcmLength"))){
pa.setCdcmLength( json.getDouble("cdcmLength"));
}else{
pa.setCdcmLength(0.0);
}
if(null != json.get("cdcmWidth") && !"".equals(json.get("cdcmWidth"))){
pa.setCdcmWidth( json.getDouble("cdcmWidth"));
}else{
pa.setCdcmWidth(0.0);
}
if(null != json.get("cdcmHeight") && !"".equals(json.get("cdcmHeight"))){
pa.setCdcmHeight( json.getDouble("cdcmHeight"));
}else{
pa.setCdcmHeight(0.0);
}
if(null != json.get("cdcmCube") && !"".equals(json.get("cdcmCube"))){
pa.setCdcmCube( json.getDouble("cdcmCube"));
}else{
pa.setCdcmCube(0.0);
}
pa.setProClass(String.valueOf(json.get("proClass")));
pa.setProCode(String.valueOf(json.get("proCode")));
pa.setCusGroup(String.valueOf(json.get("cusGroup")));
pa.setIsEnable(String.valueOf(json.get("isEnable")));
list.add(pa);
}
}
return list;
}
实体对象
public class ProductAll {
/**
* 商品id
*/
private String cdcmId;
/**
* 品牌
* */
private String proBrand;
/**
* 毛重
*/
private Double cdcmWeight;
/**
* 净重
*/
private Double cdcmNetWeight;
/**
* 商品编码
*/
private String proCode;
/**
* 商品名称
*/
private String proName;
/**
*长
* */
private Double cdcmLength;
/**
*宽
* */
private Double cdcmWidth;
/**
*高
* */
private Double cdcmHeight;
/**
* 体积
*/
private Double cdcmCube;
/**
* 商品分类编码
*/
private String proClass;
/**
* 客户组编码
*/
private String cusGroup;
/**
* 是否删除 1:删除 0:正常
*/
private String isEnable;
/**
* 最后修改时间
*/
private String modifyTime;
/**
* 是否是搜索历史
*/
private int isHistory;
//9.包装规格
private String proPackageCode;
//包装规格name
private String proPackageName;
//商品规格
private String proCdcmModel;
//10.包装单位code
private String proUnitCode;
//10.包装单位name
private String proUnitName;
//11.批次(色号)
private String lot;
public Double getCdcmLength() {
return cdcmLength;
}
public void setCdcmLength(Double cdcmLength) {
this.cdcmLength = cdcmLength;
}
public Double getCdcmWidth() {
return cdcmWidth;
}
public void setCdcmWidth(Double cdcmWidth) {
this.cdcmWidth = cdcmWidth;
}
public Double getCdcmHeight() {
return cdcmHeight;
}
public void setCdcmHeight(Double cdcmHeight) {
this.cdcmHeight = cdcmHeight;
}
public Double getCdcmCube() {
return cdcmCube;
}
public void setCdcmCube(Double cdcmCube) {
this.cdcmCube = cdcmCube;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public Double getCdcmWeight() {
return cdcmWeight;
}
public void setCdcmWeight(Double cdcmWeight) {
this.cdcmWeight = cdcmWeight;
}
public Double getCdcmNetWeight() {
return cdcmNetWeight;
}
public void setCdcmNetWeight(Double cdcmNetWeight) {
this.cdcmNetWeight = cdcmNetWeight;
}
public String getProBrand() {
return proBrand;
}
public void setProBrand(String proBrand) {
this.proBrand = proBrand;
}
public String getProClass() {
return proClass;
}
public void setProClass(String proClass) {
this.proClass = proClass;
}
public String getCusGroup() {
return cusGroup;
}
public void setCusGroup(String cusGroup) {
this.cusGroup = cusGroup;
}
public String getIsEnable() {
return isEnable;
}
public void setIsEnable(String isEnable) {
this.isEnable = isEnable;
}
public String getCdcmId() {
return cdcmId;
}
public void setCdcmId(String cdcmId) {
this.cdcmId = cdcmId;
}
public String getModifyTime() {
return modifyTime;
}
public void setModifyTime(String modifyTime) {
this.modifyTime = modifyTime;
}
public int getIsHistory() {
return isHistory;
}
public void setIsHistory(int isHistory) {
this.isHistory = isHistory;
}
public String getProPackageCode() {
return proPackageCode;
}
public void setProPackageCode(String proPackageCode) {
this.proPackageCode = proPackageCode;
}
public String getProUnitCode() {
return proUnitCode;
}
public void setProUnitCode(String proUnitCode) {
this.proUnitCode = proUnitCode;
}
public String getProUnitName() {
return proUnitName;
}
public void setProUnitName(String proUnitName) {
this.proUnitName = proUnitName;
}
public String getLot() {
return lot;
}
public void setLot(String lot) {
this.lot = lot;
}
public String getProPackageName() {
return proPackageName;
}
public void setProPackageName(String proPackageName) {
this.proPackageName = proPackageName;
}
public String getProCdcmModel() {
return proCdcmModel;
}
public void setProCdcmModel(String proCdcmModel) {
this.proCdcmModel = proCdcmModel;
}
}