.net core elasticsearch 打印查询DSL,以及model设置mapping,查询nested结构化数据

该博客介绍了如何使用Elasticsearch的.NET客户端Nest来执行查询,并开启DebugMode以打印查询DSL。示例代码展示了如何创建索引、插入数据以及执行查询,包括对nested字段的查询操作。同时,还提到了Elasticsearch的属性映射在.NET中的应用。
摘要由CSDN通过智能技术生成
  1. 设置可以打印查询dsl
    https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/debug-mode.html

需要在连接设置上打开EnableDebugMode,并在执行完查询之后,获取DebugInformation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elasticsearch.Net;
using ElasticSearchHelper.Models;
using Nest;
using Newtonsoft.Json;

namespace ElasticSearchHelper.Helper
{
    public class EsHelper
    {
        private readonly ElasticClient _elasticClient;
        public EsHelper()
        {
            var connectorpool = new SingleNodeConnectionPool(new Uri("http://192.168.214.132:9200"));
            var connect = new ConnectionSettings(connectorpool).BasicAuthentication("elastic","ellischen").EnableDebugMode().PrettyJson()
    .RequestTimeout(TimeSpan.FromMinutes(2));
            _elasticClient = new ElasticClient(connect);
        }


        public List<QueryModel> TestNestQuery()
        {
            SearchRequest searchrequest = new SearchRequest<QueryModel>("myindex")
            {
                Query = new NestedQuery()
                {
                    Path = "aaa_meta",
                    Query = new TermQuery
                    {
                        Field = "aaa_meta.username",
                        Value ="chen"
                    },
                    IgnoreUnmapped = true
                }
            };

            ISearchResponse<QueryModel> result = _elasticClient.Search<QueryModel>(searchrequest);

            // 打印出来query dsl
            var debugInformation = result.DebugInformation;

            return result.Documents.ToList();

        }

        public bool create(string index)
        {
            if (_elasticClient.Indices.Exists(index) ==null)
                _elasticClient.Indices.Create(index, c => c
        .Map<QueryModel>(m => m.AutoMap()));
            return true;
        }


        public string TestQuery()
        {
            SearchRequest searchrequest = new SearchRequest<QueryModel>("myindex")
            {
                Query = new TermQuery()
                {
                    Field="test",
                    Value="test"
                }
            };
            var result = _elasticClient.Search<QueryModel>(searchrequest);

            return JsonConvert.SerializeObject(result);
        }
    }
}

  1. 创建model到es数据的映射
    https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/attribute-mapping.html
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElasticSearchHelper.Models
{
    public class QueryModel
    {
        [Nested(Name ="aaa_meta")]
        public List<AModel> AMeta { get; set; }


        [Nested(Name = "bbb_meta")]
        public BModel BMeta { get; set; }


        [Text(Name = "test")]
        public string Test { get; set; }
    }


    public class AModel
    {
        [Text(Name ="age")]
        public string AgeNumber { get; set; }

        [Text(Name = "username")]
        public string User_Name { get; set; }
        [Text(Name = "staffname")]
        public string Staff_Name { get; set; }
    }


    public class BModel
    {
        [Text(Name = "a")]
        public string AField { get; set; }

        [Text(Name = "b")]
        public string BField { get; set; }

        [Text(Name = "c")]
        public string CField { get; set; }

        [Nested(Name = "d")]
        public List<BChild> Children { get; set; }
    }


    public class BChild
    {
        [Text(Name ="h")]
        public string  TestField { get; set; }
    }
}

执行第一步里面的create 创建index
3. 插入数据

POST myindex/_doc/1
{
  "test":"test",
  "aaa_meta":[{"username":"chen","staffname":"ellis","age":"100"}],
  "bbb_meta":{"a":"haha","b":"bbb","c":"100","d":[{"h":"vv"}]}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值