开发elasticsearch遇到的问题

1.查询报错all shards failed
原因一:可能是因为查询条数过多。
解决办法:设置最大查询数量。
原因二:spring boot提供的elasticsearch依赖,使用createIndex方法时,不会创建mapping。缺少mapping,查询就会报这个错。
可以在kibana中查询创建的索引是否有mapping
正确的index:

{
  "pt-elasticsearch-info": {
    "aliases": {},
    "mappings": {
      "info": {
        "properties": {
          "content": {
            "type": "text"
          },
          "createTime": {
            "type": "text"
          },
          "createUserId": {
            "type": "text"
          },
          "id": {
            "type": "long"
          },
          "indexName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "indexType": {
            "type": "text"
          },
          "infoType": {
            "type": "text"
          },
          "size": {
            "type": "integer"
          },
          "title": {
            "type": "text"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1606636705734",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "Yc0081jgSj6wxqzLXTA8QQ",
        "version": {
          "created": "6040299"
        },
        "provided_name": "pt-elasticsearch-info"
      }
    }
  }
}

错误的index:

{
  "pt-elasticsearch-info": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1606636705734",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "Yc0081jgSj6wxqzLXTA8QQ",
        "version": {
          "created": "6040299"
        },
        "provided_name": "pt-elasticsearch-info"
      }
    }
  }
}

解决办法:在创建index时候,创建mapping。这里我是使用实体bean转的mapping

package com.shineyue.index;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.Data;

/**
 * 基础index
 * 
 * @author Administrator
 *
 */
@Data
@Document(indexName = "pt-elasticsearch-base", type = "base")
public class BaseIndex {
	@Id
	private String id;
	/**
	 * 时间自增ID
	 */
	private String indexType;
	private String title;
	private String content;
	private int size;
	private String createTime;
}

bean转mapping的方法:
根据类的路径转换成mapping

public JSONObject classToMapping(String className, String indexType) {
		Class<?> aClass = null;
		try {
			aClass = ClassLoader.getSystemClassLoader().loadClass(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 这里是需要转换对象
		Field[] fields = aClass.getDeclaredFields();
		JSONObject jsonObject = new JSONObject();
		for (int i = 0; i < fields.length; i++) {
			String name = fields[i].getName();
			JSONObject sub = new JSONObject();
			String type = fields[i].getType().getTypeName();
			if (type.contains("Integer") || type.contains("int")) {
				sub.put("type", "integer");
			} else if (type.contains("Long") || type.contains("long")) {
				sub.put("type", "long");
			} else if (type.contains("String")) {
				// sub.put("type", "keyword");
				sub.put("type", "text");
			} else if (type.contains("Boolean") || type.contains("boolean")) {
				sub.put("type", "boolean");
			} else if (type.contains("BigDecimal")) {
				sub.put("type", "float");
			} else if (type.contains("Date")) {
				sub.put("type", "date");
			} else {
				sub.put("type", "todo");
			}
			jsonObject.put(name, sub);
		}
		log.info("转换成的mapping==={}", JSONObject.toJSONString(jsonObject));
		JSONObject mapping2 = new JSONObject();
		JSONObject mapping3 = new JSONObject();
		mapping2.put("properties", jsonObject);
		mapping3.put(indexType, mapping2);
		return mapping3;
	}

然后在创建完成index后,创建mapping

String className = StringTool.concat("com.shineyue.index.", StringTool.InitialCap(bean.getString("indexType")),
				"Index");
		JSONObject mapping = classToMapping(className, indexType);
		Boolean createIndexFlag = elasticsearchTemplate.createIndex(bean.getString("indexName"));
		Boolean putMappingFlag = true;
		if (createIndexFlag) {
			// 创建index成功后,根据类转换成mapping创建mapping
			Class cls = null;
			try {
				cls = Class.forName(className);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			putMappingFlag = elasticsearchTemplate.putMapping(cls, mapping);
			// putMappingFlag =
			// elasticsearchTemplate.putMapping(CustomerIndex.class, mapping);

		}

java-elasticsearch-demo的git地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值