|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
package com.ruoyi.biemo.elasticsearch.util;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.ruoyi.biemo.business.domain.DocInfo;
|
|
|
|
|
import com.ruoyi.biemo.core.page.Page;
|
|
|
|
|
import com.ruoyi.biemo.elasticsearch.annotation.EsId;
|
|
|
|
@ -11,7 +13,10 @@ import com.ruoyi.common.exception.CustomException;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
import org.apache.poi.ss.formula.functions.T;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
|
@ -21,8 +26,7 @@ import org.elasticsearch.action.search.SearchRequest;
|
|
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
|
|
import org.elasticsearch.action.search.SearchScrollRequest;
|
|
|
|
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
import org.elasticsearch.client.*;
|
|
|
|
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
|
|
import org.elasticsearch.client.indices.CreateIndexResponse;
|
|
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
|
@ -126,8 +130,8 @@ public abstract class EsService<T> {
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 创建mapping
|
|
|
|
|
* @param index 索引
|
|
|
|
|
* @param type 类型
|
|
|
|
|
* @param
|
|
|
|
|
* @param
|
|
|
|
|
* @param clazz 索引类型
|
|
|
|
|
*/
|
|
|
|
|
public boolean createIndexAndCreateMapping( Class clazz, boolean rebuild, int number_of_shards, int number_of_replicas) {
|
|
|
|
@ -161,9 +165,9 @@ public abstract class EsService<T> {
|
|
|
|
|
* 根据信息自动创建索引与mapping
|
|
|
|
|
* 构建mapping描述
|
|
|
|
|
* @param index 索引名称
|
|
|
|
|
* @param type 类型名称
|
|
|
|
|
* @param
|
|
|
|
|
* @param fieldMappingList 字段信息
|
|
|
|
|
* @param client es客户端
|
|
|
|
|
* @param
|
|
|
|
|
* @param number_of_shards 分片数
|
|
|
|
|
* @param number_of_replicas 副本数
|
|
|
|
|
* @return
|
|
|
|
@ -872,6 +876,55 @@ public abstract class EsService<T> {
|
|
|
|
|
return (Class) params[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//分词 返回分词结果
|
|
|
|
|
public List<String> analyzeTitle(DocInfo docInfo){
|
|
|
|
|
try {
|
|
|
|
|
RestHighLevelClient client = ElasticSearchPoolUtil.getClient();
|
|
|
|
|
|
|
|
|
|
Request request = new Request("GET", "_analyze");
|
|
|
|
|
JSONObject entity = new JSONObject();
|
|
|
|
|
entity.put("analyzer", "hanlp_index");
|
|
|
|
|
entity.put("text", docInfo.getTitle());
|
|
|
|
|
request.setJsonEntity(entity.toJSONString());
|
|
|
|
|
Response response = client.getLowLevelClient().performRequest(request);
|
|
|
|
|
JSONObject tokens = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
|
|
|
|
|
JSONArray arrays = tokens.getJSONArray("tokens");
|
|
|
|
|
List<String> list=new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < arrays.size(); i++) {
|
|
|
|
|
JSONObject obj = JSON.parseObject(arrays.getString(i));
|
|
|
|
|
list.add(obj.getString("token"));
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// //分词 返回分词结果
|
|
|
|
|
// public List<String> analyzeTitle(DocInfo docInfo){
|
|
|
|
|
// try {
|
|
|
|
|
// RestHighLevelClient client = ElasticSearchPoolUtil.getClient();
|
|
|
|
|
// RestClient lowLevelClient = client.getLowLevelClient();
|
|
|
|
|
// Request request = new Request("GET", "_analyze");
|
|
|
|
|
// JSONObject entity = new JSONObject();
|
|
|
|
|
// entity.put("analyzer", "ik_max_word");
|
|
|
|
|
// entity.put("text", text);
|
|
|
|
|
// AnalyzeRequestBuilder analyzeRequestBuilder = new AnalyzeRequestBuilder((ElasticsearchClient) lowLevelClient,AnalyzeAction.INSTANCE,"doc_info",docInfo.getTitle());
|
|
|
|
|
// analyzeRequestBuilder.setAnalyzer("hanlp_index");
|
|
|
|
|
// List<AnalyzeAction.AnalyzeToken> analyzeTokenList = analyzeRequestBuilder.execute().actionGet().getTokens();
|
|
|
|
|
// List<String> searchTermList = new ArrayList<>();
|
|
|
|
|
// for (int i = 0; i < analyzeTokenList.size(); i++) {
|
|
|
|
|
// searchTermList.add(String.valueOf(analyzeTokenList.get(i)));
|
|
|
|
|
// }
|
|
|
|
|
// System.out.println("111");
|
|
|
|
|
// return searchTermList;
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// return null;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|