Elasticsearch Getting Started crud (add, query, modify, delete)

Source: Internet
Author: User

  It Exchange QQ Group: 79190545 (SQL server/java/asp.net/c#/js/jquery/oracle/my SQL/)--Welcome everyone to come in chat, Exchange! (regular cleaning of students who do not speak, diving too deep)

1. References to elasticsearch.net in the project

Elasticsearch Other versions available in: http://www.nuget.org/find the corresponding project to source!

This article takes version 2.4.1 as an example

First we open the project, click-Tools--"NuGet Package Manager--" Package Manager console

Enter the installation command in console: Install-package nest-version 2.4.1 (then enter to confirm the installation)

Installation succeeded:

2. CRUD cases in the project

The model class is created first based on the data object.

and add: [Jsonproperty ("field name") above the field property]

<span style= "font-size:10px;" >using newtonsoft.json;using system;using system.collections.generic;using system.linq;using System.runtime.serialization;using system.web;namespace webapplication1.models{//[DataContract] public class Order  s {//[DataMember] [Jsonproperty ("OrderID")]//newtonsoft.json namespace public string OrderID {get; Set        } [Jsonproperty ("Ispackageorder")] public int Ispackageorder {get; set;}        [Jsonproperty ("Packageorderid")] public string Packageorderid {get; set;}        [Jsonproperty ("Crsresvid")] public string Crsresvid {get; set;}        [Jsonproperty ("Guaranteemodecode")] public string Guaranteemodecode {get; set;}        [Jsonproperty ("Hotelid")] public string Hotelid {get; set;}        [Jsonproperty ("Guestsrccode")] public string Guestsrccode {get; set;}        [Jsonproperty ("Hoteldiscountcode")] public string Hoteldiscountcode {get; set;} [JSonproperty ("Membercardtypecode")] public string Membercardtypecode {get; set;}        [Jsonproperty ("Resvroutecode")] public string Resvroutecode {get; set;}        [Jsonproperty ("Guestid")] public string Guestid {get; set;}        [Jsonproperty ("Agreementid")] public string Agreementid {get; set;}        [Jsonproperty ("Membercardno")] public string Membercardno {get; set;}        [Jsonproperty ("Teamid")] public string Teamid {get; set;}        [Jsonproperty ("Amount")] public string Amount {get; set;}        [Jsonproperty ("ContactName")] public string ContactName {get; set;}        [Jsonproperty ("Contactmobile")] public string Contactmobile {get; set;}        [Jsonproperty ("Contactphone")] public string Contactphone {get; set;}        [Jsonproperty ("Contactfax")] public string Contactfax {get; set;}    [Jsonproperty ("Contactemail")] public string Contactemail {get; set;}    [Jsonproperty ("Remark1")] public string Remark1 {get; set;}        [Jsonproperty ("REMARK2")] public string Remark2 {get; set;}        [Jsonproperty ("Resvtime")] public string Resvtime {get; set;}        [Jsonproperty ("Resvcitycode")] public string Resvcitycode {get; set;}        [Jsonproperty ("Isprepaid")] public int Isprepaid {get; set;}        [Jsonproperty ("Orderstatuscode")] public string Orderstatuscode {get; set;}        [Jsonproperty ("Cancelreasoncode")] public string Cancelreasoncode {get; set;}        [Jsonproperty ("Confirmtypecode")] public string Confirmtypecode {get; set;}        [Jsonproperty ("Inputstarttime")] public string Inputstarttime {get; set;}        [Jsonproperty ("Inputspanms")] public int Inputspanms {get; set;}        [Jsonproperty ("Inputworkno")] public string Inputworkno {get; set;} [Jsonproperty ("CreatedOn")] public string CreatedOn {get; set; } [Jsonproperty ("ModifiedBy")] public string ModifiedBy {get; set;}        [Jsonproperty ("ModifiedOn")] public string ModifiedOn {get; set;}        [Jsonproperty ("TimeStamp")] public string TimeStamp {get; set;}        [Jsonproperty ("Crsoldresvroutecode")] public string Crsoldresvroutecode {get; set;}        [Jsonproperty ("Actualdiscount")] public float Actualdiscount {get; set;}        [Jsonproperty ("AgentName")] public string AgentName {get; set;}        [Jsonproperty ("Discounttypetag")] public int Discounttypetag {get; set;}        [Jsonproperty ("Fixedprice")] public string Fixedprice {get; set;}        [Jsonproperty ("Agentcode")] public string Agentcode {get; set;}        [Jsonproperty ("Isapartment")] public int isapartment {get; set;}        [Jsonproperty ("Teamprotocalid")] public string Teamprotocalid {get; set;} [Jsonproperty ("Invoicetitle")] public string INVOicetitle {get; set;}        [Jsonproperty ("Pointincrementaltimes")] public string Pointincrementaltimes {get; set;}    [Jsonproperty ("Resvtype")] public string Resvtype {get; set;} }}</span>

        <summary>//Create client instances///</summary>//        <returns></returns>        Private Static Elasticclient getsearchclient ()        {            //server IP Address: port number. (elasticsearch default port: 9200)             var node = new Uri ("");             var settings = new ConnectionSettings (node);            var client = new Elasticclient (settings);            Verifies if the index exists            if (!client. Indexexists ("Order"). Exists)              {                client. CreateIndex ("Order");    Index             }            //Return Elasticsearch operation instance return            client;        }

2.1 New


<span style= "font-size:10px;"            >//Operation Elasticsearch instance elasticclient client = Getsearchclient ();            New object instantiation of orders Ordermodel = new orders ();            Assignment Ordermodel.orderid = "79190545";            Ordermodel.contactname = "It communication QQ Group: 79190545";                                                            Initialize the inserted object iindexrequest<orders> o = new Indexrequest<orders> ( "Order",//_index: Index "Orde     Rhis ",//_type: Type Ordermodel.orderid//_id : id) {Document = Ordermodel,//New            Add data Object}; Call new Iindexresponse Inxresponse = client.            Index (o); if (inxresponse.isvalid) {//operation succeeded} Else {//operation failed}</span> 

2.2 Queries

<span style= "Font-size:32px;font-weight:bold;" > </span><span style= "font-size:10px;"  >string key = "It Exchange Mutual help group: 79190545"; The content that needs to be queried int IX = 1;            Current page//instantiation ElasticSearch operand elasticclient client = Getsearchclient (); Execute Query var response = client. Search<orders> (s = s). From ((ix-1) * 5)//Start page. Size (5)//number of pages per page.                                                Query (q =//Search filter condition//This will not be queried.) Q.wildcard (t = t.contactname, String. Format ("*{0}*", key.                                                 ToLower ())) | | Exact Find//q.term (t = T.orderid, key. ToLower ())//| | Q.term (t = t.contactmobile, key.                                              ToLower ())  //|| Q.term (t = t.contactphone, key. ToLower ())//Fuzzy search q.wildcard (t = t.or Derid, String. Format ("*{0}*", key. ToLower ())) | | Q.wildcard (t = t.contactmobile, String. Format ("*{0}*", key. ToLower ())) | | Q.wildcard (t = T.contactphone, String. Format ("*{0}*", key.                              ToLower ())//Such query, default Word search (equivalent to like '% of the first word% ' or similar '% of the second word% ' or ' like '% of the third word% ') In fact, you only need to query like '% Han Xiping '//q.match (MQ = = MQ. Field (f = f.orderid). Analyzer (key. ToLower ())//| | Q.match (MQ = MQ. Field (f = f.contactname = = key))//| | Q.match (MQ = MQ. Field (f = f.contactmobile). Query (key))//| | Q.match (MQ = MQ. Field (f = f.contactphone).                             Query (Key))  )             ); The query Results object list<orders> resultlist = Response. Documents.tolist (); </span><span style= "font-size:32px;font-weight:bold;" ></span>

2.3 Modifications

<span style= "Font-weight:bold; font-size:32px; " > </span><span style= "font-size:10px;"            >//instantiation of elascticsearch operand elasticclient client = Getsearchclient ();    Instantiating modified objects Orders Ordermodel = new orders () {ContactName = "It exchange mutual help Q Group: 79190545",            Need to modify the data, do not need to modify the can not write};                                                                                    The instantiation request modifies the object updaterequest<orders, orders> updrequest = new Updaterequest<orders, orders> (                                                                                    "Order",//_index: Index                                                                                    "Orderhis",//_type: Type                                                                                    "R1401001950922"//_ID: Flag ID            ) {Doc = Ordermodel,//object required for modification          };  Call Modify iupdateresponse<orders> updresponse = client.            Update<orders> (updrequest); if (updresponse.isvalid) {//operation succeeded} else {// Failure}</span>

2.4 Delete

<span style= "Font-weight:bold; font-size:32px; " > </span><span style= "font-size:10px;"            >//instantiation of elascticsearch operand elasticclient client = Getsearchclient ();                                                                    Instantiate delete Request Object documentpath<orders> DocPath = new Documentpath<orders> (                                                                    "R1401001950922"//_ID: Flag ID            ); Ideleteresponse delresponse = client.                                                                Delete<orders> (DocPath,//objects that need to be deleted                                                                p = P . Index ("Order")//_index: Indexes.            Type ("Orderhis")//_type: Types); if (Delresponse.isvalid) {//operation succeeded} else {//operation failed}&LT;/S Pan><span style= "Font-weight:bold; font-size:32px; " ></span>

  It Exchange QQ Group: 79190545 (SQL server/java/asp.net/c#/js/jquery/oracle/my SQL/)--Welcome everyone to come in chat, Exchange! (regular cleaning of students who do not speak, diving too deep)

Elasticsearch Getting Started crud (add, query, modify, delete)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.