ES设置字段不分词
public void createMapping(String _index, String _type, List<IndexMappingModel> mapping) throws IOException {
if (mapping == null || mapping.size() == 0) throw new EsOperationException("index mapping is null or empty!");
//create mapping
XContentBuilder mapBuilder = XContentFactory.jsonBuilder();
mapBuilder.startObject().startObject(_type).startObject("properties");
equals不等于for (IndexMappingModel mappingModel: mapping) {
if (StringUtils.Field())) {
mapBuilder.Field());
}
if (StringUtils.FieldType())) {
mapBuilder.field("type", FieldType());
}
if (StringUtils.IndexAnalyzed())) {
mapBuilder.field("index", IndexAnalyzed());
}
if (Store() == null || Store()) {
mapBuilder.field("store", "yes");
} else {
mapBuilder.field("store", "no");
}
}
PutMappingRequest putMappingRequest = Requests.putMappingRequest(_index).type(_type).source(mapBuilder); client.admin().indices().putMapping(putMappingRequest).actionGet();
}
//字段参数设置:
public class IndexMappingModel {
private String field; //字段名称
private String fieldType; //字段类型
private String indexAnalyzed; //使⽤的分词器
private Boolean store = true; //是否索引
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getFieldType() {
return fieldType;
}
public void setFieldType(String fieldType) {
this.fieldType = fieldType;
}
public String getIndexAnalyzed() {
return indexAnalyzed;
}
public void setIndexAnalyzed(String indexAnalyzed) { this.indexAnalyzed = indexAnalyzed;
}
public Boolean getStore() {
return store;
}
public void setStore(Boolean store) {
this.store = store;
}
}
类型匹配⽅法:
private void setFieldType(Field declaredField, IndexMappingModel indexMappingModel, boolean isAnalyzed) { declaredField.setAccessible(true);
Class<?> type = Type();
indexMappingModel.Name());
if (Name().equals(Name()) || Name().equals(Name()) || Name().equals(Name())) {
indexMappingModel.setFieldType("double");
}
if (Name().equals(Name()) || Name().equals(Name())) {
indexMappingModel.setFieldType("float");
}
if (Name().equals(Name()) || Name().equals(Name()) ||
indexMappingModel.setFieldType("integer");
}
if (Name().equals(Name()) || Name().equals(Name())) {
indexMappingModel.setFieldType("long");
}
if (Name().equals(Name())) {
indexMappingModel.setFieldType("string");
}
if (Name().equals(Name())) {
indexMappingModel.setFieldType("boolean");
}
if (Name().equals(Name())) {
indexMappingModel.setFieldType("date");
}
if (!isAnalyzed) {
indexMappingModel.setIndexAnalyzed("not_analyzed");
}
}
//案例
private void initContractYearLedgerType(String index, String type, Class clazz) { List<IndexMappingModel> indexMappingModels = new ArrayList<>();
Field[] declaredFields = DeclaredFields();
for (Field declaredField : declaredFields) {
IndexMappingModel indexMappingModel = new IndexMappingModel();
if (Name().equals("shopsnamebase64")
|| Name().equals("goodsnamebase64")
|| Name().equals("compensationmodename")
|| Name().equals("goodsname")
|| Name().equals("shopsname")
|| Name().equals("suppliername")
|| Name().equals("puunit_name")
|| Name().equals("supplier_name")
|| Name().equals("site_name")
|| Name().equals("goods_name")
|| Name().equals("site_name_base64")
|| Name().equals("goods_name_base64")
|| Name().equals("supplier_name")
|| Name().equals("sender_name")
|| Name().equals("receiver_name")
) {
setFieldType(declaredField, indexMappingModel, false);
} else {
setFieldType(declaredField, indexMappingModel, true);
}
indexMappingModels.add(indexMappingModel);
indexMappingModel.setStore(Boolean.TRUE);
}
try {
} catch (IOException e) {
e.printStackTrace();
}
}
备注:Name()多次调⽤不好,应该存起来。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论