ElasticSearch返回不同的type的序列化

Posted 易网无疆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch返回不同的type的序列化相关的知识,希望对你有一定的参考价值。

总体思路是:

利用json序列化的别名方法,反序列化到不同的字段上;

因为别名方法不支持多个别名,所以不得不根据不同的type,定义了多套适配内容。

最终在属性上进行选择。

 

本示例ElasticSearch返回的json串形如:

{

    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 3.4698925,
        "hits": [
            {
                "_index": "user",
                "_type": "userinfo",
                "_id": "1031014642252640",
                "_score": 3.4698925,
                "_source": {
                    "uid": 1031014642252640,
                    "level": 20
                }
            }
            ,
            {
                "_index": "user",
                "_type": "good",
                "_id": "1",
                "_score": 0.06378032,
                "_source": {
                    "id": 1,
                    "name": "good luck"
                }
            }
        ]
    }

}

 

 

using PlainElastic.Net;
using PlainElastic.Net.Queries;
using PlainElastic.Net.Serialization;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace TestQuery
{
    [Serializable]
    public class Show
    {
        /* A实体反序列化内容 */
        [JsonProperty(PropertyName = "uid")]
        private string title_1;
        [JsonProperty(PropertyName = "level")]
        private string content_1;

        /* B实体反序列化内容 */
        [JsonProperty(PropertyName = "id")]
        private string title_2;

        [JsonProperty(PropertyName = "name")]
        private string content_2;

        /* AB实体反序列化内容汇总返回 */
        public string Title
        {
            get
            {
                return title_1 ?? title_2;
            }
        }
        public string Content
        {
            get
            {
                return content_1 ?? content_2;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var query = new QueryBuilder<Object>()
                .Query(q =>
                            q.Bool(b =>
                                        b.Should(m =>
                                                    m.Term(tm =>
                                                                {
                                                                    tm.Field("uid");
                                                                    tm.Value("1031014642252640");
                                                                    return tm;
                                                                }).Custom(@"{
                                                                      ""exists"": {
                                                                            ""field"": ""name""
                                                                      }
                                                                    }")
                                                  )
                                    )
                            )
            .From(0)
            .Size(100)
            //.Sort(s => s.Field(UserInfoField.level, SortDirection.desc))
            .BuildBeautified();
            Console.WriteLine(query);
            List<Show> list = new List<Show>();
            var cmd = new SearchCommand("user");
            var client = new ElasticConnection("localhost", 9200);
            var operationResult = client.Post(cmd, query);
            var serializer = new JsonNetSerializer();
            var hits = serializer.ToSearchResult<Show>(operationResult).hits;
            var serializerResult = hits.hits;
            //页面显示实体
            //
            foreach (var item in serializerResult)
            {
                list.Add(item._source);
            } 
  
        }
    }
}

 

以上是关于ElasticSearch返回不同的type的序列化的主要内容,如果未能解决你的问题,请参考以下文章

.NET(C#)通过JSON.NET反序列化Elasticsearch返回响应的结果

ElasticSearch7.3 学习之type底层结构及弃用原因

ElasticSearch批量查询

查询Elasticsearch嵌套类型数据,且只返回嵌套数据中命中的元素

elasticsearch - 返回字段的标记

使用elasticsearch时所遇问题