Solr Schema Java Api 字段定义等相关操作
文章目录
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.CollectionAdminRequest.Delete;
import org.apache.solr.client.solrj.request.schema.SchemaRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.schema.SchemaResponse;
import org.apache.solr.common.cloud.ZkConfigManager;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.collect.Lists;
/**
* 索引节点管理
*
* @author lican
* @date 2018-12-11
*/
@Component
public class CollectionManager {
private static final Logger LOG = LoggerFactory.getLogger(CollectionManager.class);
@Autowired
private CloudSolrClient solrCloudClient;
@Autowired
private ZkConfigManager zkConfigManager;
private final String configPath = this.getClass().getClassLoader().getResource("solrconfig").getPath();
/**
* 获取schemaFields参数
*/
private static ModifiableSolrParams schemaFieldSolrParams = new ModifiableSolrParams();
private static ModifiableSolrParams schemaCopyFieldSolrParams = new ModifiableSolrParams();
private static ModifiableSolrParams schemaUniqueKeySolrParams = new ModifiableSolrParams();
static {
// 是否显示域类型的默认信息
schemaFieldSolrParams.add("showDefaults", "false");
// 是否返回动态域的定义信息
schemaFieldSolrParams.add("includeDynamic", "true");
// 指定返回那些域的定义信息
schemaFieldSolrParams.add("f1", "name,_version_");
schemaCopyFieldSolrParams.add("wt", "json");
// 设置返回sourcefield信息
schemaCopyFieldSolrParams.add("source.fl", "name");
schemaUniqueKeySolrParams.add("wt", "json");
}
/**
* 创建字段
*
* @param param
* @throws Exception
*/
public boolean createFields(String collection, List schemaParams) {
LOG.info("CollectionManager.createFields...collection:{},schema:{}", collection, schemaParams);
try {
List diffFields = getDiffFields(collection, schemaParams);
if (CollectionUtils.isEmpty(diffFields)) {
LOG.info("create fields fail,Fields is exist,collection:{},schemaParams:{}", collection, schemaParams);
return false;
}
for (SchemaParam schemaParam : diffFields) {
SchemaRequest.UniqueKey uniqueKey = new SchemaRequest.UniqueKey(schemaUniqueKeySolrParams);
insertSchemaField(collection, schemaParam);
}
LOG.info("[createFields]create success");
return true;
} catch (Exception e) {
LOG.error("[createFields] create fail,collection:{},schema:{}", collection, schemaParams, e);
return false;
}
}
/**
* 写入单个field到schema
*
* @param collection
* @param schemaParam
* @throws Exception
*/
private void insertSchemaField(String collection, SchemaParam schemaParam) throws Exception {
String fieldName = schemaParam.getName();
// 增加到字段到Collection
Map<String, Object> fieldAttributes = new HashMap<>();
fieldAttributes = new HashMap<>();
fieldAttributes.put(SchemaParam.NAME, fieldName);
fieldAttributes.put(SchemaParam.TYPE, schemaParam.getType());
fieldAttributes.put(SchemaParam.INDEXED, schemaParam.getIndexed());
fieldAttributes.put(SchemaParam.STORED, schemaParam.getStored());
fieldAttributes.put(SchemaParam.REQUIRED, schemaParam.getRequired());
fieldAttributes.put(SchemaParam.MULTIVALUED, schemaParam.getMultiValued());
fieldAttributes.put(SchemaParam.OMIT_NORMS, schemaParam.getOmitNorms());
SchemaRequest.AddField addField = new SchemaRequest.AddField(fieldAttributes);
addField.process(solrCloudClient, collection);
LOG.info("[insertField]field :{},insert success", fieldName);
}
/**
* 对比获取到schema中没有的Field
*
* @param collection
* @param schemaParams
* @return
*/
private List getDiffFields(String collection, List schemaParams) throws Exception {
List diffSchemas = Lists.newArrayList();
List schemaFields = getSchemaFields(collection);
for (SchemaParam schemaParam : schemaParams) {
String fieldName = schemaParam.getName();
if (CollectionUtils.isNotEmpty(schemaFields) && !schemaFields.contains(fieldName)) {
diffSchemas.add(schemaParam);
}
}
return diffSchemas;
}
/**
* 创建 copyField, 可用于数据union 主要参数source、dest,复制与只要将多个域组合成一个域
*
* @param collection
* @param columns
*/
public void createCopyFields(String collection, List schemaParams) {
LOG.info("create copy fields...collection:{},schema:{}", collection, schemaParams);
List destFields = getDiffCopyField(collection, schemaParams);
SchemaRequest.AddCopyField addCopyField = new SchemaRequest.AddCopyField("union_field", destFields);
try {
SchemaResponse.UpdateResponse response = addCopyField.process(solrCloudClient, collection);
NamedList
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论