ASP. NET SignalR with LayIM2.0 easy to implement the web chat room (iv) User search (Elasticsearch), add Friend Process (1).

Source: Internet
Author: User

The previous few basic has achieved most of the instant Messaging features: Chat, group chat, send files, pictures, messages. But these businesses are relatively rugged. Below we have the business refinement, before using the dead data, then we will start with friends. Add friends, first you need to know who you want to add. The Layim interface has a + sign in the lower right corner, and then it pops up to find a friend's interface, but the interface needs to be customized. Because the front-end is not my strength, reluctantly made up a page. But don't mind the details. None of this matters, today I'll focus on the Elasticsearch search solution. It is a Lucene-based search server. It provides a distributed multi-user-capable full-text search engine, based on a restful web interface. Not familiar with the elastic also does not matter, the search for this article to find friends with a relational database. (Do not want to do in-depth study of ES can be a simple look at the idea can be)

Install the ES environment first. The installation procedure can refer to http://www.cnblogs.com/panzi/p/5659697.html.

OK, first look at the molding, the first one is the user, the second is a group.

  

  

OK, so that's the interface. Of course to bind what data can be defined by their own, such as binding signatures, men and women or other information, can, at a higher level, the online information is added. Since the use of Layui, that with the binding plug-in I will directly use the LAYTPL. The front-end templates are as follows: (a user template, a group template).

Again data source, since I used the Elasticsearch (hereinafter used ES instead), but the database (MSSQL) also saved the corresponding information. This requires that when a user registers in, we also need to save the search information in es. The pseudo code is as follows:

  

    var dt = userregister ();     if (DT) {
If the user registers successfully and returns the appropriate information, we will add him to Es. adduserinfotoelastic (DT); }

Is the user information stored in ES, which is synchronized with the data in the database, so when the user information is modified, the data in this table is also synchronized. Of course there are delays and no problem.

  

An advantage of using ES is fast, he can help you do the cache, help you word segmentation query. We do practice using database search Dozens of no problem. If the amount of data is large and the search condition is complex, ES can show his advantage. Now I use the search condition is very simple, one is IM number, similar to QQ, one is nickname. The client of the docking es is plainelastic.net, which can be installed with NuGet. Install-package plainelastic.net.

ES has its own query syntax, but also complex and simple point, I use some simple, after all, I did not go too deep into the study. As mentioned earlier, it provides a restful API. The path we query is: 127.0.0.1:9200/layim/layim_user/_search post method, post parameters are as follows:

  

{  "query"    :{"Match_all": {}//without conditions is match_all equivalent to querying all  },  "from": 0 ,//pagination start   " Size ": +//page Count   " Sort ": {  //Sort, sorted    according to province positive order " province ": {       " Order ":" ASC "}}  }

Above is the query all the situation, when the user entered the query conditions, such as the precise query of the IM number 288186 users, the search criteria are as follows:

 { "query" : { "filtered" : { "filter" " or ": [//or query, below The condition conforms to one can { "term" : { "im": 288186//im =2 88186" query ": { "match_phrase" " nickname ": { "query": "288186" //or nickname with 288186  "slop ": 0 "from": 0" size ": 50"  

Not familiar with the grammar of the classmate may not understand, in short, the above parameters mean that the select top * from Layim_user where im=288186 or nickname like '%288186% '

Query results He will return the total number of queries and time-consuming (ms), like tens of thousands of of the data, most of it is a few milliseconds or even less than 10 milliseconds. Compared to the data used to check, the speed is still possible.

Based on plainelastic.net I also encapsulated the core approach of a query:

 PublicBasequeryentity<t> Querybayconditions (stringquery) {            Try            {                stringcmd = Createsearchcommand ();//Constructing query CommandsOperationresult result = client.post (cmd, query);//Post query parameter queries                vardata = serializer. Tosearchresult<t> (result. Result);//return result Conversion                returngetresults (data); }            Catch(Exception ex) {eslog.writelogexception (Methodbase.getcurrentmethod (),"Query criteria:"+query);                Eslog.writelogexception (Methodbase.getcurrentmethod (), ex); return NewBasequeryentity<t>(); }        }

 Public classBasequeryentity<t>wheret:baseentity {/// <summary>        ///number of Hit bars/// </summary>         Public LongHits {Get;Set; } /// <summary>        ///Time spent (MS in unit)/// </summary>         Public Longtook {Get;Set; }  PublicIenumerable<t> List {Get;Set; } }

Query result JSON:

A total of 3,883 users, ES query with 5ms, plus the HTTP request time-consuming, a total of 45 milliseconds.

Write here, for the time being a simple summary of ES, in fact, if you do not need to use him to check, directly check the database can be, if the buffer with the speed is not slow, and today's business logic is not complicated. Plainly, this article is about a crud Retrieve. (R) There is also chat record by the way, but also with ES, but it will add some small effect. I also do not suspense, is the Blur query keyword highlighting effect.

Continue to say add friend process, first, if you want to add a user as a friend first, you have to send a friend request, similar to QQ. Of course, if that person set up anyone can add friends, is it possible to add directly. If that person is set up to not allow anyone to add friends, then you can not add him, so, simple one plus friends may be designed very complex. This article describes the general application process. Click Add Friend, pop-up box, fill in the additional message: (We search for 288186 of the user added)

What do we have to think about when we click send? Don't look at the following, think carefully. 5, 4, 3, 2, 1 ...

  

Yes, if the other person happens to be online, and the other person is not on the line, if the other person is online, save to the database and instantly alert. Not the line, save to the database, and so on the next login to alert the user. Detailed practice next write it again. Put a preview chart first:

  

This post is a lot of pictures, talk about some business things and es simple use, scene and use of the way. This is the right place to go.

Next trailer :"Intermediate" ASP. SignalR with LayIM2.0 easy to implement the web chat room (v) Add Friends, add Group message prompt, the user usage in the hub.

Want to learn the small partner, can follow my blog Oh, my Qq:645857874,email:[email protected]

ASP. NET SignalR with LayIM2.0 easy to implement the web chat room (iv) User search (Elasticsearch), add Friend Process (1).

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.