ES设置字段不分词

原创
2016/11/30 11:21
阅读数 1.3W
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");

        for (IndexMappingModel mappingModel: mapping) {
            if (StringUtils.isNotBlank(mappingModel.getField())) {
                mapBuilder.startObject(mappingModel.getField());
            }
            if (StringUtils.isNotBlank(mappingModel.getFieldType())) {
                mapBuilder.field("type", mappingModel.getFieldType());
            }
            if (StringUtils.isNotBlank(mappingModel.getIndexAnalyzed())) {
                mapBuilder.field("index", mappingModel.getIndexAnalyzed());
            }
            if (mappingModel.getStore() == null || mappingModel.getStore()) {
                mapBuilder.field("store", "yes");
            } else {
                mapBuilder.field("store", "no");
            }
            mapBuilder.endObject();
        }

        mapBuilder.endObject().endObject().endObject();

        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 = declaredField.getType();
    indexMappingModel.setField(declaredField.getName());
    if (type.getName().equals(BigDecimal.class.getName()) || type.getName().equals(Double.class.getName()) || type.getName().equals(double.class.getName())) {
        indexMappingModel.setFieldType("double");
    }
    if (type.getName().equals(Float.class.getName()) || type.getName().equals(float.class.getName())) {
        indexMappingModel.setFieldType("float");
    }
    if (type.getName().equals(int.class.getName()) || type.getName().equals(Integer.class.getName()) || type.getName().equals(BigInteger.class.getName())) {
        indexMappingModel.setFieldType("integer");
    }
    if (type.getName().equals(long.class.getName()) || type.getName().equals(Long.class.getName())) {
        indexMappingModel.setFieldType("long");
    }
    if (type.getName().equals(String.class.getName())) {
        indexMappingModel.setFieldType("string");
    }
    if (type.getName().equals(Boolean.class.getName())) {
        indexMappingModel.setFieldType("boolean");
    }
    if (type.getName().equals(Date.class.getName())) {
        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 = clazz.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        IndexMappingModel indexMappingModel = new IndexMappingModel();
        if (declaredField.getName().equals("shopsnamebase64")
                || declaredField.getName().equals("goodsnamebase64")
                || declaredField.getName().equals("compensationmodename")
                || declaredField.getName().equals("goodsname")
                || declaredField.getName().equals("shopsname")
                || declaredField.getName().equals("suppliername")
                || declaredField.getName().equals("puunit_name")
                || declaredField.getName().equals("supplier_name")
                || declaredField.getName().equals("site_name")
                || declaredField.getName().equals("goods_name")
                || declaredField.getName().equals("site_name_base64")
                || declaredField.getName().equals("goods_name_base64")
                || declaredField.getName().equals("supplier_name")
                || declaredField.getName().equals("sender_name")
                || declaredField.getName().equals("receiver_name")
                ) {
            setFieldType(declaredField, indexMappingModel, false);
        } else {
            setFieldType(declaredField, indexMappingModel, true);
        }
        indexMappingModels.add(indexMappingModel);
        indexMappingModel.setStore(Boolean.TRUE);
    }
    try {
        esClient.createMapping(index, type, indexMappingModels);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

备注:type.getName()多次调用不好,应该存起来。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部