• ElasticSearch 2.X升级到6.X遇到的几个问题


    1.IndexExists检测索引是否存在,更简洁了,可以这样

    _ElasticClient.IndexExists(indices : indexName).Exists

    2.索引数据的时候,如果数据是个字符串json,那么索引后查看数据是有问题的,索引的数据需要是个Model对象,此情况是个人遇到的,不确定是否有其他操作方法

    3.如果索引数据是做了路由处理的话,那么使用Id搜索或者删除也必须加上路由信息,不然会404找不到的,此情况只针对Id搜索或删除

    _ElasticClient.Get(new DocumentPath<T>(esId), s => s.Type(_EsType).Routing(routing))
    
    _ElasticClient.DeleteAsync(new DeleteRequest(_ElasticClient.ConnectionSettings.DefaultIndex, _EsType, esId) { Routing = routing });

     4.es.dll 6.x的使用虽然摆脱了Newtonsoft.Json的依赖,但也出现一些新的问题,序列化会和Newtonsoft不太一样,导致老项目无法直接使用,比如c# Model中的属性“Name”标记了jsonproperty["nm"],但6.x中索引的数据并没有使用nm,依然是Name,这就是6.x中自带序列化的问题。对于这个情况,6.x其实提供了一个接口IElasticsearchSerializer,只需要去实现并且使用即可,使用方法如下

    var _ConnectionPool = new StaticConnectionPool(uris);
    var _ConnectionConfig = new ConnectionSettings(_ConnectionPool, sourceSerializer: (builtin, settingss) => new JsonNetSerializer());
    var _ElasticClient = new ElasticClient(_ConnectionConfig.DefaultIndex("IndexName").DefaultFieldNameInferrer((name) => name));

     JsonNetSerializer 是新建的实现类,继承IElasticsearchSerializer,实现如下

    public class JsonNetSerializer : IElasticsearchSerializer
    {
        private static JsonSerializer serializer = JsonSerializer.CreateDefault();
        private static JsonSerializer indentedSerializer = 
            JsonSerializer.CreateDefault(new JsonSerializerSettings
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            });
        
        public object Deserialize(Type type, Stream stream)
        {
            var streamReader = new StreamReader(stream);
            var reader = new JsonTextReader(streamReader);
            return serializer.Deserialize(reader, type);
        }
    
        public T Deserialize<T>(Stream stream)
        {
            return (T)Deserialize(typeof(T), stream);
        }
    
        public Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken))
        {
            var o = Deserialize(type, stream);
            return Task.FromResult(o);
        }
    
        public Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
        {
            var o = Deserialize<T>(stream);
            return Task.FromResult(o);
        }
    
        public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented)
        {
            var writer = new StreamWriter(stream);
            if (formatting == SerializationFormatting.Indented)
                indentedSerializer.Serialize(writer, data, typeof(T));
            else
                serializer.Serialize(writer, data, typeof(T));    
                
            writer.Flush();
        }
    
        public Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented, CancellationToken cancellationToken = default(CancellationToken))
        {
            Serialize(data, stream, formatting);
            return Task.CompletedTask;
        }
    }
  • 相关阅读:
    多行文本溢出显示省略号(…)
    CSS3 grayscale滤镜图片变黑白实例页面
    腾讯TGideas语义化标签(转)
    jQuery Ajax通用js封装
    动态加载 js
    Javascript的四种继承方式
    Android四大组件之Activity
    Android四大组件通信的Intent介绍和详解
    oracle学习笔记(十四) 数据库对象 索引 视图 序列 同义词
    oracle学习笔记(十三) 查询练习(三) 子查询查询
  • 原文地址:https://www.cnblogs.com/CuiRicky/p/9772189.html
Copyright © 2020-2023  润新知