ElasticSearch.net c# 讀寫示例 elasticSearch7.8


class Person
{
public int id { get; set; }
public int gender { get; set; }
public string name { get; set; }
public DateTime birthday { get; set; }
}

 參考官方文檔 https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/elasticsearch-net-getting-started.html

 private void Search()
        {

            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/")).RequestTimeout(TimeSpan.FromMinutes(2));
                var lowlevelClient = new ElasticLowLevelClient(settings);
                var searchResponse = lowlevelClient.Search<StringResponse>("people", PostData.Serializable(new
                {
                    from = 0,
                    size = 10,
                    query = new
                    {
                        match = new
                        {
                            gender = 0
                        }
                    }
                }));

                var successful = searchResponse.Success;
                var responseJson = searchResponse.Body;
            }
            catch (Exception exception)
            {
                
            }
        }
        private void Add()
        {
            var person = new Person
            {
                id=1,gender = 0,name = "張伶俐",birthday = DateTime.Now.AddYears(-10)
            };
            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/")).RequestTimeout(TimeSpan.FromMinutes(2));
                var lowlevelClient = new ElasticLowLevelClient(settings);
                var ndexResponse = lowlevelClient.Index<BytesResponse>("people", "1", PostData.Serializable(person));
                byte[] responseBytes = ndexResponse.Body;
            }
            catch (Exception ex)
            {}
        }

 可用直接調用 get   http://localhost:9200/people/_search?q=gender:0  搜索也可以出現結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM