提交 7c639bfe 编写于 作者: FelixHPP's avatar FelixHPP

修复DSL查询错误BUG

上级 6db3fbbb
...@@ -3,6 +3,9 @@ package indi.felix.easy.core.elastic.model; ...@@ -3,6 +3,9 @@ package indi.felix.easy.core.elastic.model;
import indi.felix.easy.core.elastic.model.metadata.*; import indi.felix.easy.core.elastic.model.metadata.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
/**
* ES 字段验证, 防止把日期类型字段、数值类型字段等设置了分词
*/
public class ElasticFieldValid { public class ElasticFieldValid {
/** /**
* keyword 类型字段后缀集合 * keyword 类型字段后缀集合
......
...@@ -22,7 +22,7 @@ public class SettingModel { ...@@ -22,7 +22,7 @@ public class SettingModel {
private int shards; private int shards;
/** /**
* 副本数, 默认1, dynamic * 副本数, 默认0, dynamic
*/ */
private int replicas; private int replicas;
...@@ -38,13 +38,16 @@ public class SettingModel { ...@@ -38,13 +38,16 @@ public class SettingModel {
*/ */
private int max_result_window; private int max_result_window;
/**
* 设置ES 字段最大上限数量
*/
private int totalFields; private int totalFields;
public SettingModel() { public SettingModel() {
this.shards = 1; this.shards = 1;
this.replicas = 0; this.replicas = 0;
this.totalFields = 5000; this.totalFields = 5000;
// this.refreshInterval = 1s this.refreshInterval = 1;
this.max_result_window = 10000; this.max_result_window = 10000;
} }
...@@ -52,23 +55,23 @@ public class SettingModel { ...@@ -52,23 +55,23 @@ public class SettingModel {
Map<String, Object> setting = new HashMap<>(); Map<String, Object> setting = new HashMap<>();
if (shards >= 0) { if (shards >= 0) {
setting.put("number_of_shards", shards); setting.put(ElasticConstant.INDEX_NUMBER_OF_SHARDS, shards);
} }
if (replicas >= 0) { if (replicas >= 0) {
setting.put("number_of_replicas", replicas); setting.put(ElasticConstant.INDEX_NUMBER_OF_REPLICA, replicas);
} }
if (refreshInterval >= 0) { if (refreshInterval >= 0) {
setting.put("index.refresh_interval", refreshInterval); setting.put(ElasticConstant.INDEX_REFRESH_INTERVAL, refreshInterval + "s");
} }
if (totalFields >= 0) { if (totalFields >= 0) {
setting.put("index.mapping.total_fields.limit", totalFields); setting.put(ElasticConstant.INDEX_MAPPING_TOTAL_FIELDS_LIMIT, totalFields);
} }
if (max_result_window >= 0) { if (max_result_window >= 0) {
setting.put("index.max_result_window", max_result_window); setting.put(ElasticConstant.INDEX_MAX_RESULT_WINDOW, max_result_window);
} }
return setting; return setting;
......
...@@ -224,7 +224,7 @@ public class EasyRest extends BaseRest { ...@@ -224,7 +224,7 @@ public class EasyRest extends BaseRest {
* @param properties * @param properties
* @return * @return
*/ */
public boolean createIndex(String index, SettingModel settings, MappingModel properties) { public boolean createIndexByModel(String index, SettingModel settings, MappingModel properties) {
Map<String, Object> mapping = new HashMap<>(); Map<String, Object> mapping = new HashMap<>();
Map<String, Object> setting = new HashMap<>(); Map<String, Object> setting = new HashMap<>();
...@@ -276,7 +276,7 @@ public class EasyRest extends BaseRest { ...@@ -276,7 +276,7 @@ public class EasyRest extends BaseRest {
String routing; String routing;
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
DSLEnum dslEnum = DSLEnum.getByQuery(method, endpointType.name()); DSLEnum dslEnum = DSLEnum.getByQuery(method, endpointType.getName());
if (dslEnum == null) { if (dslEnum == null) {
msg = "不支持" + method + "和" + dslEnum + "的组合类型,请重新选择."; msg = "不支持" + method + "和" + dslEnum + "的组合类型,请重新选择.";
map.put("status", false); map.put("status", false);
...@@ -285,25 +285,25 @@ public class EasyRest extends BaseRest { ...@@ -285,25 +285,25 @@ public class EasyRest extends BaseRest {
} }
switch (dslEnum) { switch (dslEnum) {
case DSL_GET_SEARCH: case DSL_GET_SEARCH:
endpoint = "/" + dslIndex + "/" + endpointType.name() + "?format=json&pretty"; endpoint = "/" + dslIndex + "/" + endpointType.getName() + "?format=json&pretty";
break; break;
case DSL_GET_COUNT: case DSL_GET_COUNT:
endpoint = "/" + dslIndex + "/" + endpointType.name(); endpoint = "/" + dslIndex + "/" + endpointType.getName();
break; break;
case DSL_GET_MAPPING: case DSL_GET_MAPPING:
endpoint = "/" + dslIndex + "/" + endpointType.name() + "?format=json&pretty"; endpoint = "/" + dslIndex + "/" + endpointType.getName() + "?format=json&pretty";
break; break;
case DSL_PUT_MAPPING: case DSL_PUT_MAPPING:
endpoint = "/" + dslIndex + "/" + endpointType.name(); endpoint = "/" + dslIndex + "/" + endpointType.getName();
break; break;
case DSL_GET_SETTINGS: case DSL_GET_SETTINGS:
endpoint = "/" + dslIndex + "/" + endpointType.name() + "?format=json&pretty"; endpoint = "/" + dslIndex + "/" + endpointType.getName() + "?format=json&pretty";
break; break;
case DSL_PUT_SETTINGS: case DSL_PUT_SETTINGS:
endpoint = "/" + dslIndex + "/" + endpointType.name(); endpoint = "/" + dslIndex + "/" + endpointType.getName();
break; break;
case DSL_POST_ANALYZE: case DSL_POST_ANALYZE:
endpoint = "/" + dslIndex + "/" + endpointType.name() + "?format=json&pretty"; endpoint = "/" + dslIndex + "/" + endpointType.getName() + "?format=json&pretty";
break; break;
case DSL_DELETE_BY_ID: case DSL_DELETE_BY_ID:
case DSL_UPDATE_BY_ID: case DSL_UPDATE_BY_ID:
...@@ -326,7 +326,7 @@ public class EasyRest extends BaseRest { ...@@ -326,7 +326,7 @@ public class EasyRest extends BaseRest {
break; break;
default: default:
ret = false; ret = false;
msg = "不支持" + method + "和" + endpointType.name() + "的组合类型,请重新选择."; msg = "不支持" + method + "和" + endpointType.getName() + "的组合类型,请重新选择.";
break; break;
} }
......
...@@ -2,6 +2,7 @@ package indi.felix.easy.core.elastic.rest; ...@@ -2,6 +2,7 @@ package indi.felix.easy.core.elastic.rest;
import indi.felix.easy.core.elastic.client.XContentUtils; import indi.felix.easy.core.elastic.client.XContentUtils;
import indi.felix.easy.core.elastic.model.ESFieldTypeEnum; import indi.felix.easy.core.elastic.model.ESFieldTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity; import org.apache.http.nio.entity.NStringEntity;
...@@ -60,22 +61,47 @@ public class IndexRestApi extends BaseRest { ...@@ -60,22 +61,47 @@ public class IndexRestApi extends BaseRest {
return responseBody(defaultMethod, index); return responseBody(defaultMethod, index);
} }
/** /**
* curl -XGET 'localhost:9200/_mapping * curl -XGET 'localhost:9200/_settings
* *
* @return 多个索引用英文逗号分隔 * @return 多个索引用英文逗号分隔
*/ */
public String getMapping() { public String getSetting() {
return getSetting(null);
}
/**
* curl -XGET 'localhost:9200/_settings
*
* @return 多个索引用英文逗号分隔
*/
public String getSetting(String index) {
String newEndpoint = ""; String newEndpoint = "";
if (StringUtils.isNotBlank(index)) {
index = index + "/";
} else {
index = "";
}
if (!isAllowIncludeType()) { if (!isAllowIncludeType()) {
newEndpoint = "_mapping"; newEndpoint = index + "_settings";
} else { } else {
// 6.x版本无法识别include_type_name参数 // 6.x版本无法识别include_type_name参数
newEndpoint = "_mapping?include_type_name=true"; newEndpoint = index + "_settings?include_type_name=true";
} }
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
} }
/**
* curl -XGET 'localhost:9200/_mapping
*
* @return 多个索引用英文逗号分隔
*/
public String getMapping() {
return getMapping(null);
}
/** /**
* curl -XGET 'localhost:9200/twitter/_mapping * curl -XGET 'localhost:9200/twitter/_mapping
...@@ -84,11 +110,18 @@ public class IndexRestApi extends BaseRest { ...@@ -84,11 +110,18 @@ public class IndexRestApi extends BaseRest {
*/ */
public String getMapping(String index) { public String getMapping(String index) {
String newEndpoint = ""; String newEndpoint = "";
if (StringUtils.isNotBlank(index)) {
index = index + "/";
} else {
index = "";
}
if (!isAllowIncludeType()) { if (!isAllowIncludeType()) {
newEndpoint = index + "/_mapping"; newEndpoint = index + "_mapping";
} else { } else {
// 6.x版本无法识别include_type_name参数 // 6.x版本无法识别include_type_name参数
newEndpoint = index + "/_mapping?include_type_name=true"; newEndpoint = index + "_mapping?include_type_name=true";
} }
return responseBody(defaultMethod, newEndpoint); return responseBody(defaultMethod, newEndpoint);
......
...@@ -17,4 +17,8 @@ public enum EndpointType { ...@@ -17,4 +17,8 @@ public enum EndpointType {
this.type = type; this.type = type;
this.name = name; this.name = name;
} }
public String getName(){
return name;
}
} }
...@@ -142,6 +142,14 @@ public class EasyRestTest extends TestCase { ...@@ -142,6 +142,14 @@ public class EasyRestTest extends TestCase {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
} }
String indexName2 = "test000231";
Assert.assertTrue(easyEs.rest().createIndex(indexName2, null, null));
Assert.assertTrue(easyEs.rest().indexExists(indexName2));
System.out.println(easyEs.rest().index().getMapping(indexName2));
System.out.println(easyEs.rest().index().getSetting(indexName2));
Assert.assertTrue(easyEs.rest().deleteIndex(indexName2));
} }
public void testMain(){ public void testMain(){
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册