码迷,mamicode.com
首页 > Web开发 > 详细

Elasticsearch .net client NEST使用说明 2.x

时间:2016-08-01 19:22:56      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

Elasticsearch .net client NEST使用说明 2.x

引用

Install-Package NEST

 

Connection链接

//单node
Var node = new Uri(“……”);
var settings = new ConnectionSettings(node);
 
//多node
Var nodes = new Uri [] {
       new Uri(“……”),
         new Uri(“……”)
};
//多node
Var nodes = new Node [] {
       new Node (new Uri(“……”)),
         new Node (new Uri(“……”))
};

var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool);
var client = new ElasticClient(settings);

索引选择

方式1:

var settings = new ConnectionSettings().DefaultIndex("defaultindex");

方式2:

var settings = new ConnectionSettings().MapDefaultTypeIndices(m => m.Add(typeof(Project), "projects")  );

方式3:

client.Search<VendorPriceInfo>(s => s.Index("test-index"));

client.Index(data,o=>o.Index("test-index"));

 

优先级:方式3  > 方式2  > 方式1

 

索引操作

创建:

client.CreateIndex("test2");

//基本配置

IIndexState indexState=new IndexState()

{
       Settings = new IndexSettings()

       {
            NumberOfReplicas = 1,//副本数

            NumberOfShards = 5//分片数
       }
};

client.CreateIndex("test2", p => p.InitializeUsing(indexState));

 

判断:

client.IndexExists("test2");

删除:

client.DeleteIndex("test2");

Open/Close:

client.OpenIndex("index");

client.CloseIndex("index");

 

添加索引数据

索引唯一Id:

1)  默认以“Id”字段值作为索引唯一Id值,无“Id”属性,Es自动生成唯一Id值,添加数据时统一类型数据唯一ID已存在相等值,将只做更新处理。

 

2)  标记唯一Id值

[ElasticsearchType(IdProperty = "priceID")]

    public class VendorPriceInfo
    {
        public Int64 priceID { get; set; }

        public int oldID { get; set; }

        public int source { get; set; }
}

 

3)  索引时指定

client.Index(data, o => o.Id(data.vendorName));

优先级: 3) > 2) > 1)

索引类型:

1)  默认类型为索引数据的类名(自动转换为全小写)

2)  标记类型

[ElasticsearchType(Name = "v3")]

    public class VendorPriceInfo
    {
        public Int64 priceID { get; set; }

        public int oldID { get; set; }

        public int source { get; set; }
    }

 

3)  索引时指定

client.Index(data, o => o.Type(new TypeName() { Name = "v5", Type = typeof(VendorPriceInfo) }));

client.Index(data, o => o.Type<MyClass>());

优先级:3> 2) > 1)

添加数据:

添加单条数据

var data = new VendorPriceInfo() { vendorName = "测试"};

client.Index(data);

 

添加多条数据

var datas = new List<VendorPriceInfo> {

                new VendorPriceInfo(){priceID = 1,vendorName = "test1"},

                new VendorPriceInfo(){priceID = 2,vendorName = "test2"}};

client.IndexMany(datas);

删除数据:

单条数据

DocumentPath<VendorPriceInfo> deletePath=new DocumentPath<VendorPriceInfo>(7);

client.Delete(deletePath);

IDeleteRequest request = new DeleteRequest("test3", "vendorpriceinfo", 0);

client.Delete(request);

集合数据

var list=new List<VendorPriceInfo> {

                new VendorPriceInfo(){priceID = 5},

                new VendorPriceInfo(){priceID = 7}};

var response = client.DeleteMany(list);

 

var response = client.DeleteMany(list, "test3", "vendorpriceinfo");

更新数据:

更新所有字段

DocumentPath<VendorPriceInfo> deletePath=new DocumentPath<VendorPriceInfo>(2);

Var response=client.Update(deletePath,(p)=>p.Doc(new VendorPriceInfo(){vendorName = "test2update..."}));

 

IUpdateRequest<VendorPriceInfo, VendorPriceInfo> request = new UpdateRequest<VendorPriceInfo, VendorPriceInfo>(deletePath)
            {
                Doc = new VendorPriceInfo()
                {
                   priceID = 888,

                    vendorName = "test4update........"
                }
            };
            var response = client.Update<VendorPriceInfo, VendorPriceInfo>(request);

更新部分字段

IUpdateRequest<VendorPriceInfo, VendorPriceInfoP> request = new UpdateRequest<VendorPriceInfo, VendorPriceInfoP>(deletePath)
            {
                Doc = new VendorPriceInfoP()
                {
                    priceID = 888,
                    vendorName = "test4update........"
                }
            };
            var response = client.Update(request);

更新部分字段

IUpdateRequest<VendorPriceInfo, object> request = new UpdateRequest<VendorPriceInfo, object>(deletePath)
            {
                Doc = new
                {
                    priceID = 888,
                    vendorName = " test4update........"
                }
            };
            var response = client.Update(request);

client.Update<VendorPriceInfo, object>(deletePath, upt => upt.Doc(new { vendorName = "ptptptptp" }));

获取数据:

获取单条

var response = client.Get(new DocumentPath<VendorPriceInfo>(0));

 

var response = client.Get(new DocumentPath<VendorPriceInfo>(0),pd=>pd.Index("test4").Type("v2"));

获取多条

var response = client.MultiGet(m => m.GetMany<VendorPriceInfo>(new List<long> { 1, 2, 3, 4 }));

搜索:

var result = client.Search<VendorPriceInfo>(
                 s => s
                .Explain()//返回数据的解释信息 https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-explain.html
                .FielddataFields(fs=>fs
                        .Field(p => p.vendorFullName)
                        .Field(p=>p.cbName)
                )
                //.Fields(fs => fs
                //    .Field(p => p.vendorID)
                //    .Field(p => p.cbID)
                //)
                .From(0)//跳过的数据个数
                .Size(pagesize)//返回数据个数
                .Query(q =>
                    //q.Term(p => p.vendorID, vid)
                    //&&
                    //q.Term(p => p.cbID, cbid)
                    //&&
                    q.Match(ma => ma.Field(f => f.vendorName).Query("测试"))
                    //q.Bool(b => b.Must(m => m.Term(p => p.vendorID, vid) && m.Term(p=>p.cbID,cbid)))
                    )//查询条件
                .Sort(st => st.Ascending(asc => asc.vendorPrice))//排序
               );

 

 

var result = client.Search<VendorPriceInfo>(new SearchRequest()
        {
            Sort =new List<ISort>
                   {
                        new SortField { Field = "vendorPrice", Order = SortOrder.Ascending }
                    },
            Size = 10,
            From = 0,
            Query = new TermQuery()
            {
                Field = "priceID",
                Value = 6
            }
            ||
            new TermQuery()
            {
                Field = "priceID",
                Value = 8
            }
        });

 

Elasticsearch .net client NEST使用说明 2.x

标签:

原文地址:http://www.cnblogs.com/huhangfei/p/5726650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!