在.Net环境下使用elasticsearch实现大数据量的搜索

Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。

Elasticsearch使用标准的RESTful API和JSON。我们还用多种语言构建和维护客户机,如Java、Python、。NET、SQL和PHP。此外,我们的社区贡献了更多。它们易于使用,使用起来很自然,而且,就像Elasticsearch一样,不会限制您对它们的使用。

下面看下基于Net的使用

https://github.com/elastic/elasticsearch-net

选择 Elasticsearch.Net 作为客户端

连接

var node = new Uri("http://myserver:9200");
var config = new ConnectionConfiguration(node);
var client = new ElasticLowLevelClient(config);
var myJson = @"{ ""hello"" : ""world"" }";
client.Index<StringResponse>("myindex", "1", myJson);
var myJson = new { hello = "world" };
client.Index<BytesResponse>("myindex", "1", PostData.Serializable(myJson));

创建

public class ElasticSearchClient
    {
        public ElasticLowLevelClient Client { get; }

        private readonly IConfiguration _configuration;

        public ElasticSearchClient(IConfiguration configuration)
        {
            _configuration = configuration;
            Client = InitClient();
        }

        #region Methods
        public async Task<string> Index(string index, string id, PostData body)
        {
            var response = await Client.IndexAsync<StringResponse>(index, id, body);

            ResponseValidate(response);
            return response.Body;
        }

查询

public async Task<List<string>> SearchWithHighLight(string index, string query)
        {
            var response = await Client.SearchAsync<StringResponse>(
                index,
                PostData.Serializable(new
                {
                    from = 0,
                    size = 100,
                    query = new
                    {
                        match = new
                        {
                            content = query
                        }
                    },
                    highlight = new
                    {
                        pre_tags = new[] { "<tag1>", "<tag2>" },
                        post_tags = new[] { "/<tag1>", "/<tag2>" },
                        fields = new
                        {
                            content = new { }
                        }
                    }
                }));

            ResponseValidate(response);
            var responseJson = (JObject)JsonConvert.DeserializeObject(response.Body);

            var hits = responseJson["hits"]["hits"] as JArray;

            var result = new List<string>();

            foreach (var hit in hits)
            {
                var id = hit["_id"].ToObject<string>();

                result.Add(id);
            }

            return result;
        }

删除

public async Task<bool> Delete(string index, string id)
        {
            var response = await Client.DeleteAsync<StringResponse>(index, id);

            ResponseValidate(response);

            return response.Success;
        }
        #endregion

        #region privates
        private ElasticLowLevelClient InitClient()
        {
            var node = new Uri(_configuration.GetConnectionString("ElasticSearch"));
            var settings = new ConnectionConfiguration(node);
            var client = new ElasticLowLevelClient(settings);

            return client;
        }

        private void ResponseValidate(StringResponse response)
        {
            if (response.Success == false)
            {
                throw new ResultException(response.Body);
            }
        }

        #endregion

    }

使用

private readonly ElasticSearchClient _elasticSearchClient;
await _elasticSearchClient.Index(Article.EsIndex, article.ArticleUID,PostData.Serializable(article));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
elasticsearch.net是一个用于与Elasticsearch进行交互的.NET客户端库。该手册为开发人员提供了使用elasticsearch.net库的详细指南和参考资料。 首先,手册介绍了elasticsearch.net库的基本概念和原理,包括如何建立与Elasticsearch集群的连接、使用各种索引、搜索和分析数据的方法等。它还解释了如何使用elasticsearch.net的高级功能,例如使用自定义分析器、处理复杂查询、执行聚合操作等。 除了基本功能之外,手册还介绍了elasticsearch.net库的一些高级特性和最佳实践。例如,如何处理大规模数据集、使用滚动搜索提高性能、处理并发请求等。此外,手册还包含了一些与elasticsearch.net相关的常见问题和解决方案,可以帮助开发人员快速解决常见的问题。 手册的结构清晰,内容易于理解,提供了大的示例代码和实际的用例,使开发人员能够快速上手并开始使用elasticsearch.net库。它还提供了一些额外的资源,如链接到官方文档和讨论区的指南,以及一些建议的学习资源和工具,可以帮助开发人员更深入地了解elasticsearch.netElasticsearch的工作原理。 总的来说,elasticsearch.net手册是使用elasticsearch.net库的必备参考资料,它为开发人员提供了全面的指导和实用的技巧,帮助他们更好地利用Elasticsearch进行数据搜索和分析。无论是初学者还是有经验的开发人员,都可以从该手册中获得巨大的帮助,并且能够更高效地使用elasticsearch.net库来开发和管理Elasticsearch应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值