一、基本操作
[if !supportLists]1. [endif]创建索引(字段属性为not_analyzed类型)
POST 10.120.135.104:9200/analysis_engine
{
"mappings" : {
"fa_test" : {
"properties" : {
"interface_name" : {
"type" : "string",
"index" : "not_analyzed"
},
"request_params":{
"type" : "string",
"index" : "not_analyzed"
},
"trace_id":{
"type" : "string",
"index" : "not_analyzed"
},
"request_type":{
"type" : "string",
"index" : "not_analyzed"
},
"request_mode":{
"type" : "string",
"index" : "not_analyzed"
},
"version":{
"type" : "integer",
"index" : "not_analyzed"
},
"response_content":{
"type" : "string",
"index" : "not_analyzed"
},
"preInterfaceInfo":{
"type" : "string",
"index" : "not_analyzed"
},
"nextInterfaceInfo":{
"type" : "string",
"index" : "not_analyzed"
},
"request_time":{
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
说明:
Index: analysis_engine(相当于数据库的概念)
type: fa_test(相当于数据库里的表概念)
[if !supportLists]2. [endif]插入文档
POST 10.120.135.104:9200/tester/tester/1
{
"interface_name": "fa.163.com:10007/favicon.ico",
"request_params": "",
"trace_id": "2b8faa0e2d2c4f31ba5f36155dc1de22",
"request_type": "GET",
"request_mode": "SOURCE",
"version": 1,
"response_content":"aaaaaa",
"preInterfaceInfo": null,
"nextInterfaceInfo": null,
"request_time": 1533627190823
}
[if !supportLists]3. [endif]搜索最大id值
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"_source":false,
"sort":
{
"_uid": {
"order": "desc"
}
},
"size":1
}
[if !supportLists]4. [endif]统计表中数据总条数
GET 10.120.135.104:9200/analysis_engine/fa_test/_count
[if !supportLists]5. [endif]新增一列
POST 10.120.135.104:9200/analysis_engine/_mapping/fa_test
{
"properties":{
"version":{"type":"integer"}
}
}
新增的列名为version,类型为integer
[if !supportLists]6. [endif]按条件搜索并排序
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"query": {
"term": {
"interface_name": "ddd"
}
},
"sort": {
"version": {
"order": "desc"
}
}
}
[if !supportLists]7. [endif]按条件搜索
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"query": {
"match": {
"interface_name": "fa.163.com:10007/interfaces/activePush/getToken.do"
}
}
}
[if !supportLists]8. [endif]搜索全量数据
GET 10.120.135.104:9200/analysis_engine/fa_test/_search?size=10000
[if !supportLists]9. [endif]按某个字段名和字段值精准搜索
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"interface_name" : "fa.163.com:10007/interfaces/activePush/getToken.do"
}
}
}
}
}
[if !supportLists]10. [endif]根据id查询
POST http://10.120.135.104:9200/analysis_engine/_mget?pretty
{
"ids":["1","2"]
}
或者:
GET http://10.120.135.104:9200/analysis_engine/fa_test/1
[if !supportLists]11. [endif]删除索引
DELETE 10.120.135.104:9200/analysis_engine
[if !supportLists]12. [endif]根据id删除
DELETE 10.120.135.104:9200/analysis_engine/fa_test/10
[if !supportLists]13. [endif]按范围搜索
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"query": {
"range": {
"request_time": {
"gt": 1533714426135,"lt":1533714426139
}
}
},
"size":10000
}
[if !supportLists]14. [endif]按范围搜索并返回指定的字段值(必须指定size否则默认只返回10条数据)
POST 10.120.135.104:9200/analysis_engine/fa_test/_search
{
"_source":{
"include":["trace_id","request_mode","request_time"]
},
"query": {
"range": {
"request_time": {
"gt": 1533714416135,"lt":1533714446139
}
}
},
"size":10000
}
[if !supportLists]15. [endif]多条件查询(bool查询)
POST 10.120.135.104:9200/analysis_engine/fa_test/_search?size=10000
{
"query":
{
"bool": {
"must": { "term": { "trace_id": "3b8faa0e2d2c4f31ba5f36155dc1de22" }},
"must": { "term": { "request_time": "2633627190249" }}
}
},
"size":100
}
[if !supportLists]二、[endif]JAVA API
[if !supportLists]1. [endif]ES连接器
/**
* AnalysisEngine——EsConnector
*
* @author ZhangChi
* @date 2018年6月25日---下午8:11:01
* @version 1.0
*/
@Service("esConnector")
public class EsConnector {
private static Logger logger = LoggerFactory.getLogger("EsConnector");
private static RestClient restClient;
@Value("${elasticSearch.host}")
private String host;
@Value("${elasticSearch.port}")
private int port;
@PostConstruct
public void initConnection() {
HttpHost[] httpHosts = new HttpHost[1];
httpHosts[0] = new HttpHost(host, port);
restClient = RestClient.builder(httpHosts).build();
}
public Response request(EsMethod method, String endPoint) {
try {
return restClient.performRequest(method.toString(), endPoint);
} catch (IOException e) {
logger.error("[ESConnector] 请求失败! method={},endPoint={}", method.toString(), endPoint, e);
}
return null;
}
public Response request(EsMethod method, String endPoint, String requetBody) {
try {
HttpEntity entity = new StringEntity(requetBody, ContentType.APPLICATION_JSON);
return restClient.performRequest(method.toString(), endPoint, Collections.emptyMap(), entity,
new Header[0]);
} catch (IOException e) {
logger.error("[ESConnector] 请求失败! method={},endPoint={},requetBody={}",
new Object[] { method.toString(), endPoint, requetBody, e });
}
return null;
}
public Response request(EsMethod method, String db, String table, String _id) {
try {
String endPoint = "";
if (db != null) {
endPoint = endPoint + "/" + db;
}
if (table != null) {
endPoint = endPoint + "/" + table;
}
if (table != null) {
endPoint = endPoint + "/" + _id;
}
return restClient.performRequest(method.toString(), endPoint, new Header[0]);
} catch (IOException e) {
logger.error("[ESConnector] method={},db={},table={},id={}",
new Object[] { method.toString(), db, table, _id, e });
}
return null;
}
public RestClient getClient() {
return restClient;
}
public Response clientRequest(EsMethod method, String db, String table, String _id, Map params,
HttpEntity entity, Header... headers) {
Response indexResponse = null;
try {
indexResponse = restClient.performRequest(method.toString(), "/" + db + "/" + table + "/" + _id, params,
entity, headers);
} catch (IOException e) {
logger.error("[ESConnector] 请求失败! method={},db={},table={},_id={},entity={}",
new Object[] { method.toString(), db, table, _id, JsonUtils.toJson(entity), e });
}
return indexResponse;
}
}
[if !supportLists]2. [endif]ES索引操作
/**
* AnalysisEngine——EsIndexServiceImpl
*
* @author ZhangChi
* @date 2018年7月2日---上午10:59:17
* @version 1.0
*/
@Service("esIndexServiceImpl")
public class EsIndexServiceImpl implements EsIndexService {
@Autowired
private EsConnector esConnector;
public Response createCommonIndexTemplate() {
try {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("template", "analysis_engine*")
.field("order", 0).startObject("mappings").startObject("_default_").startObject("_all")
.field("enabled", false).endObject().startArray("dynamic_templates").startObject()
.startObject("strings_ik").field("match_mapping_type", "string").field("match", "content_*")
.startObject("mapping").field("type", "string").field("index", "analyzed")
.field("analyzer", "ik_max_word").field("search_analyzer", "ik_max_word").endObject().endObject()
.endObject().startObject().startObject("strings").field("match_mapping_type", "string")
.startObject("mapping").field("type", "string").field("index", "not_analyzed").endObject()
.endObject().endObject().endArray().endObject().endObject().endObject();
return createIndexTemplate("analysis_engine", builder);
} catch (Exception e) {
LogConstant.runLog.error("[CreateIndexTemplate] error : ", e);
e.printStackTrace();
}
return null;
}
public Response createIndexTemplate(String templateName, XContentBuilder builder) {
try {
return createIndexTemplate(templateName, builder.string());
} catch (Exception e) {
LogConstant.runLog.error("[EsIndexServiceImpl]create index template : ", e);
e.printStackTrace();
}
return null;
}
/*创建索引模板*/
@Override
public Response createIndexTemplate(String templateName, String template) {
try {
String queryStr = templateExist(templateName);
if (StringUtils.isEmpty(queryStr)) {
LogConstant.runLog.info("[index template]" + template);
HttpEntity entity = new NStringEntity(template, ContentType.APPLICATION_JSON);
return this.esConnector.getClient().performRequest("PUT", "/_template/" + templateName,
Collections.emptyMap(), entity, new Header[0]);
}
LogConstant.runLog.info("Create Index Success!");
} catch (Exception e) {
LogConstant.runLog.error("[EsIndexServiceImpl] Create Index Template : ", e);
e.printStackTrace();
}
return null;
}
/*删除索引*/
@Override
public int deleteIndex(String indexName) {
try {
if (indexExist(indexName).booleanValue()) {
Response response = this.esConnector.request(EsMethod.DELETE, indexName);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return 1;
}
} else {
LogConstant.runLog.info("[Es deleteIndex] " + indexName + "not exists!");
return 0;
}
} catch (Exception e) {
LogConstant.runLog.warn("[ES deleteIndex]", e);
}
return 0;
}
private String templateExist(String templateName) {
Response response = null;
try {
response = this.esConnector.getClient().performRequest("GET", "/_template/" + templateName,
Collections.emptyMap(), new Header[0]);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
}
return null;
}
private Boolean indexExist(String indexName) {
Response response = this.esConnector.request(EsMethod.HEAD, indexName);
try {
if (response.getStatusLine().getStatusCode() == 200) {
return Boolean.valueOf(true);
}
} catch (Exception e) {
LogConstant.runLog.warn("[indexExist]", e);
}
return Boolean.valueOf(false);
}
}
[if !supportLists]3. [endif]ES文档操作
ES文档操可以基于ES连接器,是在其基础上的封装
/* 插入 */
@Override
public Response insert(String index, String type, String _id, Object entity) {
/**
* 对应关系:index-db,type-table
*/
return insert(index, type, _id, JsonUtils.toJson(entity));
}
private Response insert(String db, String table, String _id, String json) {
LogUtils.log().debug("[DocumentServiceImp.insert],index={},type={},id={}", new Object[] { db, table, _id });
HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);
return esConnector.clientRequest(EsMethod.POST, db, table, _id, Collections.emptyMap(), entity, new Header[0]);
}
/* 删除 */
@Override
public Response deleteById(String db, String table, String id) {
LogUtils.log().info("[DocumentServiceImp.deleteById],index={},type={},id={}", new Object[] { db, table, id });
return esConnector.request(EsMethod.DELETE, db, table, id);
}
/* 查询 */
@Override
public Response queryById(String db, String table, String id) {
LogUtils.log().info("[DocumentServiceImp.queryById],index={},type={},id={}", new Object[] { db, table, id });
return esConnector.request(EsMethod.GET, db, table, id);
}
/* 统计 */
@Override
public Response countByIndex(String indexName) {
return esConnector.request(EsMethod.GET, indexName + "/_count");
}
[if !supportLists]4. [endif]ES搜索操作
ES搜索操可以基于ES连接器和ES文档操作,是在其基础上的封装
/* 类sql搜索*/
@Override
public String queryByQuerySql(String db, String table, String querysql) {
// TODO Auto-generated method stub
LogUtils.log().debug("[SearchServiceImpl.queryByQuerySql] db={},table={},querybody={}",
new Object[] { db, table, querysql });
Response response = esDocumentService.queryByQuerySql(db, table, querysql);
return EsUtils.parseResponse2Str(response);
}
/**
* 类sql搜索,返回Hits 入参clazz: ResponseHits.class
*/
@Override
public Hits queryByQuerySql(String db, String table, String querysql, Class clazz) {
LogUtils.log().debug("[SearchServiceImpl.queryByQuerySql] db={},table={},querybody={}",
new Object[] { db, table, querysql });
Response response = esDocumentService.queryByQuerySql(db, table, querysql);
return EsUtils.parseResponse(response, clazz);
}
/**
* 全量查询
*/
@Override
public String queryAllData(String db, String table, String query, String time, String scroll_id) {
// TODO Auto-generated method stub
StringBuilder endPoint = new StringBuilder();
endPoint.append("/").append(db).append("/").append(table).append("/_search?scroll=").append(scroll_id);
Response response = esConnector.request(EsMethod.GET, endPoint.toString(), query);
return EsUtils.parseResponse2Str(response);
}